如何在Word中的光标位置插入文本? [英] How do I insert text at my cursor location in Word?

查看:149
本文介绍了如何在Word中的光标位置插入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的Excel宏在已经打开的Word文档中的光标位置插入一些文本.

I'm trying to have my Excel macro insert some text at my cursor location in a already-opened Word Document.

这是我写的.我知道ActiveDocument.Range具有Start和End参数,但是我似乎无法为它们分配当前选择"作为值.帮助?谢谢吗?

This is what I have written. I'm aware that the ActiveDocument.Range has the Start and End arguments, but I cannot seem to be able to assign them the "current selection" as a value. Help? Thanks?

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

推荐答案

当前选择为Selection.

如果如您所指示的那样,需要在自动执行Word的Excel宏中使用它,则需要使用已声明并实例化的Word.Application对象对其进行限定.看起来像这样:

If, as you indicate, you need to use this in an Excel macro that's automating Word, then you need to use the Word.Application object you've declared and instantiated to qualify it. It would look something like this:

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"

这篇关于如何在Word中的光标位置插入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆