Solved

Delete whole line in word doc

Posted on 2008-10-16
3
1,179 Views
Last Modified: 2012-05-05
Experts -

Using VB.Net I'm trying to delete a whole line in a word doc. I found the following function, but I can't get it to work! Any help would be appreciated. I would like the function to simply pass in the word doc that I'm already accessing, for example something like:

Private sub DeleteLine(ByRef doc As Object)

Anyways, below is the function I found, but I can't get it to work. I would like to delete the whole line where the word "Filed:" appears.

Sub RemoveWelshLines()
Selection.HomeKey Unit:=wdStory
Selection.Find.Text = "Filed:"
Do While Selection.Find.Execute(FindText:="Filed:", Forward:=True, _
            Format:=True) = True
    With Selection
            .Expand Unit:=wdLine
            .Delete
    End With
Loop

TIA!
0
Comment
Question by:crafuse
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 22733960
Do you get any errors, or does it not find anything?

I don't think that the unqualified Selection object helps.

See if this works (I don't have .Net to run a full test)

Sub RemoveWelshLines(doc As Object)
Dim wdApp As Object 'Word.Application
'Dim doc As Word.Document
'Set wdApp = doc.Application
wdApp = doc.Application
doc.Range.Select
wdApp.Selection.Find.Text = "Filed:"
Do While wdApp.Selection.Find.Execute(FindText:="Filed:", Forward:=True, _
            Format:=True) = True
    With wdApp.Selection
            .Expand Unit:=wdLine
            .Delete
    End With
Loop
End Sub

Open in new window

0
 

Author Comment

by:crafuse
ID: 22734179
Sweet!

Changed it to the following and now works like a charm!

Sub RemoveWelshLines(ByVal doc As Object)

        Dim wdApp As Object 'Word.Application
        Dim Unit1 As Microsoft.Office.Interop.Word.WdUnits = Microsoft.Office.Interop.Word.WdUnits.wdLine
        'Dim doc As Word.Document
        'Set wdApp = doc.Application
        wdApp = doc.Application
        doc.Range.Select()
        wdApp.Selection.Find.Text = "Filed:"
        Do While wdApp.Selection.Find.Execute(FindText:="Filed:", Forward:=True, _
                    Format:=True) = True
            With wdApp.Selection
                .Expand(Unit1)
                .Delete()
            End With
        Loop
    End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22734464
That's good news. Thanks and good luck with the rest of the project.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Outlook Free & Paid Tools
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question