Need a VB/MSWord Macro Wiz

Moderator: Event DM

Post Reply
User avatar
IceThorn
Team Member; Retired with Honors
Posts: 5291
Joined: Mon Jan 10, 2005 2:06 pm
Location: Austin, Tx. (Central Time; GMT-6)
Contact:

Need a VB/MSWord Macro Wiz

Post by IceThorn » Wed May 03, 2006 6:42 pm

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.
User avatar
RCon
CCC
CCC
Posts: 4824
Joined: Wed Sep 15, 2004 2:19 am
Location: New York, NY (GMT -5)

Post by RCon » Wed May 03, 2006 7:20 pm

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
Paladin Date Night - One thing led to another...
User avatar
IceThorn
Team Member; Retired with Honors
Posts: 5291
Joined: Mon Jan 10, 2005 2:06 pm
Location: Austin, Tx. (Central Time; GMT-6)
Contact:

Post by IceThorn » Wed May 03, 2006 7:35 pm

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.
User avatar
IceThorn
Team Member; Retired with Honors
Posts: 5291
Joined: Mon Jan 10, 2005 2:06 pm
Location: Austin, Tx. (Central Time; GMT-6)
Contact:

Post by IceThorn » Wed May 03, 2006 10:50 pm

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. :(
User avatar
RCon
CCC
CCC
Posts: 4824
Joined: Wed Sep 15, 2004 2:19 am
Location: New York, NY (GMT -5)

Post by RCon » Wed May 03, 2006 11:58 pm

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]
Paladin Date Night - One thing led to another...
User avatar
IceThorn
Team Member; Retired with Honors
Posts: 5291
Joined: Mon Jan 10, 2005 2:06 pm
Location: Austin, Tx. (Central Time; GMT-6)
Contact:

Post by IceThorn » Thu May 04, 2006 12:57 am

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?
User avatar
RCon
CCC
CCC
Posts: 4824
Joined: Wed Sep 15, 2004 2:19 am
Location: New York, NY (GMT -5)

Post by RCon » Thu May 04, 2006 3:21 am

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
Paladin Date Night - One thing led to another...
User avatar
IceThorn
Team Member; Retired with Honors
Posts: 5291
Joined: Mon Jan 10, 2005 2:06 pm
Location: Austin, Tx. (Central Time; GMT-6)
Contact:

Post by IceThorn » Thu May 04, 2006 5:42 am

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! :D
Post Reply