VB.NET with Word - 如何在查找后修改段落缩进

时间:2013-01-25 16:58:32

标签: vb.net formatting ms-word replace indentation

我使用VB.NET(VS2012)和Word(2013)与Word 14互操作。注意:我没有在Word中使用VBA,也不能将其用作解决方案。

我正在尝试在文档中搜索并替换键值对的所有实例,并且可以使用Word.Document.Find.Execute()命令轻松完成此操作。

但是,我还需要做的是调整单词所在段落的LeftIndent,因为替换文本比它替换的文本更大(例如“XXXXXXXXXX”)(例如“XXXXX” )而且面积不够大。

所以我一直在尝试以下内容,但段落缩进只是没有改变。注意:dicWords只是一个包含我的查找/替换字词的词典。

Imports Microsoft.Office.Interop.Word

' Open Word document
Dim WordApp As New Application
Dim WordDoc As Document = WordApp.Documents.Open(WordFile.FullName, False, True, False)

' Loop through the dictionary of parts and find/replace
Dim pair As KeyValuePair(Of String, String)
For Each pair In dicWords

    ' Replace text
    Dim bFound As Boolean = False
    Do
        ' Do Search
        bFound = WordDoc.Content.Find.Execute(FindText:=pair.Key, ReplaceWith:=pair.Value, Replace:=WdReplace.wdReplaceOne, Wrap:=WdFindWrap.wdFindContinue)
        If bFound = True Then
            ' The range should be set from above Find, so now set the LeftIndent
            WordDoc.Content.ParagraphFormat.LeftIndent = 5
        End If

    Loop While bFound = True

Next

但是,上述方法无效。

1 个答案:

答案 0 :(得分:0)

尝试:

WordDoc.Selection.Range.ParagraphFormat.LeftIndent = 5