Server Totals
M'Chek - (3)
Elysia - (2)
Underdark - (1)
Deglos - (1)
Ferrell - (0)
Le'Or - (0)
Wilderness - (0)
Visimontium - (0)
Mikona - (0)
Total players: 7
Gallery

Links Menu

Need a VB/MSWord Macro Wiz

Moderators: Nighthawk4, Dungeon Masters

Need a VB/MSWord Macro Wiz

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

PostAuthor: 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
Silver Fox: I hear they've been stepping up attacks in Elysia.
Ulu Elexin: Hah, it's interesting that they are even drafting in fairies to fight them now.
Brant Risko: *snickers* Be careful... I'll tell mister Ang that ya called him a drafted fairy.
User avatar
RCon
Legacy DM
Legacy DM
 
Posts: 4719
Joined: Wed Sep 15, 2004 2:19 am
Location: New York, NY (GMT -5)

PostAuthor: 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: 5292
Joined: Mon Jan 10, 2005 2:06 pm
Location: Austin, Tx. (Central Time; GMT-6)

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

PostAuthor: 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
Silver Fox: I hear they've been stepping up attacks in Elysia.
Ulu Elexin: Hah, it's interesting that they are even drafting in fairies to fight them now.
Brant Risko: *snickers* Be careful... I'll tell mister Ang that ya called him a drafted fairy.
User avatar
RCon
Legacy DM
Legacy DM
 
Posts: 4719
Joined: Wed Sep 15, 2004 2:19 am
Location: New York, NY (GMT -5)

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

PostAuthor: 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
Silver Fox: I hear they've been stepping up attacks in Elysia.
Ulu Elexin: Hah, it's interesting that they are even drafting in fairies to fight them now.
Brant Risko: *snickers* Be careful... I'll tell mister Ang that ya called him a drafted fairy.
User avatar
RCon
Legacy DM
Legacy DM
 
Posts: 4719
Joined: Wed Sep 15, 2004 2:19 am
Location: New York, NY (GMT -5)

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


Return to NWN Scripting

Who is online

Registered users: Bing [Bot], Google [Bot]