joshuar
New member
- Local time
- Today, 16:11
- Joined
- Dec 15, 2020
- Messages
- 9
This thread is a list of a few VBA tricks to getting information from an Access database to a PDF form.
I wish I knew how to do this stuff about a decade ago for a big project I was working on, but the resources on how to do it were pretty limited at the time; I had to settle for Access Report formats with the forms little more than pictures in the background- painful. Resources are still somewhat limited, so below are a few consolidated cliff notes on how to do it. If it wasn’t for a few more recent postings from Karl Heinz Kremer on the subject, I’d likely still be producing forms with Access reports. Big thanks to him.
There are 2 attached documents:
Opening the form: With Acrobat, I can open the form, have VBA adjust stuff in it and see the changes as they happen. If the PDF document is open in Adobe Reader, this doesn't happen and it doesn't really change anything in the document. If Adobe Reader is the default application for PDFs, I wait till the VBA is done making the adjustments in the form prior to opening the form. That said, I use the code below to do check if PDFs open with Acrobat by default:
This code is used before the section of VBA that modifies the form. strFilePath is a string variable with the path to the PDF form:
This code is used after the section to open the form if Acrobat is not the default program
Editing the form: Below is the code necessary for VBA to open the form (usually in the background) and start editing it
Closing the form: Below is code that closes the vba editing session, usually at the bottom of a function:
Editing a text field value: This edits the value of the text field named "Demo Text Field". CStr ensures any string passed to the text field is indeed a string. It tends to error out when not passed a string. Nz sets the value if the variable passed is NULL; CStr doesn't like working with null values, so using both of these is a good habit
Editing a check box value: Check boxes don't work with TRUE or FALSE values through VBA, it uses the string value of "Yes" or "No". If that doesn't work, use "On" or "Off" (for some reason that works in a different form produced in a different way).
Page Templates: Page Templates are pages within the PDF that can be cloned or copied. Generally, page templates should be a hidden page from the user so the user doesn't fill them template with data and end up cloning the same data to multiple pages. By default, the "Page Templates" menu is also hidden in Acrobat. To get to it, go to 'Tools' - (dropdown for more) - Check ' Document Processing' then under ' Document Processing' select 'Page Templates'.
To create a template, highlight the page you want to convert to a template under "Page Thumbnails", and under the "Page Templates" name it then "Add".
Unchecking the eyeball will hide the template page from the user. You'll have to recheck it IOT edit the templates again later on if needed.
I wish I knew how to do this stuff about a decade ago for a big project I was working on, but the resources on how to do it were pretty limited at the time; I had to settle for Access Report formats with the forms little more than pictures in the background- painful. Resources are still somewhat limited, so below are a few consolidated cliff notes on how to do it. If it wasn’t for a few more recent postings from Karl Heinz Kremer on the subject, I’d likely still be producing forms with Access reports. Big thanks to him.
There are 2 attached documents:
- LockAfterSigning.js(.txt) is a script that must be located in the javascript folder of Acrobat. This allows VBA to set lock (setLock) fields for a signature block. The javascript used to do this is a privileged method that can’t be called directly from VBA. Remove the .txt extension to use. The file should be placed in a directory similar to below:
C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Javascripts\
- Demo Adobe Forms.accdb has two forms in it:
- simpleForm produces an example form and each button does one of the functions listed below to alter the form or fill it
- Make2062 has a button that puts it all together to produce a pretty lengthy inventory list with digital signatures (a DA2062) from dummy data in tbl2062Data.
Opening the form: With Acrobat, I can open the form, have VBA adjust stuff in it and see the changes as they happen. If the PDF document is open in Adobe Reader, this doesn't happen and it doesn't really change anything in the document. If Adobe Reader is the default application for PDFs, I wait till the VBA is done making the adjustments in the form prior to opening the form. That said, I use the code below to do check if PDFs open with Acrobat by default:
Code:
'Check if Acrobat is default and open form if it is
Private Function checkForAcrobat(ByVal sFile$)
strFileAssociation = GetFileAssociation(sFile)
If strFileAssociation Like "*Acrobat.exe" Then
checkForAcrobat = True
Else
checkForAcrobat = False
End If
End Function
Private Function GetFileAssociation$(ByVal sFile$)
GetFileAssociation = "File not found !"
If Dir(sFile) = "" Or sFile = "" Then Exit Function
GetFileAssociation = "No association found !"
Dim i&, E$: E = String(260, Chr$(0))
i = FindExecutable(sFile, vbNullString, E)
If i > 32 Then GetFileAssociation = Left$(E, InStr(E, Chr$(0)) - 1)
End Function
This code is used before the section of VBA that modifies the form. strFilePath is a string variable with the path to the PDF form:
Code:
strFilePath = "C:\PathToPDF\MyForm.pdf"
usesAcrobat = checkForAcrobat(strFilePath)
If usesAcrobat Then
VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch the file
End If
This code is used after the section to open the form if Acrobat is not the default program
Code:
If Not (usesAcrobat) Then
VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch the file
End If
Editing the form: Below is the code necessary for VBA to open the form (usually in the background) and start editing it
Code:
Dim AcroApp As Acrobat.CAcroApp 'Used to open Acrobat App
Dim theForm As Acrobat.CAcroPDDoc 'Used for opening the form
Dim jso As Object 'Javascript object for the PDF form
Dim fldTextField As Object 'Used for the text field in the PDF document
Set AcroApp = CreateObject("AcroExch.App")
Set theForm = CreateObject("AcroExch.PDDoc")
theForm.Open (strFilePath) 'Opens the document for editing in the background
Closing the form: Below is code that closes the vba editing session, usually at the bottom of a function:
Code:
theForm.Save PDSaveFull, strFilePath
theForm.Close
AcroApp.Exit
Set AcroApp = Nothing
Set theForm = Nothing
Editing a text field value: This edits the value of the text field named "Demo Text Field". CStr ensures any string passed to the text field is indeed a string. It tends to error out when not passed a string. Nz sets the value if the variable passed is NULL; CStr doesn't like working with null values, so using both of these is a good habit
Code:
Dim fldTextField As Object
Set fldTextField = jso.getField("Demo Text Field")
strText = "Some Text to be placed in a field"
'Set the value of the text
fldTextField.Value = CStr(Nz(strText, ""))
Editing a check box value: Check boxes don't work with TRUE or FALSE values through VBA, it uses the string value of "Yes" or "No". If that doesn't work, use "On" or "Off" (for some reason that works in a different form produced in a different way).
Code:
Set chkCheckblock = jso.getField("Demo Check Field")
check = TRUE
'Set the value of the check block. "On" is used for true and "Off" for false
chkCheckblock.Value = IIf(check, "Yes", "No")
Page Templates: Page Templates are pages within the PDF that can be cloned or copied. Generally, page templates should be a hidden page from the user so the user doesn't fill them template with data and end up cloning the same data to multiple pages. By default, the "Page Templates" menu is also hidden in Acrobat. To get to it, go to 'Tools' - (dropdown for more) - Check ' Document Processing' then under ' Document Processing' select 'Page Templates'.
To create a template, highlight the page you want to convert to a template under "Page Thumbnails", and under the "Page Templates" name it then "Add".
Unchecking the eyeball will hide the template page from the user. You'll have to recheck it IOT edit the templates again later on if needed.
Attachments
Last edited: