none
Minimizing a pdf file from Word 2010 VBA RRS feed

  • Question

  • Hello,

    I have a Word macro that saves a file as a pdf. Then the macro continues on but the pdf file that was created is on the foreground and I want to minimize it.

    The code to create the pdf is simply:

        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            vFileName _
            , 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

    Is there an easy way to minimize the pdf file from my macro?

    Thanks so much,

    Jill

    Friday, October 4, 2013 3:39 PM

All replies

  • Hi Jill,

    If you are working with a 32-bit version of Office on the PC you can insert this into your code. Hope it helps.

        Dim shell As New Shell32.shell
        shell.minimizeAll
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, October 4, 2013 10:39 PM
  • Hi Rich,

    Thanks for the suggestion and I am using a 32 bit version of Word 2010. When I add the code I get a compile error: User defined type not defined.

    What am I missing?

    Thanks,

    JillE

    Tuesday, October 8, 2013 7:56 PM
  • Press Alt + F11 then go to Tools > References... and add a reference for Microsoft Shell Controls and Automation.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, October 8, 2013 9:15 PM
  • Hi Rich,

    Makes sense! I added the reference and retested and while no error occurs anymore, the Adobe Reader window is still maximized.

    I'm not familiar with the shell control. I did a bit of research but was unable to find anything that significantly helped my understanding. I see its used to open apps but in the example you provided, I don't see how the code is actually connecting to the instance of Adobe Reader that's running.

    If you can provide any further assistance, I would be grateful, and/or if you can point me to some reference materials that would be useful, that would be wonderful as well.

    Thanks so much,

    JillE

    Wednesday, October 9, 2013 2:18 PM
  • Hi Jill,

    That's different behavior than I get. I tested it again on both 2007 and 2010.

    Try this command.

        CreateObject("shell.application").MinimizeAll
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Wednesday, October 9, 2013 4:12 PM
  • Hi Rich,

    OK, so probably my problem is that I'm unclear on how to position the code you've provided relative to the code I already have:

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            vFileName _
            , 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

    Can you help me understand this?

    Thanks,

    JillE

    Wednesday, October 9, 2013 5:48 PM
  • Hi Jill,

    The issue appears to be timing. A delay factor is needed in order to allow the export to PDF and opening of the file to complete. Here is code that worked in my test environment.

    Sub minPDF()
        Dim vFileName As String, shell As Object
        vFileName = "MyPDF"
        
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            vFileName _
            , 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
        Application.OnTime When:=Now + TimeValue("00:00:02"), _
             Name:="DelayedSub"
            
    End Sub
    
    Private Sub DelayedSub()
        Dim shell As Variant
        shell = CreateObject("shell.application").minimizeAll
        MsgBox "I am here"
        Documents(1).ActiveWindow.WindowState = wdWindowStateMaximize
        shell = ""
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, October 10, 2013 3:06 AM
  • Hi Rich,

    Thanks for taking the time to provide me with the code! That makes perfect sense since I know that the code will continue to run concurrently whilethe pdf is being created. In my environment, "Delayed Sub" doesn't appear to be executing as the message box is not appearing. I tried increasing the time delay but that doesn't have an impact either.

    Thoughts? I'm using Word 2010.

    Thanks,

    JillE

    Thursday, October 10, 2013 3:40 PM
  • Hi Jill,

    I tested it on two separate systems, both Win 7 64-bit. One had Office 2007 and the other Office 2010. Both versions of Office are fully updated.

    The DelayedSub executed just fine and the Adobe Reader window was minimized and the Msgbox appeared.

    I'm not sure why it's failing to work in your environment.

    As another approach have you thought about just creating the PDF file and not having it open in an active window?


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, October 10, 2013 4:05 PM
  • Hi Rich,

    Thanks again for your time.

    I am running 32-bit version of Office. Perhaps that's the explanation?

    Thanks,

    JillE

    Thursday, October 10, 2013 6:00 PM
  • Hi Jill,

    Both of my test machines were running 32-bit Office versions.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, October 10, 2013 10:38 PM
  • Hi Rich,

    My Windows version is also 32-bit. (Windows 7 Enterprise, SP1) and I believe you said yours was 64-bit. Would that make the difference?

    Thanks,

    JillE

    Friday, October 11, 2013 1:53 PM