Home Case Studies MS Office Tips and Tricks Sample Code Helpful Videos Custom Development  Custom Training Join Us Contact Us
Search Our Site:  

Adobe Acrobat/MS Access VBA Code

I Get It! Development uses Adobe Acrobat® to produce newsletters, classroom materials, and most importantly invoices! The portable document format (PDF) is the de facto standard for sharing documents on the web. You can download Acrobat Reader for free, but in order to produce PDF files, you must purchase the full version of Acrobat. You must also set a reference to the Adobe library in the VB Editor before attempting to program. See the movie showing how to set the reference in Windows Media format or RealOne format for more information.

Once you have Acrobat installed, you can use VBA to control it - print to file, open documents and edit them. You can rearrange pages and insert parts of one document into another document.



A Word From Our Sponsor

Jos. A. Bank is The Expert in Men's Apparel
Jos. A. Bank is The Expert in Men's Apparel


I Get It! Development uses the code below to document database applications which use Access as the front end. Unfortunately, Access VB Editor does not print out forms like most other VB Editors. The forms must be captured using Print Screen or more easily by using the SnagIt VBA as the code on the SnagIt VBA page shows.

The form code is printed directly from the VB Editor into a PDF file into a standard directory.

Then the screen captures are inserted one at a time either directly in front of their associated code, or for forms without modules, at the end of the documentation.

If you'd like to download a sample database, try this Inventory Control Example.zip

Sub InsertFormCaptures()
'---------------------- Start module ------
'I Get It! Development provides programming examples
'for illustration only, without warranty either
'expressed or implied, including, but not limited to,
'the implied warranties of merchantability and/or
'fitness for a particular purpose. This article
'assumes that you are familiar with the programming
'language being demonstrated and the tools used to
'create and debug procedures. These examples assume that
'you have licensed copies of all relevant software installed
'on the machine upon which the examples will be run.

'This routine takes preexisting .jpg files (Run
CreateScreenShots first)
'and adds them to a preexisting CodeFile.pdf file (print to PDF from VB Editor)
'The routine assumes that all files are in C:\Documentation. Please
'change this in the following code or create the folder.
'Remember to set a reference to Adobe Acrobat Type Library in
'Tools > References in the VB Editor
'------ Written by Theo Callahan 4/2003 -------------

Const DOC_FOLDER As String = "C:\Documentation"

Dim objCurrent As AccessObject
Dim frmCurrent As Form
Dim strFormName As String
Dim blnNeedToClose As Boolean

Dim Acroapp As CAcroApp
Dim avCodeFile As CAcroAVDoc
Dim avFormCapture As CAcroAVDoc
Dim pdCodeFile As CAcroPDDoc
Dim pdFormCapture As CAcroPDDoc
Dim lngPage As Long
Dim AVPage As CAcroAVPageView
Dim PDPage As CAcroPDPage

'Start Acrobat in the background
Set Acroapp = CreateObject("AcroExch.App")

'Uncomment the following line if you want to watch the program run
Acroapp.Show

Set avCodeFile = CreateObject("AcroExch.AVDoc") 'This is the code file
Set avFormCapture = CreateObject("AcroExch.AVDoc") 'This will be each jpg in turn

'Open the already created code file
avCodeFile.Open DOC_FOLDER & "\CodeFile.pdf", "Code File"
Set pdCodeFile = avCodeFile.GetPDDoc

'Loop through each form. We have to use the AllForms collection because Forms
'only shows open forms. In order to document all of them, we loop through
'the AllForms collection and open forms as needed. They have to be open for
'us to check whether or not they have modules(associated code)

For Each objCurrent In CurrentProject.AllForms

'Open the form if it's not already open
If Not objCurrent.IsLoaded Then
blnNeedToClose = True 'This reminds us to close the form when done
DoCmd.OpenForm objCurrent.Name, acDesign, , , acFormPropertySettings, acWindowNormal
Set frmCurrent = Application.Screen.ActiveForm
strFormName = frmCurrent.Name
Else
blnNeedToClose = False
Set frmCurrent = Forms(objCurrent.Name)
strFormName = frmCurrent.Name
End If

'Open the jpg file
avFormCapture.Open DOC_FOLDER & "\" & strFormName & ".jpg", ""
Set pdFormCapture = avFormCapture.GetPDDoc

If frmCurrent.HasModule Then 'if there's code, look for the right spot

'Look for the form name and ' - 1' in the code file: that's page 1 of code
avCodeFile.FindText "Form_" & strFormName & " - 1", 0, 0, 1
Set AVPage = avCodeFile.GetAVPageView

'Go to the page just before the form's code
lngPage = AVPage.GetPageNum - 1 'we want page before
If lngPage < 0 Then lngPage = 0

Else
'If there's no code, throw the form in the back of the package
lngPage = pdCodeFile.GetNumPages - 1
End If

'Insert the jpg at the right page
pdCodeFile.InsertPages lngPage, pdFormCapture, 0, 1, 0

'Unfortunately, there is no page 0 so the first form comes AFTER the first
'page of code if it's on page 1. We need to swap the image and code if that's the case
If lngPage = 0 Then pdCodeFile.MovePage 1, 0

'Close the jpg file
pdFormCapture.Close
avFormCapture.Close 1
Set pdFormCapture = Nothing

'If we need to, close the form
If blnNeedToClose Then DoCmd.Close acForm, strFormName, acSaveNo

Next objCurrent

'close the doc file now with form captures
pdCodeFile.Close
avCodeFile.Close 0

'Exit Acrobat
Acroapp.Exit
Set objCurrent = Nothing
Set frmCurrent = Nothing
Set Acroapp = Nothing
Set avCodeFile = Nothing
Set pdCodeFile = Nothing
Set avFormCapture = Nothing

End Sub

 

References

Journal of Accountancy Articles

Tweaking The Numbers

Block That Spreadsheet Error

Excel Security Issues


 

Clients

Do you need help with Microsoft Office? Contact us

See how we've helped companies like yours develop effective business processes

Client Success Stories


 

Consultants

I Get It! Consultants set their own hours, work with their own clients, and choose their own projects.

If you're a Microsoft Office guru and would like to explore being a full-time consultant, please see our consultant information pages.