Solved

Delete whole line in word doc

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 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

11 Experts available now in Live!

Get 1:1 Help Now