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?
Insert picture into textbox in word document using excel
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Insert picture into textbox in word document using excel
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.
This method returns a Shape object that you can use to refer to the picture.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Insert picture into textbox in word document using excel
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.
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.
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Insert picture into textbox in word document using excel
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
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Insert picture into textbox in word document using excel
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)?
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Insert picture into textbox in word document using excel
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
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Insert picture into textbox in word document using excel
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?
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Insert picture into textbox in word document using excel
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Insert picture into textbox in word document using excel
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.
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.
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Insert picture into textbox in word document using excel
Sorry, I'm busy with other things so I didn't look at the entire thread. It's something like
where objShp is the Shape object representing the text box that you created.
Code: Select all
objShp.Fill.UserPicture "C:\Pictures\Yasser.jpg"
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Insert picture into textbox in word document using excel
Thanks you very much. Now it worked well
Best and Kind regards
Best and Kind regards