当我运行excel宏并出现错误5487(文件权限错误)时,我该怎么办?
我在excel中编写了一个marco。宏可以从excel文件读取数据,并将数据填充到word模板文件中。每行数据将生成一个word文件 运行代码时出现错误5487。 以下是我的代码。希望有人能指出错误的原因。非常感谢当我运行excel宏并出现错误5487(文件权限错误)时,我该怎么办?,excel,vba,ms-word,Excel,Vba,Ms Word,我在excel中编写了一个marco。宏可以从excel文件读取数据,并将数据填充到word模板文件中。每行数据将生成一个word文件 运行代码时出现错误5487。 以下是我的代码。希望有人能指出错误的原因。非常感谢 Sub output() Dim wordApp As Object Dim wordDoc As Object Dim savedFileName As String 'Dim counter As Long
Sub output()
Dim wordApp As Object
Dim wordDoc As Object
Dim savedFileName As String
'Dim counter As Long
'On Error GoTo CLOSEWORD
'press the button
msg = MsgBox("Be about to fill in data. Don't do anything!" & Chr(13) & "run or not?", _
vbQuestion + vbYesNo, "confirm?")
If msg = 7 Then
Exit Sub
End If
'check if there is the template
fileAddress = Dir(ThisWorkbook.Path & "\template.doc")
If fileAddress = "" Then
msg = MsgBox("templete doesn't exist!", vbCritical, "WRONG")
Exit Sub
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'do the job
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open(ThisWorkbook.Path & "" & fileAddress)
For i = 2 To ThisWorkbook.Sheets(1).UsedRange.Rows.Count
'tempI = ThisWorkbook.Sheets(1).UsedRange.Rows.Count
wordDoc.Tables(1).Cell(1, 2) = ThisWorkbook.Sheets(1).Cells(i, 1)
wordDoc.Tables(1).Cell(2, 2) = ThisWorkbook.Sheets(1).Cells(i, 3)
wordDoc.Tables(1).Cell(2, 4) = ThisWorkbook.Sheets(1).Cells(i, 2)
wordDoc.Tables(1).Cell(3, 2) = "$" & moneyChange(ThisWorkbook.Sheets(1).Cells(i, 5) * 100000000)
wordDoc.Tables(1).Cell(4, 2) = ThisWorkbook.Sheets(1).Cells(i, 4)
wordDoc.Tables(1).Cell(4, 4) = ThisWorkbook.Sheets(1).Cells(2, 11) & ThisWorkbook.Sheets(1).Cells(2, 12)
wordDoc.Tables(1).Cell(5, 2) = ThisWorkbook.Sheets(1).Cells(i, 6)
wordDoc.Tables(1).Cell(6, 2) = "$" & moneyChange(ThisWorkbook.Sheets(1).Cells(i, 6))
wordDoc.Tables(1).Cell(7, 2) = ThisWorkbook.Sheets(1).Cells(i, 7)
wordDoc.Tables(1).Cell(8, 2) = ThisWorkbook.Sheets(1).Cells(i, 9)
wordDoc.Tables(1).Cell(9, 2) = ThisWorkbook.Sheets(1).Cells(i, 8)
wordDoc.Tables(1).Cell(9, 4) = ThisWorkbook.Sheets(1).Cells(i, 10)
wordDoc.Tables(1).Cell(12, 2) = ThisWorkbook.Sheets(1).Cells(i, 3)
'save file
savedFileName = ThisWorkbook.Path & "" & ThisWorkbook.Sheets(1).Cells(i, 2) & ThisWorkbook.Sheets(1).Cells(i, 1) & ".doc"
wordDoc.SaveAs (savedFileName)
'counter = counter + 1
Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True
wordDoc.Close
Set wordApp = Nothing
msg = MsgBox("complete", vbOKOnly, "Mission Complete")
End Sub
谢谢拉法布。
savedFileName包含一些特殊字符,使宏向下。在第
savedFileName
行后添加MsgBox savedFileName
或Debug.print savedFileName
。仔细检查SaveAs
方法之前的名称是否有效。也去掉花括号。你似乎在拼写英语单词方面有问题。例如“马可”和“坦普尔特”。因此,可能您的代码、变量名或使用的字符串中存在简单的输入错误。在代码中,这些细节很重要。