Create Drop Down Menu in PDF

LH_Racing

New Member
Joined
May 17, 2023
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Dear all,

first point: sorry for the grammar, english isn't my native language.
Now my problem: I do have a Excel sheet from which a pdf is created with a macro. There's also a field for signature being added to the pdf. I would like to create 2 drop down menus in the pdf, so u can tell, which rank the person signing has. But I cant find any code to create a drop down menü. Can anyone help me? Thanks in advance. Following is the code I'm currently using.

Public Sub TESTSave_Sheet_As_PDF_Add_2_Signature_Fields()

Dim PDDoc As Object
Dim AVDoc As Object
Dim JSO As Object
Dim formField As Object
Dim inputPDFfile As String, outputPDFfile As String
Dim coords() As Variant

Const TOP_LEFT_X = 51
Const TOP_LEFT_Y = 197
Const WIDTH = 161
Const HEIGHT = 63

With ActiveSheet
.Range("F1").Value = "Created " & Now
inputPDFfile = ThisWorkbook.Path & "\" & .Name & ".pdf"
outputPDFfile = ThisWorkbook.Path & "\" & .Name & " WITH 2 SIGNATURE FIELDS.pdf"
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=inputPDFfile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False


End With






Set PDDoc = CreateObject("AcroExch.PDDoc")
Set AVDoc = CreateObject("AcroExch.AVDoc")

If PDDoc.Open(inputPDFfile) Then

Set JSO = PDDoc.GetJSObject

'The 4th argument to JSO.addField is an array of coordinates specifying the position and size of the signature field's bounding rectangle,
'with origin (0,0) at bottom left of page, in the following order: top-left x, top-left y, bottom-right x and bottom-right y.

'1st signature field

coords = Array(TOP_LEFT_X, TOP_LEFT_Y, TOP_LEFT_X + WIDTH, TOP_LEFT_Y - HEIGHT)
Set formField = JSO.addField("SignatureField1", "signature", 0, coords) '0 = 1st page
formField.StrokeColor = JSO.Color.black 'StrokeColor sets the border and text colours of the field


If PDDoc.Save(1, outputPDFfile) Then
PDDoc.Close
AVDoc.Open outputPDFfile, vbNullString
AVDoc.BringToFront
MsgBox "Created " & outputPDFfile
AVDoc.Close False
Else
MsgBox "Unable to save " & outputPDFfile
End If

End If

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
When posting code, please put it inside VBA code tags.

Here's your code modified to add a combobox with 3 items below the signature field.
VBA Code:
Public Sub Save_Sheet_As_PDF_Add_Signature_and_Combobox_Fields()

    Dim PDDoc As Object
    Dim AVDoc As Object
    Dim JSO As Object
    Dim formField As Object
    Dim inputPDFfile As String, outputPDFfile As String
    Dim coords() As Variant
    Dim comboboxItems(0 To 2) As String
   
    comboboxItems(0) = "AAA"
    comboboxItems(1) = "BBB"
    comboboxItems(2) = "CCC"
   
    Const TOP_LEFT_X = 51
    Const TOP_LEFT_Y = 197
    Const WIDTH = 161
    Const HEIGHT = 63
    Const COMBOBOX_HEIGHT = 20
   
    With ActiveSheet
        .Range("F1").Value = "Created " & Now
        inputPDFfile = ThisWorkbook.Path & "\" & .Name & ".pdf"
        outputPDFfile = ThisWorkbook.Path & "\" & .Name & " WITH SIGNATURE AND COMBOBOX FIELDS.pdf"
        .ExportAsFixedFormat Type:=xlTypePDF, FileName:=inputPDFfile, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With
   
    Set PDDoc = CreateObject("AcroExch.PDDoc")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
   
    If PDDoc.Open(inputPDFfile) Then
   
        Set JSO = PDDoc.GetJSObject
       
        'The 4th argument to JSO.addField is an array of coordinates specifying the position and size of the signature field's bounding rectangle,
        'with origin (0,0) at bottom left of page, in the following order: top-left x, top-left y, bottom-right x and bottom-right y.
       
        'Add signature field
       
        coords = Array(TOP_LEFT_X, TOP_LEFT_Y, TOP_LEFT_X + WIDTH, TOP_LEFT_Y - HEIGHT)
        Set formField = JSO.addField("Signature1", "signature", 0, coords) '0 = 1st page
        formField.StrokeColor = JSO.Color.black 'StrokeColor sets the border colour of the field with a line as large as the line width
       
        'Add combobox field
       
        coords = Array(TOP_LEFT_X, TOP_LEFT_Y - HEIGHT - 10, TOP_LEFT_X + WIDTH, TOP_LEFT_Y - HEIGHT - 10 - COMBOBOX_HEIGHT)
        Set formField = JSO.addField("Combobox1", "combobox", 0, coords)   '0 = 1st page
        With formField
            .StrokeColor = JSO.Color.black
            .textSize = 10
            .textFont = "Calibri"
            On Error Resume Next 'Prevent Run-time error 1001
            .setItems comboboxItems
            On Error GoTo 0
        End With
       
        If PDDoc.Save(1, outputPDFfile) Then
            PDDoc.Close
            AVDoc.Open outputPDFfile, vbNullString
            AVDoc.BringToFront
            MsgBox "Created " & outputPDFfile
            AVDoc.Close False
        Else
            MsgBox "Unable to save " & outputPDFfile
        End If
   
    End If

End Sub
Note that I needed On Error Resume Next to prevent "Run-time error '1001' at the .setItems comboboxItems line.
 
Upvote 1
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top