Thanks for the help so far guys. When I try to use the For loop, I'm getting an error (see image) with description: '_NewEnum' is not a method. I've tried with and without the "each" but I'm getting the same error in both cases.
I'm not sure what to do from here. I've posted the actual script I'm trying to run (see below), changing red text to black text throughout a word document with multiple sections. The original VBA code, which is functioning as desired, is based on guidance from this site:
http://word.mvps.org/faqs/macrosvba/Fin ... ithVBA.htm
AHK Testing Script:
Code: Select all
#InstallKeybdHook
#InstallMouseHook
Loop
{
sleep 100
#SingleInstance Force
}
Numpad6::
Reload
Numpad5::
ExitApp
Numpad1::
Current1()
return
Numpad2::
Current2()
return
Current1()
{
Testing1()
return
}
Current2()
{
Testing2()
return
}
;****TTMsg****
TTMsg(Message, Timer) ;example TTMsg("Testing", 1000)
{
MouseGetPos ttX, ttY
ToolTip, %Message%, ttX + 50, ttY + 50
SetTimer, RemoveToolTip, %Timer%
return
}
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return
;----TTMsg----
Testing1()
{
oWord := ComObjActive("Word.Application")
For myStoryRange, In oWord.ActiveDocument.StoryRanges
{
findObject := myStoryRange.Find
findObject.Text := ""
findObject.Font.Color := "255"
findObject.Replacement.Font.Color := "0"
findObject.Replacement.Text := ""
findObject.Forward := 1
findObject.Wrap := 1
findObject.Execute(,,,,,,,,,,2)
While !(myStoryRange.NextStoryRange = "")
{
myStoryRange := myStoryRange.NextStoryRange
findObject := myStoryRange.Find
findObject.Text := ""
findObject.Font.Color := "255"
findObject.Replacement.Font.Color := "0"
findObject.Replacement.Text := ""
findObject.Forward := 1
findObject.Wrap := 1
findObject.Execute(,,,,,,,,,,2)
}
}
return
}
Testing2()
{
oWord := ComObjActive("Word.Application")
For Each, myStoryRange In oWord.ActiveDocument.StoryRanges
{
findObject := myStoryRange.Find
findObject.Text := ""
findObject.Font.Color := "255"
findObject.Replacement.Font.Color := "0"
findObject.Replacement.Text := ""
findObject.Forward := 1
findObject.Wrap := 1
findObject.Execute(,,,,,,,,,,2)
While !(myStoryRange.NextStoryRange = "Nothing")
{
myStoryRange := myStoryRange.NextStoryRange
findObject := myStoryRange.Find
findObject.Text := ""
findObject.Font.Color := "255"
findObject.Replacement.Font.Color := "0"
findObject.Replacement.Text := ""
findObject.Forward := 1
findObject.Wrap := 1
findObject.Execute(,,,,,,,,,,2)
}
}
return
}
VBA Code that is functioning properly:
Code: Select all
Sub wReColor(wColorFind, wColorReplace)
Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = ""
.Font.Color = wColorFind
.Replacement.Text = ""
.Replacement.Font.Color = wColorReplace
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = ""
.Font.Color = wColorFind
.Replacement.Text = ""
.Replacement.Font.Color = wColorReplace
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
End Sub
Again, I appreciate any help with this, thanks.