如何在Word.Application中的光标位置获取文本 [英] How Can I Get Text At Cursor Position In Word.Application
问题描述
大家好,
我正在使用单词COM对象从现有的word文档中获取一些数据。我需要知道如何从oWord.ActiveDocument获取光标位置的文本。几乎所有谷歌链接都在拼写在word文档中插入文本。但我需要知道如何从单词中获取单词,句子,一行文本。那是在当前光标位置。我知道一种名为AutoIt的脚本语言。在AutoIt中,我可以轻松地告诉我的程序从光标的后面或光标的前面捕获单词或senetence或char。但我不知道如何在VB.NET中做到这一点。请帮忙。
这就是我的意思。
Hi all,
I am using word COM object to get some data from an existing word document. All i need to know how to get the text at the cursor position from oWord.ActiveDocument. Almost all google links are ponting to insert text in word document. But i need to know how to get a word, sentence, a line of text from word. And that is in the current cursor position. I know a scripting language named AutoIt. In AutoIt, i can easily tell my program to catch word or senetence or char from either backward of the cursor or forward of the cursor. But i don't know how to do it in VB.NET. Please help.
And this is i am up to.
Dim oWord As Word.Application
Dim Sel As Word.Section
oWord = GetObject(, "Word.Application")
Sel = oWord.Selection
Dim oRange As Word.Range = Sel.Range()
推荐答案
您可以使用Word-intern功能:
You could use the Word-intern functionality for that :
' connection to MS-Word
objWordApplication = CreateObject("Word.Application")
objWordApplication.Visible = False
Dim objDoc As Word.Document
objDoc = objWordApplication.Documents.Add(myFileName)
objDoc.Protect(Word.WdProtectionType.wdNoProtection)
objDoc.Activate()
' replace Text-elements
With objWordApplication.Selection.Find
.Text = "Date"
.Replacement.Text = Now.ToString("dd.MM.yyyy")
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
.Text = "Name"
.Replacement.Text = myName
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
.Text = "VorName"
.Replacement.Text = myForeName
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
在此示例中关键字的每个外观将被我的应用程序中的字符串替换...
我希望它可以帮助你...
In this sample each appearence of the keyword will be replaced by a string from my application ...
I hope it helps you ...
这篇关于如何在Word.Application中的光标位置获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!