Word 2010 Hangs when macro is used to ExportAsFixedFormat to PDF

I have a document that I want to automate the conversion to PDF format.  When I run the macros below manually Word does the conversion ok, but if I run the macro test() (with Word open and no documents open), word just hangs and I have to force quit the application.  This happens on Windows 7 and Windows 8 but does NOT happen on XP.  Also tested with Word 2007, Word 2010 and Word 2013, all do the same thing.

I think the issue is the document has nested tables in it?  Doesn't make sense if I can do it manually or run the 2 macros independently.  It just freezes when I run them back to back with the test() macro.

Note: I can step through the test macro and it will work if I go slow enough, if I hit F8 too fast it will freeze.  I tried to put a delay in between the steps but it still freezes.

Sub openfile()
    Documents.Open FileName:="c:\temp\test.docx", ConfirmConversions _
        :=False, ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
End Sub

Sub export()
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\temp\test.pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=False, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=False, BitmapMissingFonts:= _
        True, UseISO19005_1:=False
    ActiveDocument.Close
End Sub

Sub test()
    openfile
    export

End Sub

I had the same problem with excel withe multiple sheets. I was creating and additional sheet to an existing document.

Finally I resolved by making the sheet as default "Page break preview" Mode and saved the excel.

 excelApp.ActiveWindow.View = Excel.XlWindowView.xlPageBreakPreview;

Hopes this helps.

Regards

www.vouchercodes.net

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I would think that it is probably a timing issue. Try running the test macro with the following code

Sub openandExportfile()
    Documents.Open FileName:="c:\temp\test.docx", ConfirmConversions _
        :=False, ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
      ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\temp\test.pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=False, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=False, BitmapMissingFonts:= _
        True, UseISO19005_1:=False
    ActiveDocument.Close
End Sub

Sub test()
    openandExportfile

End Sub

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 736 Applies to: