How to perform a find ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rafio
Posts: 41
Joined: 07 Oct 2018, 05:14

How to perform a find ?

16 Oct 2018, 11:17

Hi,

I have a VBA macro to change text color in Publisher :

Code: Select all

Sub paraPuce()
    Dim objFind As FindReplace
    Dim fFound As Boolean
    
    ThisDocument.Selection.TextRange.Expand Unit:=pbTextUnitParagraph
    ThisDocument.Selection.TextRange.ParagraphFormat.TextStyle = "204-BODY PUCE"
    
    Set objFind = ActiveDocument.Selection.TextRange.Find
     
    With objFind
        .Clear
        .FindText = "Société"
         Do While .Execute = True
            .FoundTextRange.Font.Fill.ForeColor.SchemeColor = pbSchemeColorAccent1
         Loop
    End With
End Sub
I can't find how to convert it for AHK.
It's the Do Wile loop that causes me headaches.
If some one could help…
--
Windows 11
AHK 1.1.35.00
Adventure 3.0.4
eelrod
Posts: 65
Joined: 10 Apr 2018, 11:17

Re: How to perform a find ?

16 Oct 2018, 12:06

For the Do-While Loop in VBA, you can use While in AHK. Something like:

Code: Select all

While objFind.Execute
{
    objFind.FoundTextRange.Font.Fill.ForeColor.SchemeColor = pbSchemeColorAccent1
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to perform a find ?

16 Oct 2018, 12:27

Perhaps something like this:

Code: Select all

;AutoHotkey-scripts/ms office constants (all).ahk at master · jeeswg/AutoHotkey-scripts · GitHub
;https://github.com/jeeswg/AutoHotkey-scripts/blob/master/ms office constants (all).ahk
;TextRange.Expand Method (Publisher) | Microsoft Docs
;https://docs.microsoft.com/en-us/office/vba/api/publisher.textrange.expand

q::
pbSchemeColorAccent1 := 2
pbTextUnitParagraph := 4

oPub := ComObjActive("Publisher.Application")
oPub.ActiveDocument.Selection.TextRange.Expand(pbTextUnitParagraph)
oPub.ActiveDocument.Selection.TextRange.ParagraphFormat.TextStyle := "204-BODY PUCE"
oFind := oPub.ActiveDocument.Selection.TextRange.Find
oFind.Clear
oFind.FindText := "Société"
while oFind.Execute
	oFind.FoundTextRange.Font.Fill.ForeColor.SchemeColor := pbSchemeColorAccent1
oPub := oFind := ""
return
I had partial success with the code. Some things worked, some things didn't. (And I got sensible error messages.) I use MS Office 2007, so some features may not be available to me.

You can do this within Publisher to get the values for constants:

Code: Select all

MsgBox pbSchemeColorAccent1 '2
MsgBox pbTextUnitParagraph '4
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rafio
Posts: 41
Joined: 07 Oct 2018, 05:14

Re: How to perform a find ?

16 Oct 2018, 17:13

That’s perfect!
while oFind.Execute is concise just as I like.
Thank you guys.
--
Windows 11
AHK 1.1.35.00
Adventure 3.0.4
Rafio
Posts: 41
Joined: 07 Oct 2018, 05:14

Re: How to perform a find ?  Topic is solved

17 Oct 2018, 03:04

jeeswg wrote:
16 Oct 2018, 12:27
I had partial success with the code. Some things worked, some things didn't. (And I got sensible error messages.) I use MS Office 2007, so some features may not be available to me.
I missed this line.
Here is a working version:

Code: Select all

pubApp := ComObjActive("Publisher.Application")

pubApp.ActiveDocument.Selection.TextRange.Expand(Unit:=4)
pubApp.ActiveDocument.Selection.TextRange.ParagraphFormat.TextStyle := "204-BODY PUCE"
    
objFind := pubApp.ActiveDocument.Selection.TextRange.Find
    objFind.Clear
    objFind.FindText := "Société"
while objFind.Execute
	objFind.FoundTextRange.Font.Fill.ForeColor.SchemeColor := 2
pubApp := objFind := ""
return
--
Windows 11
AHK 1.1.35.00
Adventure 3.0.4

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 343 guests