vbams-wordword-2010

Why does Word VBA tell me that a dialog box is open?


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

Solution

  • It should be ActiveDocument.Close instead of ActiveWindow.Close.