Macro for deleting line....new logic now

B

Brad

I'm really new to VBA and I need to come up with a Word Macro that meets the
following logic to delete timestamps in my log files:

for instance my timestamp looks like

10:58:05 - 06/06/2005

So I think the logic would be....Delete the line if.....

First character is a "1" OR "0"
AND
Third character is a ":"
AND
Sixth character is a ":"

Any help would be appreciated!
 
J

Jezebel

It's not obvious what you're asking. Are you trying to work out how to do
pattern matching, or how to write a macro for the purpose. If you just want
to delete strings like your example, you can use Find and Replace. Enable
wildcards and search for

[0-9][0-9]:[!013]{1,}

and replace with nothing. You might need to experiment some to get the
pattern as you need it, andt o make sure you're not deleting anything else.
 
H

Helmut Weber

Hi Brad,

you are probably talking about deleting paragraphs,
which start with the pattern you indicated.
Not a beginner's question, even If it might seem to be simple.

Like this, if you can get it to work, you are not a beginner anymore:

Sub DeleteTimeDate()
Dim t As String ' time
Dim d As String ' date
Dim x As String ' time date seperator
Dim r As Range ' a range
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Range.Paragraphs
Set r = oPrg.Range
r.Start = oPrg.Range.Start
r.End = oPrg.Range.Start + 21
t = Left(r, 8)
d = Right(r, 10)
x = Mid(r, 9, 3)
If _
IsTime(t) And _
IsDate(d) And _
IsSepr(x) _
Then
oPrg.Range.Delete
End If
Next
End Sub

Public Function IsTime(t As String) As Boolean
IsTime = False
On Error GoTo Ende
If t = CStr(CDate(t)) Then
IsTime = True
End If
Ende:
End Function

Public Function IsDate(d As String) As Boolean
IsDate = False
On Error GoTo Ende
If d = CStr(CDate(d)) Then
IsDate = True
End If
Ende:
End Function

Public Function IsSepr(x As String) As Boolean
IsSepr = False
On Error GoTo Ende
If x = " - " Then IsSepr = True
Ende:
End Function

Note that regional setting are important,
and can make this very complicated.

Of course, you could use a wildcard search, too.
But to me, it is very hard to read sometimes.
To avoid all possible bugs in wildcardsearch
and disregard regional settings, you could build your own grammar,
like:
2digits:2digits:2digits - 2 digits/2digits/4 digits
and check afterwards whether all is a possible time and date.

Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
B

Brad

Basically I have a log file (.txt) file that I insert into word.....Sometimes
up to 100 pages.

In the log, everytime there is a command such as:

@Open File
10:58:05 - 06/06/2005

....

@Close File
10:58:05 - 06/06/2005


The timestamp is written underneath...We have about 50 macros that were
written 6 years ago to format the document a certain way but not a macro to
delete the 600 timestamps that clutter up the log.

Thanks for your help!

Jezebel said:
It's not obvious what you're asking. Are you trying to work out how to do
pattern matching, or how to write a macro for the purpose. If you just want
to delete strings like your example, you can use Find and Replace. Enable
wildcards and search for

[0-9][0-9]:[!013]{1,}

and replace with nothing. You might need to experiment some to get the
pattern as you need it, andt o make sure you're not deleting anything else.


Brad said:
I'm really new to VBA and I need to come up with a Word Macro that meets
the
following logic to delete timestamps in my log files:

for instance my timestamp looks like

10:58:05 - 06/06/2005

So I think the logic would be....Delete the line if.....

First character is a "1" OR "0"
AND
Third character is a ":"
AND
Sixth character is a ":"

Any help would be appreciated!
 
J

Jezebel

It's still not clear what your problem is. If you just want to delete the
timestamp lines, use a single Find and Replace statement, as suggested
previously.


Brad said:
Basically I have a log file (.txt) file that I insert into
word.....Sometimes
up to 100 pages.

In the log, everytime there is a command such as:

@Open File
10:58:05 - 06/06/2005

...

@Close File
10:58:05 - 06/06/2005


The timestamp is written underneath...We have about 50 macros that were
written 6 years ago to format the document a certain way but not a macro
to
delete the 600 timestamps that clutter up the log.

Thanks for your help!

Jezebel said:
It's not obvious what you're asking. Are you trying to work out how to do
pattern matching, or how to write a macro for the purpose. If you just
want
to delete strings like your example, you can use Find and Replace. Enable
wildcards and search for

[0-9][0-9]:[!013]{1,}

and replace with nothing. You might need to experiment some to get the
pattern as you need it, andt o make sure you're not deleting anything
else.


Brad said:
I'm really new to VBA and I need to come up with a Word Macro that
meets
the
following logic to delete timestamps in my log files:

for instance my timestamp looks like

10:58:05 - 06/06/2005

So I think the logic would be....Delete the line if.....

First character is a "1" OR "0"
AND
Third character is a ":"
AND
Sixth character is a ":"

Any help would be appreciated!
 
B

Brad

We want to automate the process. We have a Word template that hundreds of
our employees use all the time.

We have a "Format Log" button..it kicks off 50 macros in a row. I just want
to write a macro to include in this list.

Thanks.

Jezebel said:
It's still not clear what your problem is. If you just want to delete the
timestamp lines, use a single Find and Replace statement, as suggested
previously.


Brad said:
Basically I have a log file (.txt) file that I insert into
word.....Sometimes
up to 100 pages.

In the log, everytime there is a command such as:

@Open File
10:58:05 - 06/06/2005

...

@Close File
10:58:05 - 06/06/2005


The timestamp is written underneath...We have about 50 macros that were
written 6 years ago to format the document a certain way but not a macro
to
delete the 600 timestamps that clutter up the log.

Thanks for your help!

Jezebel said:
It's not obvious what you're asking. Are you trying to work out how to do
pattern matching, or how to write a macro for the purpose. If you just
want
to delete strings like your example, you can use Find and Replace. Enable
wildcards and search for

[0-9][0-9]:[!013]{1,}

and replace with nothing. You might need to experiment some to get the
pattern as you need it, andt o make sure you're not deleting anything
else.


I'm really new to VBA and I need to come up with a Word Macro that
meets
the
following logic to delete timestamps in my log files:

for instance my timestamp looks like

10:58:05 - 06/06/2005

So I think the logic would be....Delete the line if.....

First character is a "1" OR "0"
AND
Third character is a ":"
AND
Sixth character is a ":"

Any help would be appreciated!
 
D

David Sisson

This assumes the timelog is always under the Open/Close file entry.
It also assumes there are no other ampersands.

Sub TimeStampDelete()

Dim Rng, Rng2 As Range
Set Rng = ActiveDocument.Range
Set Rng2 = Rng.Duplicate

'Goto start of Story
Rng2.StartOf wdStory
Do
'Find ampersand
With Rng2.Find
.ClearFormatting
.Text = Chr$(64)
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Rng2.Find.Found Then

'Move range back to the beginning of the paragraph/line
Rng2.EndOf wdParagraph

'Move down one line
Rng2.MoveEnd wdParagraph, 1

'MsgBox Rng2
Rng2.Delete
'Move range to exclude last find
Rng2.Start = Rng2.End + 1
End If
Loop Until Not Rng2.Find.Found
End Sub
 

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