View Single Post
 
Old 11-04-2019, 02:20 PM
LSMcD LSMcD is offline Windows 10 Office 2019
Novice
 
Join Date: Nov 2019
Posts: 2
LSMcD is on a distinguished road
Default Inserting Icons into TextBox

Hi - first post, so apologies for any errors...

I am building a set a macros that can be used to quickly insert a textbox with a pre-determined icon inside it. I have been able to successfully build the textbox and select it, but I don’t know how to get the cursor inside the box to add the icon graphic so that it resides within the textbox.

The following routine adds a textbox to the document in the place and size I want it, and selects the textbox border. I want to get the cursor inside the textbox so that I can then add the icon.

With just the TextBox selected (cursor not inside the box) I am getting “Method 'AddPicture' of object 'InlineShapes' failed" when I try to add the icon. Here’s what I have:

--------------------------------------------------------
Sub SetWindowUp()
Dim winMain As Window
Dim Box As Shape

For Each winMain In Windows
winMain.View.Zoom.Percentage = 100
Next winMain
Application.ActiveWindow.View.Type = WdViewType.wdPrintView

x = Selection.Information(wdHorizontalPositionRelative ToPage)
y = Selection.Information(wdVerticalPositionRelativeTo Page)

Set Box = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=x, Top:=y, Width:=50, Height:=50)

boxName = Box.Name

ActiveDocument.Shapes.Range(boxName).Select

' the following line gives me error: "Method 'AddPicture' of object 'InlineShapes' failed":

Selection.InlineShapes.AddPicture FileName:= _
"https://cdn.hubblecontent.osi.office.net/firstpartycontent/internal/icons/wheelchairaccess.svg" _
, LinkToFile:=False, SaveWithDocument:=True
End Sub
----------------------------------------------------
Sub Macro2()
' This is a macro I recorded that started with where I clicked
' on the selected textbox (left over by the previous routine above) and placed the cursor inside the box.
' From there I could insert the icon using this line - but here the TextBox name is literal, not a variable.
'
ActiveDocument.Shapes.Range(Array("Text Box 37")).Select
Selection.InlineShapes.AddPicture FileName:= _
"https://cdn.hubblecontent.osi.office.net/firstpartycontent/internal/icons/wheelchairaccess.svg" _
, LinkToFile:=False, SaveWithDocument:=True

End Sub

------------------------------------
What do I need to be doing to be able to insert the icon into the newly created textbox?
Am new to VBA so any assistance most appreciated!
Reply With Quote