none
VB.NET with Word - how to modify a paragraph indent after find RRS feed

  • Question

  • I am using VB.NET (VS2012) and Word (2013) with the Word 14 interop. Note: I am NOT using VBA within Word and can't use that as a solution.

    I am trying to do a search and replace in the document for all instances of a key value pair and can do this easily with a Word.Document.Find.Execute() command.

    However, what I also need to do is adjust the LeftIndent of the paragraph where the word is located because the replacement text is larger (eg "XXXXXXXXXX") than the text it is replacing (eg "XXXXX") and the area isn't large enough.

    So I have been trying the following, but the paragraph indent is just not changing. Note: dicWords is just a Dictionary with my find/replace words.

    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

    However, the above isn't working. No indents happen. Can anyone help?

    Friday, January 25, 2013 9:18 PM

Answers

  • Hi Justin

    Word's Find/Replace functionality can also handle formatting. Is there a reason to be applying the formatting in a separate step, rather than as part of parameters set before executing Find.Execute?

    Something along these lines (untested):

    Dim r as Word.Range = WordDoc.Content
    r.ClearFormatting()
    r.Find.Replacement.ParagraphFormat.LeftIndent = .5
    r.Find.Execute(FindText:=pair.Key, ReplaceWith:=pair.Value, Replace:=WdReplace.wdReplaceAll, _
                           Wrap
    :=WdFindWrap.wdFindContinue)

    I wouldn't use Selection.Find, for various reasons, nor should it be necessary.

    But if you want to work with a loop, as in your original code, I recommend you declare and use a Range object specifically for Find (as in my sample, above) rather than always referring back to WordDoc.Content. The second time you use it, in the If, would apply this formatting to the entire document, were it executed. Word can only work with what you've "found" if you give it a Range object to work with (or a Selection); it won't work on a Range derived from the document.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Justin Sher Monday, January 28, 2013 7:09 PM
    Monday, January 28, 2013 4:14 PM
    Moderator

All replies

  • Hi Justin,

    Thanks for posting in the MSDN Forum.

    I hope following snippet can help you out:

    Public Class ThisDocument
        Private wdApp As Word.Application
        Private wdDoc As Word.Document
    
        Private Sub ThisDocument_Startup() Handles Me.Startup
            wdApp = Globals.ThisDocument.Application
            wdDoc = wdApp.ActiveDocument
            Dim pair As KeyValuePair(Of String, String)
            Dim dir As Dictionary(Of String, String)
    
            dir = New Dictionary(Of String, String)
            dir.Add("Test2", "Indent")
    
            For Each pair In dir
                Dim bfound As Boolean = False
                Do
                    wdDoc.Range().Select()
                    bfound = wdApp.Selection.Find.Execute(FindText:=pair.Key, ReplaceWith:=pair.Value _
                                                        , Replace:=Word.WdReplace.wdReplaceOne, Wrap:=Word.WdFindWrap.wdFindContinue)
                    If bfound = True Then
                        wdApp.Selection.Range.ParagraphFormat.LeftIndent = 35
                    End If
                Loop While bfound = True
            Next
        End Sub
    
        Private Sub ThisDocument_Shutdown() Handles Me.Shutdown
    
        End Sub
    
    End Class

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 28, 2013 6:34 AM
    Moderator
  • Hi Tom,

    Thanks for this but:

    1. When implementing your code in my project, it fails at the wdApp.Selection.Find.Execute with a COM exception saying "This command is not available".

    2. Your code looks like it is VBA as opposed to pure VB.NET with Word interop, so I am not sure if I am missing a COM object or some other assembly. I have only included the Microsoft.Office.Interop.Word assembly (v14.0.0.0). So is there something missing that is causing the COM error?

    - Justin

    Monday, January 28, 2013 3:15 PM
  • Hi Justin

    Word's Find/Replace functionality can also handle formatting. Is there a reason to be applying the formatting in a separate step, rather than as part of parameters set before executing Find.Execute?

    Something along these lines (untested):

    Dim r as Word.Range = WordDoc.Content
    r.ClearFormatting()
    r.Find.Replacement.ParagraphFormat.LeftIndent = .5
    r.Find.Execute(FindText:=pair.Key, ReplaceWith:=pair.Value, Replace:=WdReplace.wdReplaceAll, _
                           Wrap
    :=WdFindWrap.wdFindContinue)

    I wouldn't use Selection.Find, for various reasons, nor should it be necessary.

    But if you want to work with a loop, as in your original code, I recommend you declare and use a Range object specifically for Find (as in my sample, above) rather than always referring back to WordDoc.Content. The second time you use it, in the If, would apply this formatting to the entire document, were it executed. Word can only work with what you've "found" if you give it a Range object to work with (or a Selection); it won't work on a Range derived from the document.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Justin Sher Monday, January 28, 2013 7:09 PM
    Monday, January 28, 2013 4:14 PM
    Moderator
  • Hi Cindy,

    That is exactly what I was after and I got it working.

    Thanks!

    - Justin

    Monday, January 28, 2013 7:09 PM