Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2005

Tip: Looking for answers? Try searching our database.

Macro for deleting line....new logic now

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brad - 20 Jun 2005 22:10 GMT
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!
Jezebel - 21 Jun 2005 09:30 GMT
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
[quoted text clipped - 13 lines]
>
> Any help would be appreciated!
Brad - 22 Jun 2005 19:20 GMT
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!

> 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
[quoted text clipped - 23 lines]
> >
> > Any help would be appreciated!
Jezebel - 23 Jun 2005 08:40 GMT
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.

> Basically I have a log file (.txt) file that I insert into
> word.....Sometimes
[quoted text clipped - 47 lines]
>> >
>> > Any help would be appreciated!
Brad - 23 Jun 2005 17:29 GMT
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.

> 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
[quoted text clipped - 51 lines]
> >> >
> >> > Any help would be appreciated!
David Sisson - 23 Jun 2005 19:57 GMT
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
Helmut Weber - 21 Jun 2005 12:48 GMT
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"
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.