确定按下了哪个按钮?
我想知道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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的,所以它们是文档中的命令按钮。
在这种情况下,您无能为力 - 您需要有名为
Button1_Click
、Button2_Click
等(或任何按钮名称)的处理程序。但是,您可以执行以下操作:
另请参阅此技术说明了解如何创建按钮在代码中。
编辑:更新为传递
CommandButton
引用,以便共享函数可以访问按钮属性。编辑 2:更新为使用 InlineShapes 显示完整代码。 请注意,这不再传递 Button 对象,因为按钮的宽度/高度可以直接从字段获取。
编辑 3:根据要求进行更新以使用 Shapes 而不是 InlineShapes。 (命令按钮和插入的图片现在都是形状)。
OK, so they're
CommandButton
s 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:
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.
EDIT 3: Updated as requested to use Shapes rather than InlineShapes. (Both the CommandButton and the inserted Picture are now Shapes).
我假设您的意思是该按钮是命令栏按钮(又名工具栏按钮)。
如果是这样,您可以使用
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.您可以将基本宏放入单独的子宏中,然后从每个按钮的单击事件中调用宏,并将所需大小作为参数传递。 那么按钮中唯一的代码就是调用基本子函数。
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.
您可以在 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.
将以下内容放置在 ActiveX 按钮和文本框的各种 click/got_focus/change 事件中对我来说很有效:
Placing the following in the various click/got_focus/change events of ActiveX buttons and textboxes works for me: