vba 确定按下了哪个按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1119315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:35:15  来源:igfitidea点击:

Determine which button was pressed?

vbabuttonms-wordword-vba

提问by Kenny Bones

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.

我想知道是否有一种简单的方法可以让 Word 宏确定刚刚按下了哪个按钮?我有一个带有几个按钮的文档模板,它们都应该触发一个宏。问题是,我想创建一个在每个按钮中调用的宏。我不希望每个按钮都有大量的宏。

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 ImageButton1_Click() PicturePlaceholder ImageButton1 End Sub

Private Sub ImageButton2_Click() PicturePlaceholder ImageButton2 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

回答by Gary McGill

OK, so they're CommandButtons in the document.

好的,所以它们CommandButton在文档中。

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).

在这种情况下,有什么可以做-你需要有处理程序叫Button1_ClickButton2_Click等(或任何按钮名称)。

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 notefor how to create your buttons in code.

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



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

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



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.

编辑 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


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

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



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

回答by Gary McGill

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

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

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

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

回答by StevenzNPaul

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.

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

回答by PhilipDG

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

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

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

回答by Dcritelli

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.

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