How to manage PDF controls using VBA in ms access

We could open up an existing PDF file into MS access through VBA code and also can manage its controls. This article provides solution for managing PDF controls in MS access through the VBA code. Here's an example is describe the procedure to accomplish this task.

First, we have created a form in access with a button as shown in Fig: -1.1, which will open an existing PDF file whose path is given in the code.

How to manage PDF controls using VBA in ms access Fig-1.1

Fig-1.1

The code given below is the code to hide the "Navigation Pane" and some other controls in the opened PDF.

Private Sub cmdOpenPDF_Click()
Call OpenPDF ("Path of your PDF file", , , , , , , , 1)
End Sub

It will open up the PDF file with no "Navigation Bar" and other controls, shown in Fig: -1.2.

How to manage PDF controls using VBA in ms access Fig-1.2

Fig-1.2

In the same way the code given below is to unhide these controls.

Private Sub cmdOpenPDF_Click()
Call OpenPDF ("Path of your PDF file", , , , , , , , 0)
End Sub

It will open up the PDF file with "Navigation Bar" and other controls, shown in Fig: -1.3.

How to manage PDF controls using VBA in ms access Fig-1.3

Fig-1.3

To execute the code Given above to hide and unhide the controls you must have to create a module with the code given below:-

' strFileName : Fully qualified pathe and filename, including extension, of the PDF to open.
' page : Page number to open the document at
' zoom : Numerical value representing a zoom factor; 100=100%, 65=65%, ...
' pagemode : Displays bookmarks or thumbnails; bookmarks, thumbs, none
' scrollbar : Turns scrollbars on or off; 1=Turn on, 0=Turn off
' toolbar : Turns the toolbar on or off; 1=Turn on, 0=Turn off
' statusbar : Turns the status bar on or off; 1=Turn on, 0=Turn off
' messages : Turns the document message bar on or off; 1=Turn on, 0=Turn off
' navpanes : Turns the navigation panes and tabs on or off; 1=Turn on, 0=Turn off

Public Function OpenPDF(strFileName As String, Optional page, Optional zoom, Optional pagemode, Optional scrollbar, Optional toolbar, Optional statusbar, Optional messages, Optional navpanes)

On Error GoTo Err_Handler
Dim ShellObj As Object
Dim strAcrobatPath As String
Dim strParameter As String
Dim strCmd As String

Set ShellObj = CreateObject("Wscript.Shell")
strAcrobatPath = ShellObj.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe\")

If Not IsMissing(page) Then
If Len(strParameter) = 0 Then
strParameter = "page=" & page
Else
strParameter = strParameter & "&" & "page=" & page
End If
End If

If Not IsMissing(zoom) Then
If Len(strParameter) = 0 Then
strParameter = "zoom=" & zoom
Else
strParameter = strParameter & "&" & "zoom=" & zoom
End If
End If

If Not IsMissing(pagemode) Then
If Len(strParameter) = 0 Then
strParameter = "pagemode=" & pagemode
Else
strParameter = strParameter & "&" & "pagemode=" & pagemode
End If
End If

If Not IsMissing(scrollbar) Then
If Len(strParameter) = 0 Then
strParameter = "scrollbar=" & scrollbar
Else
strParameter = strParameter & "&" & "scrollbar=" & scrollbar
End If
End If

If Not IsMissing(toolbar) Then
If Len(strParameter) = 0 Then
strParameter = "toolbar=" & toolbar
Else
strParameter = strParameter & "&" & "toolbar=" & toolbar
End If
End If

If Not IsMissing(statusbar) Then
If Len(strParameter) = 0 Then
strParameter = "statusbar=" & statusbar
Else
strParameter = strParameter & "&" & "statusbar=" & statusbar
End If
End If

If Not IsMissing(messages) Then
If Len(strParameter) = 0 Then
strParameter = "messages=" & messages
Else
strParameter = strParameter & "&" & "messages=" & messages
End If
End If

If Not IsMissing(navpanes) Then
If Len(strParameter) = 0 Then
strParameter = "navpanes=" & navpanes
Else
strParameter = strParameter & "&" & "navpanes=" & navpanes
End If
End If

If Len(strParameter) = 0 Then
Shell strAcrobatPath & " " & Chr(34) & strFileName & Chr(34), vbNormalFocus
Else
strCmd = strAcrobatPath & " /A " & Chr(34) & strParameter & Chr(34) & " " & Chr(34) & strFileName & Chr(34)
Shell strCmd, vbNormalFocus
End If

Err_Handler_Exit:
On Error Resume Next
Set ShellObj = Nothing
Exit Function

Err_Handler:
MsgBox Err.Number & ":" & Err.Description
Resume Err_Handler_Exit
End Function


DISCLAIMER

It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. Access Guru will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.