Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete whole line in word doc

Posted on 2008-10-16
3
Medium Priority
?
1,266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

718 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