Solved

Delete whole line in word doc

Posted on 2008-10-16
3
1,143 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
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 …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now