Search code examples
excelvbapdfsave

VBA: SendKey Saving a PDF


I am trying to open, enter data, save, and close a PDF Form without the use of Acrobat. I have figured out how to open, and enter data, but am having trouble figuring out how to save. I would like to be able to navigate using SendKeys to change the path and name of the file, but can't figure out how to get to either of them.enter image description here

Code:

        Application.SendKeys "+^(s)", True 'Saves
        'Application.Wait Now + 0.00001
        Application.SendKeys "New File Name", 'Attempts to send new file name but doesn't.
        'Application.Wait Now + 0.00001

        Application.SendKeys "^(q)", True 'closes the PDF reader.

When I "Save As" manually, the "File Name" is highlighted, which made me think I could just SendKeys a new file name and then SendKeys an Enter to save, but the new file name never gets to where it's supposed to go.


Solution

  • First, there seems to be a typo in the line Application.SendKeys "New File Name",. Either you meant Application.SendKeys "New File Name", True or there is an extra comma at the end.

    After typing the name you would normally have to press the save button or press enter to submit the saving dialog. If you quit the PDF application before that, it won't get submitted and the file won't be saved.

    To do that you can add Application.SendKeys "~", True after the line where you type the file name (or Application.SendKeys "{ENTER}", True which is equivalent to press Enter on the numeric keypad).

    Side note: I would advise not to use SendKeys if you can find another way to make this work. SendKeys isn't reliable and sometimes keys won't get registered properly, so it's to use with caution.