【发布时间】:2017-11-07 12:31:02
【问题描述】:
当我再次尝试运行我的代码时,我不断收到错误代码 462。该代码应该使用来自 excel 的数据创建一个 word 文档,然后提示用户保存该文档。这是代码的保存部分,它给出了错误。
代码如下:
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
我试图让它忽略错误消息,说 go to endrutine 但这也不起作用。
【问题讨论】:
-
尝试在最后关闭文档。文档的名称是如何保存的?
-
关闭文档不起作用....代码提示用户保存,以便他们在保存时选择文档的名称。
-
谢谢,但我已经经历过这些,谷歌在这个问题上没有多大帮助。
-
至少应该是
WordApp.Documents.Save NoPrompt:=False,尤其是当您通过 Excel 运行时。