确定按下了哪个按钮?

时间:2009-07-13 13:02:10

标签: vba button ms-word word-vba

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

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

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

更新:这是目前的代码

Private Sub ImageButton1_Click() PicturePlaceholder ImageButton1 结束子

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

5 个答案:

答案 0 :(得分:3)

好的,所以他们在文档中是CommandButton

在这种情况下,您无能为力 - 您需要拥有名为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

另请参阅this tech note了解如何在代码中创建按钮。


编辑:已更新以传递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。 (CommandButton和插入的Picture现在都是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

答案 1 :(得分:1)

我认为你的意思是按钮是一个命令栏按钮(又名工具栏按钮)。

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

你可以在Vba中有一个按钮,当它调用带参数的宏时传递参数。 例如:如果你有一个函数叫做函数测试(x作为字符串) 然后调用宏的按钮将具有onclick语法(“sheetx!test”,“whatever”)。这样你就可以有一个通用的宏来调用。希望这会对你有所帮助。

答案 4 :(得分:0)

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

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