Results 1 to 7 of 7
  1. #1
    Darth_Elicious is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2017
    Posts
    11

    Convert VBA script from Excel to Access Table

    Hello all,

    I have found a very useful script online for filling an existing PDF from an Excel Table. It works for the most part from Excel, but it would be even better if I could make it work straight from the Access Database I produce the excel table from. Here is the VBA Script I have:

    Sub WritePDFForms()

    'Declaring the necessary variables.


    Dim strPDFPath As String
    Dim strFieldNames(1 To 154) As String
    Dim i As Long
    Dim j As Integer
    Dim LastRow As Long
    Dim objAcroApp As Object
    Dim objAcroAVDoc As Object
    Dim objAcroPDDoc As Object
    Dim objJSO As Object
    Dim strPDFOutPath As String
    Dim shMain As Worksheet

    strPDFPath = ThisWorkbook.Path & "" & "Unlocked Structure Form.pdf"
    Set shMain = Sheets("Main")

    'Set the required field names in the PDF form.
    strFieldNames(1) = "FormData[0].Page1[0].CRtype[0]"
    strFieldNames(2) = "FormData[0].Page1[0].Original"
    strFieldNames(3) = "FormData[0].Page1[0].FieldDate[0]"
    strFieldNames(4) = "FormData[0].Page1[0].FormDate[0]"
    strFieldNames(5) = "FormData[0].Page1[0].RecorderNo[0]"
    strFieldNames(6) = "FormData[0].Page1[0].Sitename[0]"
    strFieldNames(7) = "FormData[0].Page1[0].ProjectName[0]"
    strFieldNames(8) = "FormData[0].Page1[0].MultList[0]"
    strFieldNames(9) = "FormData[0].Page1[0].SurveyNo[0]"
    strFieldNames(10) = "FormData[0].Page1[0].NRcategory[0]"

    'Find the last row of data in sheet Write.
    With shMain
    .Activate
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With

    'Loop through all rows of sheet Write and use the data to fill the PDF form.

    For i = 3 To LastRow

    On Error Resume Next

    'Initialize Acrobat by creating the App object.
    Set objAcroApp = CreateObject("AcroExch.App")


    'Create the AVDoc object.
    Set objAcroAVDoc = CreateObject("AcroExch.AVDoc")

    On Error GoTo 0

    'Open the PDF file.
    If objAcroAVDoc.Open(strPDFPath, "") = True Then

    'Set the PDDoc object.
    Set objAcroPDDoc = objAcroAVDoc.GetPDDoc

    'Set the JS Object - Java Script Object.
    Set objJSO = objAcroPDDoc.GetJSObject

    On Error Resume Next

    'Fill the form fields.
    For j = 1 To 154

    objJSO.GetField(strFieldNames(j)).Value = CStr(shMain.Cells(i, j + 1).Value)

    Next j

    On Error GoTo 0

    With shMain
    strPDFOutPath = "C:\Users\dcothran\Desktop\New folder\Test.pdf"
    End With

    'Save the form as new PDF file.
    objAcroPDDoc.Save 1, strPDFOutPath

    'Close the form without saving the changes.
    objAcroAVDoc.Close True

    'Close the Acrobat application.
    objAcroApp.Exit

    'Release the objects.
    Set objJSO = Nothing
    Set objAcroPDDoc = Nothing
    Set objAcroAVDoc = Nothing
    Set objAcroApp = Nothing

    Else

    MsgBox "Could not open the file!", vbCritical, "File error"

    'Close the Acrobat application.
    objAcroApp.Exit

    'Release the objects and exit.
    Set objAcroAVDoc = Nothing
    Set objAcroApp = Nothing
    Exit Sub

    End If

    Next i

    'Enable the screen.
    Application.ScreenUpdating = True

    'Inform the user that forms were filled.
    MsgBox "All forms were created successfully!", vbInformation, "Finished"

    End Sub

    Is there any way to convert this to work for Access? I found the original script here: http://www.myengineeringworld.net/20...excel-vba.html

    Also, the only thing it currently does not do is fill in check boxes or radio buttons. The buttons are in exclusion groups with YES and NO values assigned, but when I import the data into the form, the boxes are not checked. Any ideas why? This is more of a PDF question, but I have had such good luck here I thought I'd ask

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Normally you create Access report and export it to PDF format. So this is an Online PDF Form and you want Access to fill in the values on that PDF online form?

  3. #3
    Darth_Elicious is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2017
    Posts
    11
    Not exactly. This is a PDF form issued to us by the State that we have to fill out for each resource we survey. It has a number of text fields for descriptions we have to fill in.

  4. #4
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Options would be to create the form in Access as a Report and fill in the data that way, then export the report to PDF so it looks like the other one. Or create it in Word and do mail merge. Or use your code if it works. Might be simpler ways that others know.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    this issue has been covered in this forum a few times; you might try searching using some sort of terms including "pdf". After one long involvement recently, creating a report is exactly where it ended up.

    Please consider using code tags around your code in the future...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Forum won't search on 3-letter words.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Ok, here's one for starters
    https://www.accessforums.net/showthread.php?t=65290
    Other than that, I hope the OP can figure out some ways to expand the search string.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Convert text to number in linked table to excel....
    By ideasfactory in forum Queries
    Replies: 3
    Last Post: 05-24-2015, 05:17 PM
  2. Replies: 5
    Last Post: 03-16-2015, 10:17 PM
  3. Replies: 3
    Last Post: 07-29-2014, 12:11 AM
  4. Replies: 1
    Last Post: 01-09-2013, 04:11 PM
  5. Convert Excel to Access
    By wkenddad in forum Database Design
    Replies: 1
    Last Post: 04-19-2012, 01:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums