Page 1 of 1
Need a VB/MSWord Macro Wiz
Posted: Wed May 03, 2006 6:42 pm
by IceThorn
I'd like to greatly reduce the time it takes me to edit my logs by using a macro to delete the first 41 characters in each line. I don't know the VB for moving down a line and looping until EoF. Anyone know?
Then I can really fill up the forums with logs.
Posted: Wed May 03, 2006 7:20 pm
by RCon
I could whip something up to do this for you pretty quickly.
Or, if you already have the rest, and want to do this in Word, you can use a couple things. If you're using the Selection object, go with Selection.Move
To move the cursor to the beginning of the next paragraph:
Selection.Move wdParagraph, 1
Posted: Wed May 03, 2006 7:35 pm
by IceThorn
RCon wrote:I could whip something up to do this for you pretty quickly.
Or, if you already have the rest, and want to do this in Word, you can use a couple things. If you're using the Selection object, go with Selection.Move
To move the cursor to the beginning of the next paragraph:
Selection.Move wdParagraph, 1
Only thing I got is
Selection.Delete Unit:=wdCharacter, Count:=41
And I need to move down to the next line, which I guess is a paragraph (assuming each line ends with return).
I'd appreaciate any whipping you can do.
Posted: Wed May 03, 2006 10:50 pm
by IceThorn
Ok, I had:
Code: Select all
Sub RemoveFirst41()
'
' RemoveFirst41 Macro
'
Do While Not Selection.End
Selection.Delete Unit:=wdCharacter, Count:=41
Selection.Move wdParagraph, 1
Loop
End Sub
It was burning through the file and working well until about 50 pages into the 236 page file. Then it hung.

Posted: Wed May 03, 2006 11:58 pm
by RCon
Yeah, in your script, Selection.End won't evaluate the way you're thinking. End is a method of the Selection object, not a property. In the long run, using Selection object isn't the best way to manipulate documents, as the cursor may not always be where you think it is. Starting with the document object and working your way down may work better. Try this macro here (should be significantly faster too).
Note that the "If Len(objPAR..." line wraps to the next, even though it should be one statement in your code. Can't figure out how to do smaller fonts in a code block.
Code: Select all
Sub Test()
Dim objPAR As Word.Paragraph
' Cycle through all paragraphs in document
For Each objPAR In ActiveDocument.Paragraphs
If Len(objPAR.Range.Text) > 41 Then objPAR.Range.Text = Mid(objPAR.Range.Text, 42)
Next objPAR
End Sub
[/size]
Posted: Thu May 04, 2006 12:57 am
by IceThorn
Thanks for the quick response, and that doesn't crash. But if the line is > 41 characters, it deletes the 41 twice (or 82). How do I make it count 42 only once per line?
Posted: Thu May 04, 2006 3:21 am
by RCon
Crap, forgot it would do that - sometimes VB is dumb. Yeah, count backwards, like this:
Code: Select all
Sub Test()
Dim objPAR As Word.Paragraph
Dim i as Integer
' Cycle through all paragraphs in document
For i = ActiveDocument.Paragraphs.Count To 1 Step -1
Set objPAR = ActiveDocument.Paragraphs.Item(i)
If Len(objPAR.Range.Text) > 41 Then objPAR.Range.Text = Mid(objPAR.Range.Text, 42)
Set objPAR = Nothing
Next i
End Sub
Posted: Thu May 04, 2006 5:42 am
by IceThorn
That works! Thank you so much. It worked perfectly on a 1 page section of logs. It took about 3 full minutes for the full 280 pages of logs

It may have done a few weird things in the middle, but nothing important seemed to be missing. If I'd rememeber to turn off the combat info then everything's just perfect.
Thanks!
