作者:手机用户2602916917 | 来源:互联网 | 2023-06-25 20:21
i keep getting an error code 462, when i try to run my code a second time. The code is supposed to create a word document with data from excel, and then prompt user to save that document. It is the save part of the code, that gives the error.
当我尝试第二次运行我的代码时,我不断收到错误代码462。该代码应该使用excel中的数据创建一个word文档,然后提示用户保存该文档。它是代码的保存部分,它给出了错误。
Here is the code:
这是代码:
Sub ExportToWord()
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
Dim SrcePath As String
Range("G3:J29").Copy
'Create an Instance of MS Word
On Error Resume Next
'Is MS Word already opened?
Set WordApp = GetObject(class:="Word.Application")
'Clear the error between errors
Err.Clear
'If MS Word is not already open then open MS Word
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
'Handle if the Word Application is not found
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, aborting."
GoTo EndRoutine
End If
On Error GoTo 0
'Make MS Word Visible and Active
WordApp.Visible = True
WordApp.Activate
'Create a New Document
Set myDoc = WordApp.Documents.Add
'Paste Table into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
'Autofit Table so it fits inside Word Document
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
'Insert Header logo
SrcePath = "C:\Users\SIDVI\Pictures\logo.gif"
myDoc.Sections.Item(1).Headers(wdHeaderFooterPrimary) _
.Range.InlineShapes.AddPicture (SrcePath)
'Prompts users to save document
Documents.Save NoPrompt:=False
If Err.Number = 462 Then
GoTo EndRoutine
End If
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
I have tried to get it go ignore the error message, by saying go to endrutine but this does not work either.
我试图让它去忽略错误信息,说去endrutine,但这也不起作用。
2 个解决方案