Solved [MS Word VBO] Insert Image to Word page

f04959c

New Member
Hello,

I need to insert an image to a word file.

First I have wrote the macro in VBA to insert image to a specific page in a word file

Code:
Sub InsertImage()
    Dim PicPath As String
    Dim aShape As Shape

    PicPath = "C:\Users\f04959c\Desktop\Capture.PNG"
    ActiveDocument.GoTo(What:=wdGoToPage, Count:=1).Select
    Set aShape = Selection.InlineShapes.AddPicture(FileName:=PicPath, _
      LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
    With aShape
        .Height = 75
        .Width = 100
        .Left = 400
        .Top = 5
   End With
End Sub

Then , I have tried to translate it in a Blue Prism action but it doesn't works.

Code:
Dim doc as Object

doc = GetInstance(Handle).ActiveDocument.GoTo(What:=1, Count:=1)

with doc.InlineShapes.AddPicture(FileName:=PicPath, LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
    .Height = 75
    .Width = 100
    .Left = 400
    .Top = 10
End With

The error shown is always the same even if I change the code

Internal : Could not execute code stage because exception thrown by code stage: Variabile oggetto o variabile del blocco With non impostata

Someone can help me to fix it?
 

Sukesh Kumaru

Active Member
Hello,

Just Copy the Image you want to paste it into word document by sending keystrokes by opening the directory (For Ex: C:/users/Sukesh) where in the Sukesh folder contains only the image copy it to clipboard by using Ctrl+A and Ctrl+C and then open the Word document and paste it from Clipboard.

Good Luck.
 

f04959c

New Member
Hello,

Just Copy the Image you want to paste it into word document by sending keystrokes by opening the directory (For Ex: C:/users/Sukesh) where in the Sukesh folder contains only the image copy it to clipboard by using Ctrl+A and Ctrl+C and then open the Word document and paste it from Clipboard.

Good Luck.

Hi Sukesh,

This is not a good solution for my process. I need to insert different image in different place in different pages. So, the solution that you have showed me, doesn't work.

Thanks
Ale
 

f04959c

New Member
Hi All,

Thanks for your support, I made some mistake with the variables. Now all works correctly.

Here below the code

Code:
Dim doc as Object
Dim myBlank = True

If myPage = 0 then
    myBlank = False
End If

If myHeight = 0 then
    myBlank = False
End If

If myWidth = 0 then
    myBlank = False
End If

If myLeft = 0 then
    myBlank = False
End If

If myTop = 0 then
    myBlank = False
End If

If  NOT(myBlank) then
    doc = GetInstance(Handle).ActiveDocument
    doc.InlineShapes.AddPicture(FileName:=PicPath, LinkToFile:=False, SaveWithDocument:=True)
Else
    doc = GetInstance(Handle).ActiveDocument.GoTo(What:=1, Count:= myPage)
    with doc.InlineShapes.AddPicture(FileName:=PicPath, LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
        .Height = myHeight
        .Width = myWidth
        .Left = myLeft
        .Top = myTop
    End With
End If
 

argha.de

New Member
Hi All,

Thanks for your support, I made some mistake with the variables. Now all works correctly.

Here below the code

Code:
Dim doc as Object
Dim myBlank = True

If myPage = 0 then
    myBlank = False
End If

If myHeight = 0 then
    myBlank = False
End If

If myWidth = 0 then
    myBlank = False
End If

If myLeft = 0 then
    myBlank = False
End If

If myTop = 0 then
    myBlank = False
End If

If  NOT(myBlank) then
    doc = GetInstance(Handle).ActiveDocument
    doc.InlineShapes.AddPicture(FileName:=PicPath, LinkToFile:=False, SaveWithDocument:=True)
Else
    doc = GetInstance(Handle).ActiveDocument.GoTo(What:=1, Count:= myPage)
    with doc.InlineShapes.AddPicture(FileName:=PicPath, LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
        .Height = myHeight
        .Width = myWidth
        .Left = myLeft
        .Top = myTop
    End With
End If

Hi @f04959c

Can you please share the input and output arguments here in the code stage and there datatype.
 
Top