Macro for deleting specific line

B

Brad

I'm very new VBA and was given the task to get rid of timestamps in our log
files.

I would like to do that in word with a macro...basically our log file looks
like:

@Open File
03:15:45 6/6/05

......

@Close File
03:15:47 6/6/05

The logic to my macro is:

1) Find the @ symbol
2) Go down 1 line
3) Delete that line if the line starts with a "0" or "1"
4) Look for the next @ symbol and repeat

I was using WordBasic.EditClear statements since they are used in other
macros in the template but this is different from all the other existing
macros which brings into play new coding and problems for a beginner.

Thanks for any help!
 
J

Jay Freedman

Hi Brad,

Use this macro. It does the job in pretty much the way you proposed.

Sub DeleteTimestamps()
Dim oRg As Range, oRg2 As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.Text = "@"
.Format = False
.Forward = True
.Wrap = wdFindStop

On Error GoTo Bye
Do While .Execute
Set oRg2 = oRg.Paragraphs(1).Next.Range
If (oRg2.Characters(1) = "0") Or _
(oRg2.Characters(1) = "1") Then
oRg2.Delete
End If
oRg.Collapse direction:=wdCollapseEnd
Loop
End With

Bye:
End Sub

The range oRg is used to find the "@" in a loop. Each time it's found,
the .Execute method returns the value True and the macro enters the
inner part of the loop.

Inside, the range oRg2 is assigned to point to the paragraph after the
one that contains the "@". (Note that this goes by paragraphs, so if
the lines are separated by line breaks -- Shift+Enter -- then this
isn't going to work.)

Then the first character of oRg2 is tested, and if it's a 0 or a 1
then that paragraph is deleted.
 
G

Greg

Brad,

For a non-macro solution, you could try a Find and Replace using
wildcards:

Find:
(\@<*> File^13)([0-1]*^13)
Replace with
\1
 
B

Brad

Jay:

Thanks!! Macro works great

Jay Freedman said:
Hi Brad,

Use this macro. It does the job in pretty much the way you proposed.

Sub DeleteTimestamps()
Dim oRg As Range, oRg2 As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.Text = "@"
.Format = False
.Forward = True
.Wrap = wdFindStop

On Error GoTo Bye
Do While .Execute
Set oRg2 = oRg.Paragraphs(1).Next.Range
If (oRg2.Characters(1) = "0") Or _
(oRg2.Characters(1) = "1") Then
oRg2.Delete
End If
oRg.Collapse direction:=wdCollapseEnd
Loop
End With

Bye:
End Sub

The range oRg is used to find the "@" in a loop. Each time it's found,
the .Execute method returns the value True and the macro enters the
inner part of the loop.

Inside, the range oRg2 is assigned to point to the paragraph after the
one that contains the "@". (Note that this goes by paragraphs, so if
the lines are separated by line breaks -- Shift+Enter -- then this
isn't going to work.)

Then the first character of oRg2 is tested, and if it's a 0 or a 1
then that paragraph is deleted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top