none
VB.NET Word Interop, Find and Delete (Range?) RRS feed

  • Question

  • I would like to clean up this word document.  the below code works.  but I would like to better understand Word Interop.  instead of finding each line and replace with "" to remove the bottom signature.   is there a way to delete lines starting with "Your Name" and ending with "youremail@server.com"  and everything in between?  I guess setting a range and delete?  and or deleting everything after a specific find.    any help is greatly appreciated. 

     Dim objWord As Word.Application = New Word.Application
            Dim objDoc As Document = objWord.Documents.Open(LoadDir & tbEMailIfContains.Text & eMailHitCount & ".rtf")
            objWord.Visible = False
    
            'This does Header!!!
            Do While objDoc.Content.Find.Execute(FindText:="Subject:", Wrap:=Word.WdFindWrap.wdFindContinue) = True
                objWord.Selection.Range.Bookmarks("\line").Range.Delete()
            Loop
    
            'This Does Footer
            objDoc.Content.Find.Execute(FindText:="Your Name", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="Your Title", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="Your Company", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="Your Address", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="Your Work Number", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="Your Cell Number", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="youremail@server.com", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
    
    
            objWord.Documents.Close(Word.WdSaveOptions.wdSaveChanges)
            objWord.Quit()


    Work Smarter Not Harder




    Tuesday, June 28, 2016 1:36 PM

Answers

  • Hi jimbrown.ws

    as you asked to find the text and then try to search in document. so I modify the code that I had mentioned above and make little changes in that now it can able to search the text and from that we can get line number and then we will try to delete the range.

    Sub Main()
    
            Dim objWord As Word.Application = New Word.Application
            Dim objDoc As Word.Document
            objDoc = objWord.Documents.Open("C:\Users\v-padee\Desktop\dem16.docx")
            objWord.Visible = True
    
            Dim rng, myRange, myRange1 As Word.Range
            Dim start, finish As Integer
            myRange = objDoc.Content
            myRange.Find.Execute("Your name:")
            myRange.Select()
    
            start = myRange.Information(Word.WdInformation.wdFirstCharacterLineNumber)
    
            myRange1 = objDoc.Content
            myRange1.Find.Execute("youremail@server.com")
            myRange1.Select()
    
            finish = myRange1.Information(Word.WdInformation.wdFirstCharacterLineNumber)
    
    
            Dim startLocation As Object = objDoc.Sentences(start).Start
            Dim endLocation As Object = objDoc.Sentences(finish).End
    
            ' Supply a Start and End value for the Range.
            rng = objDoc.Range(Start:=startLocation, End:=endLocation)
    
            ' Select the Range
            rng.Select()
            rng.Delete()
            objWord.Documents.Close(Word.WdSaveOptions.wdSaveChanges)
            objWord.Quit()
        End Sub

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by jimbrown.net Thursday, June 30, 2016 7:00 PM
    Thursday, June 30, 2016 7:12 AM
    Moderator

All replies

  • Hi jimbrown.ws,

    if you know the line numbers then you can define the range and try to delete it then you not need to delete each sentence line by line and you also not need to use loop.

    you can see in the image below it will select the sentences and then you can delete that range.

    Here below is the code you can try to use.

    Sub Main()
    
            Dim objWord As Word.Application = New Word.Application
            Dim objDoc As Word.Document
            objDoc = objWord.Documents.Open("C:\Users\v-padee\Desktop\dem15.docx")
            objWord.Visible = True
    
            Dim rng As Word.Range
    
            Dim startLocation As Object = objDoc.Sentences(4).Start
            Dim endLocation As Object = objDoc.Sentences(10).End
    
            ' Supply a Start and End value for the Range.
            rng = objDoc.Range(Start:=startLocation, End:=endLocation)
    
            ' Select the Range
            rng.Select()
            rng.Delete()
            objWord.Documents.Close(Word.WdSaveOptions.wdSaveChanges)
            objWord.Quit()
        End Sub

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 29, 2016 2:11 AM
    Moderator
  • Thank you, Deepak Panchal10 the sentences can very.  is there away to do a find "Your Name" to get the sentence and then go from there? 

    Work Smarter Not Harder

    Wednesday, June 29, 2016 1:26 PM
  • Yes there is. You use Regular Expressions. Here is some code that you can adapt to suit your needs.
    By setting the constant REMOVEALLTEXT to True it removes the complete line that matches.
    By setting the constant REMOVEALLTEXT to False it removes only the text between the name and the email address.

    Option Explicit
    '*
    '* Modify here
    '*
    Const REGSTR = "Your name(.*)yourmail@company.com"
    Const REMOVEALLTEXT = False
    Sub test()
        Dim doc As Document
        Dim theLine As String
        Dim i As Integer
        Dim n As Integer
        Dim matchCount As Integer
        Dim submatchCount As Integer
        Dim theMatch As Variant
        Dim thesubMatch As String
        Dim theMatches As Variant
        Dim regExp As Variant
    
        Set regExp = CreateObject("vbscript.regexp")
    
        Set doc = ActiveDocument    'Set this to your target document
        regExp.Pattern = REGSTR     'Modify REGSTR above as needed
        regExp.Global = True        'True -> Finds all matches in a sting, False -> stops at first match
        regExp.IgnoreCase = True    'Adjust as needed
    
        n = ActiveDocument.Paragraphs.Count 'Number of lines in the document
        For i = 1 To n
            theLine = doc.Paragraphs(i).Range.Text
            Debug.Print theLine
            Set theMatches = regExp.Execute(theLine)
            matchCount = theMatches.Count
            If matchCount Then                'Match found
                Set theMatch = theMatches(0)  'Complete line here. Use this to remove the complete line
                '*
                '* Remove text between name and email
                '*
                If REMOVEALLTEXT Then    'Remove all text on the current line
                    doc.Paragraphs(i).Range.Text = Replace(theLine, theMatch, " ")
                Else
                    submatchCount = theMatch.Submatches.Count
                    If submatchCount Then   'Match on text in between. Use this to remove text in between only
                        thesubMatch = theMatch.Submatches(0)
                        Debug.Print "Submatch=" & thesubMatch
                        '*
                        '* Remove text between name and email
                        '*
                        doc.Paragraphs(i).Range.Text = Replace(theLine, thesubMatch, " ")
                    End If
                End If    'Else
            End If    'MatchCount
        Next i
    End Sub

    Original document:

    Testline without change
    Your name lorem ipsum yourmail@company.com
    Testline without change

    With REMOVEALLTEXT = False you get:

    Testline without change
    Your name yourmail@company.com
    Testline without change

    With REMOVEALLTEXT = True you get:

    Testline without change

    Testline without change

    Please note that Regular Experssions is not for the faint of heart. It's a steep learning curve but once you know it you have a very powerful tool you can use in many applications. Google is full of references, here is my fav site:

    http://www.regular-expressions.info/quickstart.html


    Best regards, George


    Thursday, June 30, 2016 7:03 AM
  • Hi jimbrown.ws

    as you asked to find the text and then try to search in document. so I modify the code that I had mentioned above and make little changes in that now it can able to search the text and from that we can get line number and then we will try to delete the range.

    Sub Main()
    
            Dim objWord As Word.Application = New Word.Application
            Dim objDoc As Word.Document
            objDoc = objWord.Documents.Open("C:\Users\v-padee\Desktop\dem16.docx")
            objWord.Visible = True
    
            Dim rng, myRange, myRange1 As Word.Range
            Dim start, finish As Integer
            myRange = objDoc.Content
            myRange.Find.Execute("Your name:")
            myRange.Select()
    
            start = myRange.Information(Word.WdInformation.wdFirstCharacterLineNumber)
    
            myRange1 = objDoc.Content
            myRange1.Find.Execute("youremail@server.com")
            myRange1.Select()
    
            finish = myRange1.Information(Word.WdInformation.wdFirstCharacterLineNumber)
    
    
            Dim startLocation As Object = objDoc.Sentences(start).Start
            Dim endLocation As Object = objDoc.Sentences(finish).End
    
            ' Supply a Start and End value for the Range.
            rng = objDoc.Range(Start:=startLocation, End:=endLocation)
    
            ' Select the Range
            rng.Select()
            rng.Delete()
            objWord.Documents.Close(Word.WdSaveOptions.wdSaveChanges)
            objWord.Quit()
        End Sub

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by jimbrown.net Thursday, June 30, 2016 7:00 PM
    Thursday, June 30, 2016 7:12 AM
    Moderator
  • Thank you Deepak,   that worked very nicely!

    Work Smarter Not Harder

    Thursday, June 30, 2016 7:28 PM