0 votes
in VBA by Beginner (2 points)

The code below automatically fills in data from a spreadsheet in a PDF form. However, when the Save Dialog box is opened and the filename is written in, the code doesn't execute the Save button. Also, it's a crapshoot as to whether the filename will be filled in correctly. How can I ensure the focus is on the Save Dialog Box?
 

Option Explicit

Sub PDFTemplate()
Dim PDFFldr As FileDialog
Set PDFFldr = Application.FileDialog(msoFileDialogFilePicker)
With PDFFldr
    .Title = "Select PDF File to attach"
    .Filters.Add "PDF Type Files", "*.pdf", 1
    If .Show <> -1 Then GoTo NoSelection
    Sheet1.Range("D19").Value = .SelectedItems(1)
End With
NoSelection:
End Sub

Sub SavePDFFolder()
Dim PDFFldr As FileDialog
Set PDFFldr = Application.FileDialog(msoFileDialogFolderPicker)
With PDFFldr
    .Title = "Select a Folder"
    If .Show <> -1 Then GoTo NoSel:
    Sheet1.Range("D22").Value = .SelectedItems(1)
End With
NoSel:
End Sub
 
Sub CreatePDFForms()
'Has to run when the PDF Template is closed or errors propagate
Dim PDFTemplateFile, NewPDFName, SavePDFFolder, LastName As String
Dim ApptDate As Date
Dim CustRow, LastRow As Long

With Sheet1
LastRow = .Range("B9999").End(xlUp).Row 'Last Row
PDFTemplateFile = .Range("D19").Value   'Template File Name
SavePDFFolder = .Range("D22").Value     'Save PDF Folder
ThisWorkbook.FollowHyperlink PDFTemplateFile
Application.Wait Now + 0.00005

For CustRow = 8 To 8  'Last Row
LastName = .Range("B" & CustRow).Value 'Last Name
ApptDate = .Range("D" & CustRow).Value 'Appt Date
Application.SendKeys "{Tab}", True
Application.SendKeys LastName, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True

Application.SendKeys .Range("C" & CustRow).Value, True 'First Name
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True

Application.SendKeys .Range("F" & CustRow).Value, True 'Address
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True

Application.SendKeys .Range("G" & CustRow).Value, True 'City
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True

Application.SendKeys .Range("H" & CustRow).Value, True 'State
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True

Application.SendKeys .Range("I" & CustRow).Value, True 'Zip
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True

Application.SendKeys .Range("J" & CustRow).Value, True 'Email
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True

Application.SendKeys Format(.Range("K" & CustRow).Value, "###-###-####"), True 'Home Phone #
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True

Application.SendKeys "^(p)", True  'Ctrl+P to print PDF
Application.Wait Now + 0.0001
Application.SendKeys "{Enter}", True 'Press Enter to print
Application.Wait Now + 0.03

'Delete file if it already exists
If Dir(SavePDFFolder & "\" & LastName & "_" & Format(ApptDate, "DD_MM_YYYY") & ".pdf") <> Empty Then Kill (SavePDFFolder & "\" & LastName & "_" & Format(ApptDate, "DD_MM_YYYY") & ".pdf")
Application.SendKeys (SavePDFFolder & "\" & LastName & "_" & Format(ApptDate, "DD_MM_YYYY") & ".pdf"), True
Application.Wait Now + 0.01
Application.SendKeys "%(s)", True 'Alt+S to select save
Application.Wait Now + 1

Next CustRow
Application.SendKeys "^(q)", True 'Ctrl+Q to quit Foxit
Application.Wait Now + 0.0001
'Application.SendKeys "%(n)", True 'Select No to Save changes in Template
'Application.Wait Now + 0.00002
'Application.SendKeys "{numlock}%s", True

End With
End Sub

 

1 Answer

0 votes
by Skilled (582 points)
edited by

This is Rev.1 of my previous answer. The VBA to Maximize Window in Left Monitor tutorial illustrates use of the VBA statement SendKeys. You might use that instead of the Excel method Application.SendKeys. The tutorial also illustrates use of the VBA statement AppActivate. Assuming the Save dialog box has a title that begins with "Save" you might try inserting
AppActivate "Save"
before sending keys associated with that dialog.

Since you appear to be using Foxit as your PDF app, you might review their API Reference for Application Communication plus How to use Foxit PDF SDK ActiveX with Visual Basic.

by Beginner (2 points)
Is there a more reliable method to just save the filled forms, rather than print them, without using SendKeys?
by Skilled (582 points)
See Rev.1 of my previous answer.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...