确定按下了哪个按钮?

发布于 2024-07-27 00:31:03 字数 1470 浏览 2 评论 0原文

我想知道Word宏是否有一种简单的方法来确定刚刚按下了哪个按钮? 我有一个带有多个按钮的文档模板,这些按钮都应该触发宏。 问题是,我想创建一个在每个按钮中调用的宏。 我不想为每个按钮添加大量宏。

现在,这个宏,当按下按钮时,它会插入一张图片,并且该图片的大小是根据按钮大小选择的。 意思是,这最终会成为图片占位符。 但是,我想动态编写宏,以便相同的代码可以在每个按钮上运行,而不仅仅是调用宏。

完整的宏已经完成,我只需要知道最后一件事,如果有人有任何关于如何完成此操作的信息? :) 提前致谢!

更新:这是目前的代码

Private Sub ImageButton1_Click() 图片占位符 ImageButton1 结束子

私有子ImageButton2_Click() 图片占位符 ImageButton2 结束子

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终难遇 2024-08-03 00:31:03

好的,所以它们是文档中的命令按钮。

在这种情况下,您无能为力 - 您需要有名为 Button1_ClickButton2_Click 等(或任何按钮名称)的处理程序。

但是,您可以执行以下操作:

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

另请参阅此技术说明了解如何创建按钮在代码中。


编辑:更新为传递CommandButton引用,以便共享函数可以访问按钮属性。


编辑 2:更新为使用 InlineShapes 显示完整代码。 请注意,这不再传递 Button 对象,因为按钮的宽度/高度可以直接从字段获取。

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

编辑 3:根据要求进行更新以使用 Shapes 而不是 InlineShapes。 (命令按钮和插入的图片现在都是形状)。


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

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
动听の歌 2024-08-03 00:31:03

我假设您的意思是该按钮是命令栏按钮(又名工具栏按钮)。

如果是这样,您可以使用 Application.CommandBars.ActionControl 来获取对所单击的按钮的引用。 从那里您可以检查标题、标签或其他内容。

I assume you mean that the button is a Command Bar button (aka toolbar button).

If so, you can use Application.CommandBars.ActionControl to get a reference to the button that was clicked. From there you can examine the caption, tag, or whatever.

缪败 2024-08-03 00:31:03

您可以将基本宏放入单独的子宏中,然后从每个按钮的单击事件中调用宏,并将所需大小作为参数传递。 那么按钮中唯一的代码就是调用基本子函数。

You could put your base macro into a separate sub and then just call the macro from each button's click event, passing as a parameter the desired size. Then the only code you would have in the buttons is the call to the base sub.

夢归不見 2024-08-03 00:31:03

您可以在 Vba 中使用一个按钮来在调用带有参数的宏时传递参数。
例如:如果您有一个名为 function test(x as string) 的函数
那么调用宏的按钮将具有 onclick("sheetx!test", "whatever") 的语法。 这样你就可以调用一个通用宏。 希望这对您有帮助。

You can have a button in Vba to pass arguments when it calls the macro which takes arguments.
For eg: if you have a function called as function test(x as string)
then the button which calls the macro will have a syntax as onclick("sheetx!test", "whatever"). So that way you can either have a generic macro to be called. Hope this helps you.

桜花祭 2024-08-03 00:31:03

将以下内容放置在 ActiveX 按钮和文本框的各种 click/got_focus/change 事件中对我来说很有效:

    MsgBox ThisDocument.ActiveWindow.Selection.Fields.Item(1).OLEFormat.Object.Name

Placing the following in the various click/got_focus/change events of ActiveX buttons and textboxes works for me:

    MsgBox ThisDocument.ActiveWindow.Selection.Fields.Item(1).OLEFormat.Object.Name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文