在word vba运行时创建一个新的选择对象

发布于 2024-07-16 06:01:48 字数 294 浏览 2 评论 0原文

我之前问过这个问题,但我认为我还不够清楚。

我需要编写一个 Word VBA 宏,该宏将提示用户在宏开始运行后突出显示一些文本,然后处理新的选择。 此过程将在宏内重复未知次数。

我遇到的唯一问题是弄清楚如何让 VBA 宏“暂停”并允许用户做出此选择。 如果有人熟悉 AutoCAD VBA,我正在寻找与 AcadSelectionSet.SelectOnScreen 方法等效的方法。 这看起来如此明显和基本,但我在 Microsoft 帮助文件或在线搜索中找不到任何内容来告诉我如何做到这一点。 如果可以的话请帮忙!

I asked this question previously but I don't think I was clear enough.

I need to write a Word VBA macro which will prompt the user to select some text by highlighting it after the macro has started running and then to process the new selection. This process will repeat an unknown number of times within the macro.

The only problem I have is figuring out how to get the VBA macro to 'pause' and allow the user to make this selection. If anyone is familiar with AutoCAD VBA, I am looking for the equivalent of the AcadSelectionSet.SelectOnScreen method. This seems so obvious and fundamental, but I just can't find anything in the Microsoft help files or searching online that shows me how to do this. Please help if you can!

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

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

发布评论

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

评论(2

若能看破又如何 2024-07-23 06:01:48

您是否应该查看 Application 对象的 WindowSelectionChange 事件?

在特殊的 ThisDocument 模块中,您需要如下代码:

Public WithEvents app As Application

Private Sub app_WindowSelectionChange(ByVal Sel As Selection)

MsgBox "The selection has changed"

End Sub

显然,将 MsgBox 替换为有用的代码,并使用 Sel 参数来访问选择。 如果您想尝试其他事件,请使用 ThisDocument 模块顶部的下拉菜单

进行设置,您需要在普通代码模块中使用如下宏:

Sub setUpApp()

Set ThisDocument.app = ThisDocument.Application

End Sub

setUpApp 之后> 运行一次后,每当选择更改时都会触发 app_WindowSelectionChange 事件

Should you perhaps be looking at the WindowSelectionChange event of the Application object?

In the special ThisDocument module, you need code like this:

Public WithEvents app As Application

Private Sub app_WindowSelectionChange(ByVal Sel As Selection)

MsgBox "The selection has changed"

End Sub

Obviously replace the MsgBox with useful code and use the Sel parameter to access the selection. If you want to try other events then use the dropdowns at the top of the ThisDocument module

To set it up, you need a macro like this in a normal code module:

Sub setUpApp()

Set ThisDocument.app = ThisDocument.Application

End Sub

After setUpApp has run once, the app_WindowSelectionChange event will be triggered whenever the selection changes

输什么也不输骨气 2024-07-23 06:01:48

您可以使用无模式表单让宏继续运行,直到满足特定条件:

表单设计器:

  • 添加表单。
  • 添加一个按钮。
  • 添加一个标签。

表单代码:

Option Explicit

Dim m_stopHere As Boolean
Dim m_timesDone As Long

Private Sub CommandButton1_Click()

    m_timesDone = m_timesDone + 1
    m_stopHere = Not DoStuff(m_timesDone)

    Me.Caption = "DoStuff was called " & m_timesDone & " time(s)."

       If m_stopHere Then
    MsgBox "Processing finished, closing form..."
    Unload Me
   End If
End Sub

Private Function DoStuff(times As Long) As Boolean

    Dim myCondition As Boolean

    If times < 5 Then
    MsgBox "You selected: " & Selection.Text
    Selection.Collapse wdCollapseEnd
    myCondition = True
    Else
    Me.Label1.Caption = "No more selections, thanks!"
    End If

    DoStuff = myCondition
End Function

Private Sub UserForm_Initialize()
    Me.Label1.Caption = "Please select some text in Word and press the button."
End Sub

另一个代码模块:

Sub StopAndGo()
    UserForm1.Show vbModeless
End Sub

You could use a modeless form to let your macro keep running until a certain condition was met:

Form designer:

  • Add a form.
  • Add one button.
  • Add one label.

Form code:

Option Explicit

Dim m_stopHere As Boolean
Dim m_timesDone As Long

Private Sub CommandButton1_Click()

    m_timesDone = m_timesDone + 1
    m_stopHere = Not DoStuff(m_timesDone)

    Me.Caption = "DoStuff was called " & m_timesDone & " time(s)."

       If m_stopHere Then
    MsgBox "Processing finished, closing form..."
    Unload Me
   End If
End Sub

Private Function DoStuff(times As Long) As Boolean

    Dim myCondition As Boolean

    If times < 5 Then
    MsgBox "You selected: " & Selection.Text
    Selection.Collapse wdCollapseEnd
    myCondition = True
    Else
    Me.Label1.Caption = "No more selections, thanks!"
    End If

    DoStuff = myCondition
End Function

Private Sub UserForm_Initialize()
    Me.Label1.Caption = "Please select some text in Word and press the button."
End Sub

Another code module:

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