Question

I try to save a Word 2010 document to PDF and then want to do something with the file(name) that the user selected for this purpose in the corresponding dialog. Also, I would like to make a few preconfigurations of dialog settings. However, the following macro (in Microsoft Visual basic for Aplications 7.0, i.e. the stuff you get when simply recording and then modifying macros) does not work:

Sub MyMacro()
  dim retval as long
  dim DidTheExportToPdfActuallyTakePlaceSuccessfully as Boolean
  dim WhatWasThePdfFilenameTheUserChoseFinally as String

  With Dialogs(wdDialogExportAsFixedFormat)
    .ExportFormat = wdExportFormatPDF
    .OpenAfterExport = True
    .OptimizeFor = wdExportOptimizeForPrint
    .Range = wdExportAllDocument
    .Item = wdExportDocumentContent
    .IncludeDocProps = True
    .KeepIRM = True
    .CreateBookmarks = wdExportCreateNoBookmarks
    .DocStructureTags = True
    .BitmapMissingFonts = True
    .UseISO19005_1 = False

    retval = .Show()

    ' DidTheExportToPdfActuallyTakePlaceSuccessfully = ???
    ' WhatWasThePdfFilenameTheUserChoseFinally = ???
  end with
end sub

First of all, I get errors in all the lines .ExportFormat etc. It appears these problems are caused by late binding. To circumvent this problem I tried to add option strict off on top, but that immediately produces a compilation error ("Base or Compare or Explicit or Private expected"). I also read about reflection, but it seems that things necessary for that according to online findings (such as dim x as System.Type or y.gettype()) don't compile either ...

If I simply remove the offending lines, the dialog shows successfully and the pdf export takes places successfully. However, it seems that retval is always 0, no matter if the file is actually exported or the user hit cancel. Not to mention that extracting the actual pdf filename fails in the same way as does pre-filling the dialog options.

I'm a bit at a loss because all googling and searching through MS online help tends to take me only to almost compatible situations (especially, nothing found compiles, see above). What is the right way to achieve my goal?

Was it helpful?

Solution

Broadly speaking, only the older builtin dialogs in Word support "late bound" named parameters. Since around Word 2000, most (if not all) of them only recognise the early bound members of the Dialog class.

Possible workarounds include

  • try to use Sendkeys (IMO always difficult to get right at the best of times)
  • try to control the Dialog window via WIN32+Windows messages (no idea on feasibility there)
  • adapt one of the standard Application FileDialogs as best you can (in this case, probably Application.FileDialog(msoFileDialogSaveAs) ), then follow up with your own UserDialog that prompts for any other options that you need, then call ExportAsFixedFormat to do the work.
  • (+variations on (c), e.g. do some of it in .NET with a WIndows form)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top