Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 291347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:04:51+00:00 2026-05-12T06:04:51+00:00

I’m wondering if there’s a simple way for a Word macro to determine which

  • 0

I’m wondering if there’s a simple way for a Word macro to determine which button was just pressed? I have a document template with several button which should all fire a macro.
The thing is, I want to create ONE macro which is called in each button. I don’t want tons of macros for each button.

Now, this macro, when the button is pressed, it inserts a picture and the size of this picture is selected based on the buttons size. Meaning, this ends up as a picture placeholder. But, I want to write the macro dynamically so that the same code will work on each button without doing more than just calling the macro.

The complete macro is already done, I just need to know this one last thing, if anyone has any info on how to accomplish this? 🙂 Thanx in advance!

UPDATE: This is the code at the moment

Private Sub ImageButton1_Click()
PicturePlaceholder ImageButton1
End Sub

Private Sub ImageButton2_Click()
PicturePlaceholder ImageButton2
End Sub

Public Sub PicturePlaceholder(ByVal oButton As CommandButton)    
Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range
Dim buttonHeight As String
Dim buttonWidth As String

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument


Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range

Response = MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?")
If Response = vbYes Then rgePlace.Fields(1).Delete
If Response = vbCancel Then Exit Sub
If Response = vbNo Then

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then Exit Sub
Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Anchor:=rgePlace)
With oShape
.Height = oButton.Height
.Width = oButton.Width
End With

rgePlace.Fields(1).Delete


End If
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:04:51+00:00Added an answer on May 12, 2026 at 6:04 am

    OK, so they’re CommandButtons in the document.

    In that case, there’s nothing you can do – you need to have handlers called Button1_Click, Button2_Click, etc. (or whatever the button names are).

    However, you can do something like this:

    Private Sub Button1_Click(...)
        DoStuff Button1
    End Sub
    
    Private Sub Button2_Click(...)
        DoStuff Button2
    End Sub
    
    Private Sub DoStuff(ByVal oButton As CommandButton)
        ' All your shared code goes here
        MsgBox oButton.Caption
    End Sub
    

    See also this tech note for how to create your buttons in code.


    EDIT: updated to pass CommandButton reference so that the shared function can access the button properties.


    EDIT 2: updated to show complete code using InlineShapes. Note that this no longer passes in the Button object, since the width/height of the button can be obtained directly from the field.

    Private Sub CommandButton1_Click()
        PicturePlaceholder
    End Sub
    
    Private Sub CommandButton2_Click()
        PicturePlaceholder
    End Sub
    
    Public Sub PicturePlaceholder()
    
        ' Get the selected field, which must be a button field
    
        Dim oField As Field
        Set oField = Selection.Fields(1)
    
        Debug.Assert oField.Type = wdFieldOCX
    
    
        ' Ask the user what he wants to do
    
        Select Case MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?")
    
            Case vbCancel
                Exit Sub
    
            Case vbYes
                oField.Delete
                Exit Sub
    
        End Select
    
    
        ' Get the filename of the picture to be inserted
    
        Dim strFilePath As String
    
        With Application.FileDialog(msoFileDialogFilePicker)
    
            .AllowMultiSelect = False
    
            If .Show() <> 0 Then
                strFilePath = .SelectedItems(1)
            End If
    
        End With
    
        If strFilePath = "" Then
            Exit Sub
        End If
    
    
        ' Figure out where to insert the picture, and what size to make it
    
        Dim oRange As Range
        Set oRange = oField.Result
    
        Dim sglWidth As Single
        sglWidth = oField.InlineShape.Width ' oButton.Width
    
        Dim sglHeight As Single
        sglHeight = oField.InlineShape.Height ' oButton.Height
    
    
        ' Delete the button field
    
        oField.Delete
    
    
        ' Insert and resize the picture
    
        Dim oInlineShape As Word.InlineShape
        Set oInlineShape = oRange.InlineShapes.AddPicture(FileName:=strFilePath, LinkToFile:=False, SaveWithDocument:=True, Range:=oRange)
    
        With oInlineShape
            .Width = sglWidth
            .Height = sglHeight
        End With
    
    End Sub
    

    EDIT 3: Updated as requested to use Shapes rather than InlineShapes. (Both the CommandButton and the inserted Picture are now Shapes).


    Private Sub CommandButton1_Click()
        PicturePlaceholder
    End Sub
    
    Private Sub CommandButton2_Click()
        PicturePlaceholder
    End Sub
    
    Public Sub PicturePlaceholder()
    
        ' Get the selected shape, which must be a button shape
    
        Debug.Assert Selection.Type = wdSelectionShape
    
        Dim oButtonShape As Shape
        Set oButtonShape = Selection.ShapeRange(1)
    
    
        ' Ask the user what he wants to do
    
        Select Case MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?")
    
            Case vbCancel
                Exit Sub
    
            Case vbYes
                oButtonShape.Delete
                Exit Sub
    
        End Select
    
    
        ' Get the filename of the picture to be inserted
    
        Dim strFilePath As String
    
        With Application.FileDialog(msoFileDialogFilePicker)
    
            .AllowMultiSelect = False
    
            If .Show() <> 0 Then
                strFilePath = .SelectedItems(1)
            End If
    
        End With
    
        If strFilePath = "" Then
            Exit Sub
        End If
    
    
        ' Insert the picture at the same size/position
    
        Dim oPictureShape As Shape
        Set oPictureShape = ActiveDocument.Shapes.AddPicture _
            ( _
            FileName:=strFilePath, _
            LinkToFile:=False, _
            SaveWithDocument:=True, _
            Left:=oButtonShape.Left, _
            Top:=oButtonShape.Top, _
            Width:=oButtonShape.Width, _
            Height:=oButtonShape.Height, _
            Anchor:=oButtonShape.Anchor _
            )
    
    
        ' Copy across the button shape formatting
    
        oButtonShape.PickUp
        oPictureShape.Apply
    
    
        ' Copy across other layout details
    
        oPictureShape.LayoutInCell = oButtonShape.LayoutInCell
    
        oPictureShape.LockAnchor = oButtonShape.LockAnchor
    
        oPictureShape.RelativeHorizontalPosition = oButtonShape.RelativeHorizontalPosition
        oPictureShape.RelativeVerticalPosition = oButtonShape.RelativeVerticalPosition
    
        oPictureShape.WrapFormat.Type = oButtonShape.WrapFormat.Type
        oPictureShape.WrapFormat.Side = oButtonShape.WrapFormat.Side
        oPictureShape.WrapFormat.DistanceTop = oButtonShape.WrapFormat.DistanceTop
        oPictureShape.WrapFormat.DistanceLeft = oButtonShape.WrapFormat.DistanceLeft
        oPictureShape.WrapFormat.DistanceBottom = oButtonShape.WrapFormat.DistanceBottom
        oPictureShape.WrapFormat.DistanceRight = oButtonShape.WrapFormat.DistanceRight
        oPictureShape.WrapFormat.AllowOverlap = oButtonShape.WrapFormat.AllowOverlap
    
    
        ' Delete the button shape
    
        oButtonShape.Delete
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.