简体   繁体   English

MS-Word-VBA UserForm-如何从命令按钮运行宏到文档中的特定位置?

[英]MS-Word-VBA UserForm - How to run a macro from a command button to a specific location inside the document?

I can't figure out how to run a macro at a specific spot inside the Word document when a user presses the command button on the UserForm. 当用户按下用户窗体上的命令按钮时,我无法弄清楚如何在Word文档中的特定位置运行宏。 Currently when the command button is pressed the macro runs at the beginning of the document. 当前,当按下命令按钮时,宏在文档的开头运行。 I tried finding a bookmark and running the macro there, but it still only initiates at the beginning of the document or wherever the last place the mouse clicked on the Word document. 我试图找到一个书签并在其中运行宏,但是它仍仅在文档的开头或鼠标在Word文档上单击的最后一个位置启动。 Inside the document I have field codes around a checkbox, so when the checkbox is clicked "Yes" the macro runs and lines out a few sentences. 在文档内部,我有一个复选框周围的域代码,因此,当单击复选框“是”时,宏就会运行并排列一些句子。

Original Checkbox in pic 图片中的原始复选框

在此处输入图片说明

This is what it is suppose to look like when the macro runs 这是宏运行时的样子

在此处输入图片说明

Now the Macro works fine when the user clicks the checkbox on the document. 现在,当用户单击文档上的复选框时,宏可以正常工作。 The code works inside the userform when the command button is clicked, but it runs the macro at the beginning of the document or wherever the mouse clicked last, instead of at the location of the checkmark. 当单击命令按钮时,代码在用户窗体内起作用,但是它将在文档的开头或最后单击鼠标的位置(而不是选中标记的位置)运行宏。 Basically what I can't figure out is how to have the code click near the checkbox and then run the macro. 基本上我不知道是如何使代码在复选框旁边单击,然后运行宏。 Any help would be greatly appreciated, thanks. 任何帮助将不胜感激,谢谢。

I was able to finally figure this out. 我终于能够弄清楚这一点。 I delayed asking the question on here for days trying to do research on my own. 我推迟了几天在这里问这个问题,试图自己做研究。 But of course, hours after I post on here I figure it out. 但是,当然,在我张贴在这里的几个小时后,我就知道了。

All I needed was these 2 lines 我只需要这两行

ActiveDocument.Bookmarks("BookmarkName").Select
MacroName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM