在运行时在word vba中创建新的选择对象
我以前问过这个问题,但我觉得我不够清楚 我需要编写一个Word VBA宏,它将提示用户在宏开始运行后通过高亮显示来选择一些文本,然后处理新的选择。此过程将在宏中重复未知次数在运行时在word vba中创建新的选择对象,vba,ms-word,selection,Vba,Ms Word,Selection,我以前问过这个问题,但我觉得我不够清楚 我需要编写一个Word VBA宏,它将提示用户在宏开始运行后通过高亮显示来选择一些文本,然后处理新的选择。此过程将在宏中重复未知次数 我唯一的问题是如何让VBA宏“暂停”并允许用户进行选择。如果有人熟悉AutoCAD VBA,我将寻找与AcadSelectionSet.SelectOnScreen方法等效的方法。这看起来很明显,也很基本,但我在Microsoft帮助文件或在线搜索中找不到任何可以告诉我如何做到这一点的内容。如果可以的话,请帮忙 您是否应该查
我唯一的问题是如何让VBA宏“暂停”并允许用户进行选择。如果有人熟悉AutoCAD VBA,我将寻找与
AcadSelectionSet.SelectOnScreen
方法等效的方法。这看起来很明显,也很基本,但我在Microsoft帮助文件或在线搜索中找不到任何可以告诉我如何做到这一点的内容。如果可以的话,请帮忙 您是否应该查看应用程序
对象的WindowSelectionChange
事件
在特殊的ThisDocument
模块中,您需要这样的代码:
Public WithEvents app As Application
Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
MsgBox "The selection has changed"
End Sub
显然,用有用的代码替换MsgBox
,并使用Sel
参数访问选择。如果您想尝试其他事件,请使用此文档顶部的下拉列表
模块
要进行设置,您需要在普通代码模块中使用如下宏:
Sub setUpApp()
Set ThisDocument.app = ThisDocument.Application
End Sub
setUpApp
运行一次后,如果您可能正在查看应用程序
对象的WindowSelectionChange
事件,则只要选择发生更改,就会触发app\u WindowSelectionChange
事件
在特殊的ThisDocument
模块中,您需要这样的代码:
Public WithEvents app As Application
Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
MsgBox "The selection has changed"
End Sub
显然,用有用的代码替换MsgBox
,并使用Sel
参数访问选择。如果您想尝试其他事件,请使用此文档顶部的下拉列表
模块
要进行设置,您需要在普通代码模块中使用如下宏:
Sub setUpApp()
Set ThisDocument.app = ThisDocument.Application
End Sub
运行一次
setUpApp
后,只要选择发生更改,就会触发app\u WindowSelectionChange
事件。您可以使用无模式窗体让宏继续运行,直到满足特定条件:
表单设计器:
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
- 添加一个表单
- 添加一个按钮
- 添加一个标签
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
可以使用无模式窗体使宏保持运行,直到满足特定条件: 表单设计器:
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
- 添加一个表单
- 添加一个按钮
- 添加一个标签
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