Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-04-2019, 02:20 PM
LSMcD LSMcD is offline Inserting Icons into TextBox Windows 10 Inserting Icons into TextBox Office 2019
Novice
Inserting Icons into TextBox
 
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
  #2  
Old 11-04-2019, 10:01 PM
gmayor's Avatar
gmayor gmayor is offline Inserting Icons into TextBox Windows 10 Inserting Icons into TextBox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Insert a shape rather than a text box
Code:
Sub SetWindowUp()

Dim winMain As Window
Dim Box As Shape
Dim x As Double, y As Double
Dim sPath As String
    sPath = Environ("USERPROFILE") & Chr(92) & "Pictures\"
    For Each winMain In Windows
        winMain.View.Zoom.Percentage = 100
    Next winMain
    Application.ActiveWindow.View.Type = WdViewType.wdPrintView

    x = Selection.Information(wdHorizontalPositionRelativeToPage)
    y = Selection.Information(wdVerticalPositionRelativeToPage)

    Set Box = ActiveDocument.Shapes.AddPicture _
              (FileName:=sPath & "\wheelchair access.png", _
               LinkToFile:=False, _
               SaveWithDocument:=True, _
               Left:=x, Top:=y, Width:=50, Height:=50)
End Sub
The file path in your original effort does not contain an insertable graphic, however the attached is an insertable version of that image, which you should save in your Pictures folder for it to work with the code above.

Personally I would probably just insert an inline shape at the cursor

Code:
Sub InsertIcon()
Dim winMain As Window
Dim Box As InlineShape
Dim x As Double, y As Double
Dim sPath As String
    sPath = Environ("USERPROFILE") & Chr(92) & "Pictures\"
    Application.ActiveWindow.View.Type = WdViewType.wdPrintView

    Set Box = Selection.Range.InlineShapes.AddPicture _
              (FileName:=sPath & "\wheelchair access.png", _
               LinkToFile:=False, _
               SaveWithDocument:=True)
    With Box
        .Width = 50
        .Height = 50
    End With
End Sub
Attached Images
File Type: png wheelchair access.png (2.9 KB, 17 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-05-2019, 09:05 AM
LSMcD LSMcD is offline Inserting Icons into TextBox Windows 10 Inserting Icons into TextBox Office 2019
Novice
Inserting Icons into TextBox
 
Join Date: Nov 2019
Posts: 2
LSMcD is on a distinguished road
Default

Wow, thanks for the help and your ideas. Appreciate your time and help gmayor!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax for inserting blank line before inserting table and after a line or paragraph SamDsouza Word VBA 8 08-04-2019 11:10 PM
Inserting Icons into TextBox Display result in textbox based on the input of another textbox scarymovie Word VBA 5 05-16-2012 07:05 PM
Inserting Icons into TextBox What Do The Different Calendar Icons Mean? mysticglow3 Outlook 1 05-14-2012 09:42 PM
Inserting Icons into TextBox 2 printer icons Topazdan Office 3 05-18-2010 01:35 AM
Icons for different folders pengyou Windows 3 12-09-2009 09:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:35 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft