Insert picture into textbox in word document using excel

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Insert picture into textbox in word document using excel

Post by YasserKhalil »

Hello everyone
I have a word template and I draw a textbox at a specific place in the word document.
Then I am trying to refer to that textbox in the word document from excel VBA.
Can you guide me to how to refer to the drawn textbox on the word document?

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert picture into textbox in word document using excel

Post by HansV »

I assume that you use the Shapes.AddTextBox method of the Document object.
This method returns a Shape object that you can use to refer to the picture.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert picture into textbox in word document using excel

Post by YasserKhalil »

Thanks a lot for your reply.
In fact, the textbox is already drawn but I don't know how to refer to it. I tried to find its name or anything that is a clue to it.

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert picture into textbox in word document using excel

Post by HansV »

If you know its position, you can loop through the Shapes collection of the document and inspect the Top and Left properties of each shape until they match the known position.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert picture into textbox in word document using excel

Post by YasserKhalil »

Can you give me an example as I don't know how to know the position and also need an example of how loop through the textboxes (not other shapes)?

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert picture into textbox in word document using excel

Post by HansV »

Let's say you have a Word.Application object objWrd and a Word.Document object objDoc.

Code: Select all

    Dim objShp As Shape
    For Each objShp In objDoc.Shapes
        If objShp.Type = 17 Then ' 17 = msoTextBox
            If objShp.Left = objWrd.CentimetersToPoints(4) And objShp.Top = objWrd.CentimetersToPoints(5) Then
                ' ...
                Exit For
            End If
        End If
    Next objShp
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert picture into textbox in word document using excel

Post by YasserKhalil »

Thanks a lot my tutor. Now how can picture inserted? Will it be similar to what we do in excel or the matter is different here?

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert picture into textbox in word document using excel

Post by HansV »

Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert picture into textbox in word document using excel

Post by YasserKhalil »

I tried this line
objShp.CanvasItems.AddPicture FileName:=GraphImage, LinkToFile:=False, SaveWithDocument:=True
but I got error `The member can only be accessed for a group`

By the way, I encountered an error at this line before `For Each objShp In .Shapes` but when changed the declaration like that `Dim objShp As Object`, it passed this error.

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert picture into textbox in word document using excel

Post by HansV »

Sorry, I'm busy with other things so I didn't look at the entire thread. It's something like

Code: Select all

    objShp.Fill.UserPicture "C:\Pictures\Yasser.jpg"
where objShp is the Shape object representing the text box that you created.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert picture into textbox in word document using excel

Post by YasserKhalil »

Thanks you very much. Now it worked well
Best and Kind regards