none
VB.Net Word Interop removed all blank lines? RRS feed

  • Question

  • I have the below code that works great but find and replace leaves blank lines.  as well as I have other blank lines I would like to remove / delete.  how do I do this?

            Dim objWord As Word.Application = New Word.Application
            Dim objDoc As Document = objWord.Documents.Open("C:\test.rtf")
            objWord.Visible = False
    
            Do While objDoc.Content.Find.Execute(FindText:="Subject:", Wrap:=Word.WdFindWrap.wdFindContinue) = True
                objWord.Selection.Range.Bookmarks("\line").Range.Delete()
            Loop
    
            objDoc.Content.Find.Execute(FindText:="some text", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="some more text", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
            
            'objDoc.Content.Find.Execute(FindText:=" ", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)
    
    
            objWord.Documents.Close(Word.WdSaveOptions.wdSaveChanges)
            objWord.Quit()


    Work Smarter Not Harder

    Saturday, June 25, 2016 1:48 AM

Answers

  • Add:

            objDoc.Content.Find.Execute(FindText:="^p^p", ReplaceWith:="^p", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="^p", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)

    • Marked as answer by jimbrown.net Monday, June 27, 2016 5:56 PM
    Saturday, June 25, 2016 3:05 AM
    Moderator
  • Hi jimbrown.ws,

    you can try to use the code below.

    I have check it will not remove formatting when you remove blank lines.

    Sub Removeline() 
        Do 
            With Selection.Find 
                .Text = "^p^p" 
                .Replacement.Text = "^p" 
                .Forward = True 
                .Wrap = wdFindContinue 
            End With 
        Loop Until Selection.Find.Execute(Replace:=wdReplaceAll) = False 
    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 Monday, June 27, 2016 5:56 PM
    Monday, June 27, 2016 2:12 AM
    Moderator

All replies

  • Add:

            objDoc.Content.Find.Execute(FindText:="^p^p", ReplaceWith:="^p", Replace:=Word.WdReplace.wdReplaceAll)
            objDoc.Content.Find.Execute(FindText:="^p", ReplaceWith:="", Replace:=Word.WdReplace.wdReplaceAll)

    • Marked as answer by jimbrown.net Monday, June 27, 2016 5:56 PM
    Saturday, June 25, 2016 3:05 AM
    Moderator
  • That did do what I asked,  thank you!  but it also looks like it took out formatting on lines with text.   thoughts? 


    Work Smarter Not Harder

    Saturday, June 25, 2016 12:16 PM
  • Hi jimbrown.ws,

    you can try to use the code below.

    I have check it will not remove formatting when you remove blank lines.

    Sub Removeline() 
        Do 
            With Selection.Find 
                .Text = "^p^p" 
                .Replacement.Text = "^p" 
                .Forward = True 
                .Wrap = wdFindContinue 
            End With 
        Loop Until Selection.Find.Execute(Replace:=wdReplaceAll) = False 
    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 Monday, June 27, 2016 5:56 PM
    Monday, June 27, 2016 2:12 AM
    Moderator