CodeConvert
12-14-2023, 03:20 PM
Hi all,
Don't have any experience with VBA, but I need to create a quick macro that converts every other instance of string of characters in Word document. Below is what I have, which looks for every other double underscore "__" and adds a star to the end of one of them. Unfortunately, it only seems to work once per line, so it isn't counting them correctly. It also only works unreliably if the underscores are next to another character, such as around a word like __down__.
Sub Macro1()
i = 0
k = "__"
For Each k In ActiveDocument.Words
If (k = "__") Then
i = i + 1
k.Select
If (i Mod 2) = 0 Then
blanks = ""
For Counter = 1 To Len(k) - 1
blanks = blanks + "__*"
Next Counter
Word.Selection.TypeText (blanks)
End If
End If
Next k
End Sub
Using that code on this line:
_
$__
__Down__ __Hill__
__
^__
__ __ __
__
Produces this:
_
$__
__Down__ __*Hill__
__*
^__
__ __ __
__*
:
It should look like this:
_
$__
__*Down__ __*Hill__
__*
^__
__* __ __*
__
If someone could assist me with this that'd be awesome.
Thanks for any and all help!
Don't have any experience with VBA, but I need to create a quick macro that converts every other instance of string of characters in Word document. Below is what I have, which looks for every other double underscore "__" and adds a star to the end of one of them. Unfortunately, it only seems to work once per line, so it isn't counting them correctly. It also only works unreliably if the underscores are next to another character, such as around a word like __down__.
Sub Macro1()
i = 0
k = "__"
For Each k In ActiveDocument.Words
If (k = "__") Then
i = i + 1
k.Select
If (i Mod 2) = 0 Then
blanks = ""
For Counter = 1 To Len(k) - 1
blanks = blanks + "__*"
Next Counter
Word.Selection.TypeText (blanks)
End If
End If
Next k
End Sub
Using that code on this line:
_
$__
__Down__ __Hill__
__
^__
__ __ __
__
Produces this:
_
$__
__Down__ __*Hill__
__*
^__
__ __ __
__*
:
It should look like this:
_
$__
__*Down__ __*Hill__
__*
^__
__* __ __*
__
If someone could assist me with this that'd be awesome.
Thanks for any and all help!