Skip to main content
Aug 16, 2019 10:26 AM UTC
Question

Excel VBA - unable to attach XLS file to PDF

I have the folowing VBA code that successfully creates two signature fields in PDF and then stops with Object Required error at Set oAttachment = oJS.ImportDataObject(Selected_File). Any ideas how to make it work?

===========================================================================================================

Sub Prepare_PDF()

On Error GoTo Err_Handler

Dim pdfPDDoc As New AcroPDDoc, oJS As Object, oFields, oAttachment As Object

Dim strFName As String

Dim oParam As Parameter

   

strFName = "C:\Main.pdf"

'------- Select XLS file to embed ----------

With Application.FileDialog(msoFileDialogFilePicker)

.AllowMultiSelect = False

.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1

.Show

Selected_File = .SelectedItems.Item(1)

End With

   

'------- Add signature fields to PDF file----------

If pdfPDDoc.Open(strFName) Then

Set oJS = pdfPDDoc.GetJSObject

Set oFields = oJS.AddField("SignatureField1", "signature", 0, Array(200, 620, 450, 670))

Set oFields = oJS.AddField("SignatureField2", "signature", 0, Array(200, 520, 450, 570))

'------- Embed XLS in PDF file----------

Set oAttachment = oJS.ImportDataObject(Selected_File)

'------- Save PDF file------------------

strFName = Left(strFName, Len(strFName) - 4) & "-signed.pdf"

pdfPDDoc.Save 1, strFName

End If

Exit_Proc:

    Exit Sub

       

Err_Handler:

    MsgBox "In test" & vbCrLf & Err.Number & "--" & Err.Description

    Resume Exit_Proc

End Sub

This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

Participant
Oct 17, 2023 12:04 PM UTC

I know, this is an historical thread, but  the the code will become workable replacing 

Set oAttachment = oJS.ImportDataObject(Selected_File)

with

    Dim testAnno As Object, props As Object
    Set testAnno = oJS.AddAnnot
    Set props = testAnno.getProps
    props.Type = "FileAttachment"

    oJS.importDataObject props, Selected_File
Translate
try67
Community Expert
Community Expert
Aug 16, 2019 10:31 AM UTC

You have to specify a name for the data object as the first parameter.

Translate
Aug 16, 2019 10:34 AM UTC

I am trying Set oAttachment = oJS.ImportDataObject("Attachment", Selected_File), still generates Object Required error.

Translate
Karl Heinz  Kremer
Community Expert
Community Expert
Aug 16, 2019 11:02 AM UTC

In my experience, you may have to provide all optional parameters for some func in the JSO to work. 

Translate