I'm trying to copy a selection to a new document and then export it to PDF, but I'm getting an error that I cannot resolve.
On the line ActiveWindow.Close False
I'm being told
Run-time error '5479':
You cannot close Microsoft Word because a dialog box is open. Click OK, switch to word and then close the dialog box.
Unless the export to PDF operation is firing up a dialog, I do not know what dialog would be open.
Can anyone please shed some light? Thanks.
'** Copy the selection *'
Selection.Copy
'** Create a new document to paste the copied text into *'
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
'** Paste the copied text *'
Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
'** Change the location of the file open directory *'
ChangeFileOpenDirectory OpenDirectory
'** Export the document to PDF *'
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
OpenDirectory & "\ZENworks.pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'** Close the new document *'
ActiveWindow.Close False
CopyToNewDocument = True
它应该是ActiveDocument.Close
而不是ActiveWindow.Close
。
Especially when working with multiple files, it's a best practice to use object variables to represent the files, so that you have an easier handle on them and don't need to try and remember which is Active at any particular time, you can just refer to the object directly.
For your specific problem, make sure you are closing the Document , not the Window .
Sub foo()
Dim OpenDirectory As String
Dim tempDoc As Document
'Modify as needed...
OpenDirectory = CreateObject("Wscript.Shell").SpecialFolders("Desktop")
Selection.Copy
'** Create a new document to paste the copied text into *'
Set tempDoc = Documents.Add(Template:="Normal", NewTemplate:=False, DocumentType:=0)
'** Paste the copied text *'
tempDoc.Range.PasteAndFormat (wdUseDestinationStylesRecovery)
'** Change the location of the file open directory *'
ChangeFileOpenDirectory OpenDirectory
'** Export the document to PDF *'
tempDoc.ExportAsFixedFormat OutputFileName:= _
OpenDirectory & "\ZENworks.pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'** Close the new DOCUMENT *'
tempDoc.Close
CopyToNewDocument = True
End Sub
NB: it's usually recommended to avoid relying on Activate
and Select
methods (this explains it in detail). Of course this is not as possible to avoid the Selection
in Word, but the broader point remains that avoiding it when possible will lead to better code that's easier to read and maintain in the future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.