Skip to main content
Participant
February 14, 2020
Answered

Pasting an Image and Repositioning it with VBA

  • February 14, 2020
  • 1 reply
  • 1875 views

I have a problem that I've been stuck on all day and I would appreciate any help.

 I have a vba macro that I've written in excel that opens a pdf in Acrobat and pastes the contents of a cell into the PDF as an image.

My problem is at that point I want to be able to move the image to a different position within the PDF page.  I can select the annotation using like so:

 

PDFPage = doc.AquirePage(PDFview.GetPageNum)
Set annot = PDFPage.GetAnnot(0)

 

However, there doesn't appear to be any settings that can be used with this variable to determine position.

I cannot use the function jso.getAnnot to set a variable because I do not know the name of the annotation.  I have attempted to use the function jso.getAnnots() like so:

 

Private AcroApp As Acrobat.CAcroApp
Private doc As Acrobat.CAcroPDDoc
Dim jso As Object
Dim Annotations() As Variant

Set AcroApp = CreateObject("AcroExch.App")
Set doc = CreateObject("AcroExch.PDDoc")
Set AVDoc = CreateObject("AcroExch.AVDoc")
If AVDoc.Open(sPath, "") = True Then
  Set doc = AVDoc.GetPDDoc
End If
AcroApp.Show
ThisWorkbook.Sheets("table").Range("R1").Copy
AcroApp.MenuItemExecute("Paste")

Set jso = doc.GetJSObject
Annotations = jso.getAnnots()
debug.print UBound(Annotations)

 

  However, my debug print is returning a length of zero for Annotations, so I've clearly done something wrong there.

 

I would be grateful for any help.

This topic has been closed for replies.
Correct answer Thom Parker

For the JSO: Using JavaScript from VBA is problematic in particular for objects. Because the JavaScript objects are not really compatible with VB objects. Especially for objects connected to the Acrobat DOM, such as annotations.  If you want to go this route it is better to write folder level JavaScript functions to do the heavy lifting so no complex operations or objects are handled on the VB side.  

 

However, the PDAnnot object in VB has both setRect and getRect functions, so you already have a way to set the stamp position. 

1 reply

jonstoreyAuthor
Participant
February 18, 2020

Is there anyone who is able to help me with this?

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 19, 2020

For the JSO: Using JavaScript from VBA is problematic in particular for objects. Because the JavaScript objects are not really compatible with VB objects. Especially for objects connected to the Acrobat DOM, such as annotations.  If you want to go this route it is better to write folder level JavaScript functions to do the heavy lifting so no complex operations or objects are handled on the VB side.  

 

However, the PDAnnot object in VB has both setRect and getRect functions, so you already have a way to set the stamp position. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jonstoreyAuthor
Participant
February 21, 2020

Thank you very much for your help.  I was able to use setRect to meet my needs.