MS Word VBA:修复运行时5487错误

时间:2017-08-25 13:16:42

标签: vba ms-word word-vba

我已经在这段相同的代码上工作了几天,试图让运行时5487错误消失,但没有任何运气。我很感激我能得到的任何帮助。错误发生在一行:

ActiveDocument.SaveAs Fullname, fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False. 

当我调试它时,文件路径名称为E:\assessment_rubrics\Program Art-Teaching Certif BA BS .docx

Function cvtstr(strIn As String) As String
    Dim i As Integer

    Const str = "/|?*<>"":"
    cvtstr = strIn
    For i = 1 To Len(str)
    cvtstr = Replace(cvtstr, Mid$(str, i, 1), " ")
Next i
End Function


Sub Splitter()

' splitter Macro

' Macro created by Doug Robbins to save each letter created by a mailmergeas a separate file.
Application.ScreenUpdating = False
Dim Program As String
Dim DocName As String
Dim Letters As Integer, Counter As Integer
Dim file_name, extension, Fullname, filepath, Mask As String

Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
'program =  ActiveDocument.MailMerge.DataSource.DataFields("Program_Outcomes_PlanReport_Name").Value
DocName = "Reports" & LTrim$(str$(Counter))  'Generic name of document
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
'ActiveDocument.Sections(2).PageSetup.SectionStart = wdSectionContinuous

file_name = cvtstr(ActiveDocument.Paragraphs(1).Range.Text)
file_name = Left(file_name, Len(file_name) - 1)
extension = ".docx"
DocName = "E:\assessment_rubrics\" & filename & file_name
Fullname = DocName & extension

Debug.Print Fullname
Debug.Print file_name

ActiveDocument.SaveAs Fullname, fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False

ActiveWindow.Close
Counter = Counter + 1
Wend

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

我可以通过转到https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_other/word-visual-basic-code/f1a23c49-da37-4974-9287-b224644ae617?auth=1来解决问题。事实证明我的文件名在其末尾包含一个段落标记,因此使用file_name = Replace(file_name, Chr(13), "")我能够解决问题!