Problem in making the text bold in MS Word? Please Help.

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 311
Joined: 12 Apr 2015, 03:53

Problem in making the text bold in MS Word? Please Help.

05 Feb 2018, 12:15

for example i want that it should first search the text My Name in m s word document then it should replace it with Sabestian Caine and it should also make it into bold letters. my codes are these-

Code: Select all

f1::
ow:= ComObjActive("word.application")
st:= "My Name"
ow.selection.font.bold:= 1  ; this line is not working perhaps... 
ow.selection.find.execute(st,0,0,0,0,0,1,1,0,"Sabestian Caine",2)
return
Above codes are successfully searching the string My Name in ms world document and successfully replacing it with Sabestian Caine, but they are not making Sabestina Caine in Bold letters. I want that it should not only search and replace the string but it should also convert it into bold characters. please tell me what i am missing. thanks a lot..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
Odlanir
Posts: 274
Joined: 20 Oct 2016, 08:20

Re: Problem in making the text bold in MS Word? Please Help.

05 Feb 2018, 13:29

Code: Select all

f1::
ow:= ComObjActive("word.application")
st:= "My Name"
ow.selection.find.execute(st,0,0,0,0,0,1,1,0,"Sabestian Caine",2)
ow.Selection.Find.Text := "Sabestian Caine"
ow.Selection.Find.Execute
ow.selection.font.bold := 1   
ow.Selection.MoveRight(,1)
return
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
FanaticGuru
Posts: 1235
Joined: 30 Sep 2013, 22:25

Re: Problem in making the text bold in MS Word? Please Help.

05 Feb 2018, 19:39

Odlanir wrote:

Code: Select all

f1::
ow:= ComObjActive("word.application")
st:= "My Name"
ow.selection.find.execute(st,0,0,0,0,0,1,1,0,"Sabestian Caine",2)
ow.Selection.Find.Text := "Sabestian Caine"
ow.Selection.Find.Execute
ow.selection.font.bold := 1   
ow.Selection.MoveRight(,1)
return
This code did not work for me. It made the replacements but not the bold.

You can define the "Find" and "Replacement" text outside of the Execute.

Code: Select all

F4::
	wdApp := ComObjActive("word.application")
	wdApp.Selection.Find.Text := "My Name"
	wdApp.Selection.Find.ClearFormatting
	wdApp.Selection.Find.Replacement.Text := "Sabestian Caine"
	wdApp.Selection.Find.Replacement.ClearFormatting
	wdApp.Selection.Find.Replacement.Font.Bold := true
	wdApp.Selection.Find.Execute(,,,,,,,1,,,2) ; 1 for Wrap, 2 for Replace All
return
The ClearFormatting are not required but are good pratice. Without them the Execute uses any formatting defined in a previous call which if you were doing multiple replacements with different formatting would cause problems.

Of course with this technique you can do a lot more than just make it bold. You can define all kinds of properties of the replacement text. You could also define only replacing specifically formatted find text also.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
Sabestian Caine
Posts: 311
Joined: 12 Apr 2015, 03:53

Re: Problem in making the text bold in MS Word? Please Help.

06 Feb 2018, 04:19

FanaticGuru wrote:
Odlanir wrote:

Code: Select all

f1::
ow:= ComObjActive("word.application")
st:= "My Name"
ow.selection.find.execute(st,0,0,0,0,0,1,1,0,"Sabestian Caine",2)
ow.Selection.Find.Text := "Sabestian Caine"
ow.Selection.Find.Execute
ow.selection.font.bold := 1   
ow.Selection.MoveRight(,1)
return
This code did not work for me. It made the replacements but not the bold.

You can define the "Find" and "Replacement" text outside of the Execute.

Code: Select all

F4::
	wdApp := ComObjActive("word.application")
	wdApp.Selection.Find.Text := "My Name"
	wdApp.Selection.Find.ClearFormatting
	wdApp.Selection.Find.Replacement.Text := "Sabestian Caine"
	wdApp.Selection.Find.Replacement.ClearFormatting
	wdApp.Selection.Find.Replacement.Font.Bold := true
	wdApp.Selection.Find.Execute(,,,,,,,1,,,2) ; 1 for Wrap, 2 for Replace All
return
The ClearFormatting are not required but are good pratice. Without them the Execute uses any formatting defined in a previous call which if you were doing multiple replacements with different formatting would cause problems.

Of course with this technique you can do a lot more than just make it bold. You can define all kinds of properties of the replacement text. You could also define only replacing specifically formatted find text also.

FG





Thank u so much dear fanaticguru.... you are really great....

sir one more thing...

if i have to find and replace so many strings and convert them into bold then what should i do? for example-

Code: Select all

ow:= ComObjActive("word.application")
st:= "Date"
st1:= "Amount"
st2:= "Amount In Words"
st3:= "Cust Name"
st4:= "Cust ID"
st7:= "Office Name"
ow.selection.find.execute(st,0,0,0,0,0,1,1,0,"06.02.2018",2)
ow.selection.find.execute(st1,0,0,0,0,0,1,1,0,"80000",2)
ow.selection.find.execute(st2,0,0,0,0,0,1,1,0,"Eight Thousand Only",2)
ow.selection.find.execute(st3,0,0,0,0,0,1,1,0,"Sabestian Caine",2)
ow.selection.find.execute(st4,0,0,0,0,0,1,1,0,"S123456",2)
ow.selection.find.execute(st7,0,0,0,0,0,1,1,0,"XYZ Office",2)
Please help... Thanks...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
User avatar
SL5
Posts: 497
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Problem in making the text bold in MS Word? Please Help.

06 Feb 2018, 05:36

is this help you?:
c:=copyWord2clipBoard(doSelectLine)
https://github.com/sl5net/global-Intell ... nc.ahk#L15

p := getCaretPos()
https://github.com/sl5net/global-Intell ... nc.ahk#L15

may check the CaretPos, select the word, then make it bold?
FanaticGuru
Posts: 1235
Joined: 30 Sep 2013, 22:25

Re: Problem in making the text bold in MS Word? Please Help.

06 Feb 2018, 23:49

Sabestian Caine wrote:if i have to find and replace so many strings and convert them into bold then what should i do?
If I was going to do alot of replacements like this I would create a function for it.

Code: Select all

MSWord_BoldReplace("Date", "06.02.2018", "Amount", "80000") ; as many pairs as wanted

MSWord_BoldReplace(Arr*)
{
      wdApp := ComObjActive("Word.Application")
      wdApp.Selection.Find.ClearFormatting
      wdApp.Selection.Find.Replacement.ClearFormatting
      wdApp.Selection.Find.Replacement.Font.Bold := true
      n := 1
      while !(Arr[n] = "")
      {
            wdApp.Selection.Find.Text := Arr[n++]
            wdApp.Selection.Find.Replacement.Text := Arr[n++]
            wdApp.Selection.Find.Execute(,,,,,,,1,,,2)
      }
}
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
Sabestian Caine
Posts: 311
Joined: 12 Apr 2015, 03:53

Re: Problem in making the text bold in MS Word? Please Help.

07 Feb 2018, 01:51

FanaticGuru wrote:
Sabestian Caine wrote:if i have to find and replace so many strings and convert them into bold then what should i do?
If I was going to do alot of replacements like this I would create a function for it.

Code: Select all

MSWord_BoldReplace("Date", "06.02.2018", "Amount", "80000") ; as many pairs as wanted

MSWord_BoldReplace(Arr*)
{
      wdApp := ComObjActive("Word.Application")
      wdApp.Selection.Find.ClearFormatting
      wdApp.Selection.Find.Replacement.ClearFormatting
      wdApp.Selection.Find.Replacement.Font.Bold := true
      n := 1
      while !(Arr[n] = "")
      {
            wdApp.Selection.Find.Text := Arr[n++]
            wdApp.Selection.Find.Replacement.Text := Arr[n++]
            wdApp.Selection.Find.Execute(,,,,,,,1,,,2)
      }
}
FG

Outstanding!!!!!

thanks a lot dear FG....
codes provided by you are working awesome...
sir, could you please make me understand these codes-

MSWord_BoldReplace(Arr*) ; this is Variadic function i think. Please tell me why we need to use variadic function here?
{
wdApp := ComObjActive("Word.Application")
wdApp.Selection.Find.ClearFormatting
wdApp.Selection.Find.Replacement.ClearFormatting
wdApp.Selection.Find.Replacement.Font.Bold := true
n := 1
while !(Arr[n] = "") ; what is meaning of !(Arr[n] = "") here?
{
wdApp.Selection.Find.Text := Arr[n++] ; what is meaning of Arr[n++] in these two lines
wdApp.Selection.Find.Replacement.Text := Arr[n++]
wdApp.Selection.Find.Execute(,,,,,,,1,,,2)
}
}[/code]

please help me to understand these codes.. thanks a lot FG...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
FanaticGuru
Posts: 1235
Joined: 30 Sep 2013, 22:25

Re: Problem in making the text bold in MS Word? Please Help.

07 Feb 2018, 12:36

Sabestian Caine wrote:sir, could you please make me understand these codes-

Code: Select all

MSWord_BoldReplace(Arr*)  ; this is Variadic function i think. Please tell me why we need to use variadic function here?
{
      wdApp := ComObjActive("Word.Application")
      wdApp.Selection.Find.ClearFormatting
      wdApp.Selection.Find.Replacement.ClearFormatting
      wdApp.Selection.Find.Replacement.Font.Bold := true
      n := 1
      while !(Arr[n] = "") ; what is meaning of !(Arr[n] = "") here?
      {
            wdApp.Selection.Find.Text := Arr[n++]  ; what is meaning of Arr[n++] in these two lines
            wdApp.Selection.Find.Replacement.Text := Arr[n++]
            wdApp.Selection.Find.Execute(,,,,,,,1,,,2)
      }
}
please help me to understand these codes.. thanks a lot FG...
It is a function with a variadic parameter. This is so we can have a variable number of parameters passed to the function whether it be 2 or 20.

while !(Arr[n] = "") means to keep looping while the indexed element of the array is not blank. Basically keep looping through the array until you reach the end. The ! means "not".

wdApp.Selection.Find.Text := Arr[n++] n++ bascially means use the value of n then add 1 to n. ++n would mean add 1 to n then use the value.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
Sabestian Caine
Posts: 311
Joined: 12 Apr 2015, 03:53

Re: Problem in making the text bold in MS Word? Please Help.

09 Feb 2018, 00:20

FanaticGuru wrote:
Sabestian Caine wrote:sir, could you please make me understand these codes-

Code: Select all

MSWord_BoldReplace(Arr*)  ; this is Variadic function i think. Please tell me why we need to use variadic function here?
{
      wdApp := ComObjActive("Word.Application")
      wdApp.Selection.Find.ClearFormatting
      wdApp.Selection.Find.Replacement.ClearFormatting
      wdApp.Selection.Find.Replacement.Font.Bold := true
      n := 1
      while !(Arr[n] = "") ; what is meaning of !(Arr[n] = "") here?
      {
            wdApp.Selection.Find.Text := Arr[n++]  ; what is meaning of Arr[n++] in these two lines
            wdApp.Selection.Find.Replacement.Text := Arr[n++]
            wdApp.Selection.Find.Execute(,,,,,,,1,,,2)
      }
}
please help me to understand these codes.. thanks a lot FG...
It is a function with a variadic parameter. This is so we can have a variable number of parameters passed to the function whether it be 2 or 20.

while !(Arr[n] = "") means to keep looping while the indexed element of the array is not blank. Basically keep looping through the array until you reach the end. The ! means "not".

wdApp.Selection.Find.Text := Arr[n++] n++ bascially means use the value of n then add 1 to n. ++n would mean add 1 to n then use the value.

FG

Thanks you so much dear fanaticguru for making me understand these codes...

thank u so much once again...

so nice of you...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Xtra and 31 guests