Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delete whole line in word doc

Posted on 2008-10-16
3
Medium Priority
?
1,298 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 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

927 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