2

私はVB.NET(VS2012)とWord(2013)をWord14の相互運用機能で使用しています。注: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

ただし、上記は機能していません。

4

1 に答える 1