我正在尝试让我的 Excel 宏在已打开的 Word 文档中的光标位置插入一些文本。
这是我写的。我知道 ActiveDocument.Range 有 Start 和 End 参数,但我似乎无法将“当前选择”作为值分配给它们。帮助?谢谢?
Sub InsertText()
Dim rngTemp As Word.Range
Set rngTemp = ActiveDocument.Range
With rngTemp
.InsertAfter "This is my sample text"
.Font.Name = "Tahoma"
.Font.Size = 11
.InsertParagraphAfter
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
当前的选择是Selection。
正如您所指出的,如果您需要在自动化 Word 的 Excel 宏中使用它,那么您需要使用已声明并实例化的 Word.Application 对象来限定它。它看起来像这样:
Dim wdApp as Word.Application
Set wdApp = GetObject(, "Word.Application")
wdApp.Selection.Text = "text at the current selection"
'Get a Range for the current selection
Dim rng as Word.Range
Set rng = wdApp.Selection.Range
rng.Text = "this text will replace the text in the current selection"
rng.Collapse wdCollapseEnd
rng.Text = " and this text will come after the current selection"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8447 次 |
| 最近记录: |