none
VSTO - Saving a Word doc as a PDF RRS feed

  • Question

  • My client has a 3rd party application ( ie. I am not able to access or alter the source )  which creates word documents using merge tags which can then be edited in Word, or sent directly to an email address.

    However they need extra formatting done on the document after the tags have been merged. I have developed a word addin which further processes the word document on the ActivateWindow and BeforeClose events.  This works great providing the document is sent in  native word format.

    However, in the application they can choose to send the files as PDF. In this case, the BeforeClose event (and the BeforeSave event - which I thought I might be able to use instead) are not fired.  My question is, when the application saves the word document programmatically as a PDF, are there any events I can trap in my addin which will allow me to manipulate the word document BEFORE its converted to PDF?




    • Edited by BuntyEllis Tuesday, March 18, 2014 5:10 PM
    Tuesday, March 18, 2014 5:02 PM

Answers

  • Hello,

    The Word object model doesn't provide any specific events. However, you can repurpose the standard button for saving documents and display your own dialog. But in that case you have to save all documents programmatically. I.e. you will be responsible for saving documents.

    P.S. I have just tested the described scenario on my machine with Word 2013 SP1 installed. I always get the DocumentBeforeSave event fired.

    Tuesday, March 18, 2014 5:58 PM
  • The Word object model doesn't provide any property or methods for this. The event is fired if you use the built-in functionality.

    As you may see the issue is not related to Word at all. I'd recommend contact the add-in developers instead.

    Wednesday, March 19, 2014 4:19 PM

All replies

  • Hello,

    The Word object model doesn't provide any specific events. However, you can repurpose the standard button for saving documents and display your own dialog. But in that case you have to save all documents programmatically. I.e. you will be responsible for saving documents.

    P.S. I have just tested the described scenario on my machine with Word 2013 SP1 installed. I always get the DocumentBeforeSave event fired.

    Tuesday, March 18, 2014 5:58 PM
  • Thanks for that. Unfortunately the 3rd party code is driving Word behind the scenes. There is no possibility of interacting with it directly so I am reliant on  trapping events, of which there are several defined...and a couple I am already trapping successfully.

    What is happening is as follows

    1. The  3rd party app opens a word doc programmatically and populates the merge tags ( no interface ) 

    2. I intercept the BeforeClose event in my VSTO office addin and carry out some further processing

    3. The app then continues to save the document

    This works fine for .doc but not working for .pdf

    As soon as the application settings are changed to send PDFs instead of Docs, I no longer receive the BeforeClose event . Incidentally this is also the case if I do actually run Word itself and choose "Save as PDF" -  ie. no BeforeClose event fires - but it does for "Save" and "Save As".

    Does anyone know how I can intercept a word doc which is being saved as PDF programmatically?


    • Edited by BuntyEllis Wednesday, March 19, 2014 10:15 AM
    Wednesday, March 19, 2014 10:05 AM
  • As I already wrote, the DocumentBeforeSave event is fired. What Word version do you have installed on the machine? Is it up to date?

    Did you try to turn off all other add-ins installed for Word? Does it help? Do you get the event fired now?

    Wednesday, March 19, 2014 3:07 PM
  • Thanks again. I am now trapping the DocumentBeforeSave event and trying to process the file on that basis.

    This event gets fired if you actually run word and select "Save As...". However it doesn't get called again once you select a file type of PDF and press the Save button on the dialog.

    My third party app is not triggering the DocumentBeforeSave event at all so I was guessing the api allowed them to "save as pdf" directly without raising the first save event. However, having looked at the processes running I am now no longer sure whats going on :( -  as Word appears to no longer be getting fired up in the background. 

    I have now asked the software supplier to confirm whats actually happening in their code and whether this has recently changed. Will keep you posted.

    Unfortunately it is not possible for me to turn off the only other add in which is installed...

    Wednesday, March 19, 2014 3:23 PM
  • The Word object model doesn't provide any property or methods for this. The event is fired if you use the built-in functionality.

    As you may see the issue is not related to Word at all. I'd recommend contact the add-in developers instead.

    Wednesday, March 19, 2014 4:19 PM