Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use QM's regular expression to replace text in word
#1
Microsoft's word software, the wildcard is not standardized, how to use QM regular expression function, in word: to find, and replace the text operation?

in QM software help, the find, replace of the example, are the text of the operation, the text is not format! But in the word operation can be retained format!

I think the QM code is easier to understand than the VBA code, and many people should try it Idea

I hope that QM developers can help me provide some examples, for example, how to convert the following VBA code into QM code, thank you very much Smile

I have just been in contact with programming less than two months,I think that a representative example can quickly improve the programming level Tongue


1.VBA:(Find replacement of define content)

With Selection.Find
.ClearFormatting
.Text = "hi"
.Replacement.ClearFormatting
.Replacement.Text = "hello"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With


2.VBA:(Find replacement of uncertain content)

For example, in the above code, find: hi123 hi4568 yshi8 and replace it with: hello


3.VBA:(Find replacement format)

With ActiveDocument.Content.Find
.ClearFormatting
.Font.Bold = True
With .Replacement
.ClearFormatting
.Font.Bold = False
End With
.Execute FindText:="", ReplaceWith:="", _
Format:=True, Replace:=wdReplaceAll
End With

4.VBA:(To manipulate specific words in the entire Microsoft Word document)

Const wdReplaceAll = 2
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
Set objSelection = objWord.Selection
objSelection.Find.Text = "Fabrikam"
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = TRUE
objSelection.Find.Replacement.Font.Bold = True
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
#2
Quote:1.VBA:(Find replacement of define content)

With Selection.Find
.ClearFormatting
.Text = "hi"
.Replacement.ClearFormatting
.Replacement.Text = "hello"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
Macro Find and Replace 1
Code:
Copy      Help
;/exe 1

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive
app.Selection.Find.ClearFormatting()
app.Selection.Find.Text = "hi"
app.Selection.Find.Replacement.ClearFormatting()
app.Selection.Find.Replacement.Text = "hello"
VARIANT vForward=1
VARIANT vWrap=Word.wdFindContinue
VARIANT vReplaceAll=Word.wdReplaceAll
app.Selection.Find.Execute(@ @ @ @ @ @ vForward vWrap @ @ vReplaceAll)
app.ActiveDocument.Save
#3
Quote:2.VBA:(Find replacement of uncertain content)

For example, in the above code, find: hi123 hi4568 yshi8 and replace it with: hello
Macro Find and Replace 2
Code:
Copy      Help
;/exe 1

str wordList=
;hi123
;hi4568
;yshi8

str wordFind
foreach wordFind wordList
,sub.FindandReplace wordFind "hello"

#sub FindandReplace
function str'searchStr str'replaceStr

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive
app.Selection.Find.ClearFormatting()
app.Selection.Find.Text = searchStr
app.Selection.Find.Replacement.ClearFormatting()
app.Selection.Find.Replacement.Text = replaceStr
VARIANT vForward=1
VARIANT vWrap=Word.wdFindContinue
VARIANT vReplaceAll=Word.wdReplaceAll
app.Selection.Find.Execute(@ @ @ @ @ @ vForward vWrap @ @ vReplaceAll)
app.ActiveDocument.Save
#4
Quote:3.VBA:(Find replacement format)

With ActiveDocument.Content.Find
.ClearFormatting
.Font.Bold = True
With .Replacement
.ClearFormatting
.Font.Bold = False
End With
.Execute FindText:="", ReplaceWith:="", _
Format:=True, Replace:=wdReplaceAll
End With
Macro Find and Replace 3
Code:
Copy      Help
exe 1

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive
app.ActiveDocument.Content.Find.ClearFormatting()
app.ActiveDocument.Content.Find.Font.Bold=1
app.ActiveDocument.Content.Find.Replacement.ClearFormatting()
app.ActiveDocument.Content.Find.Replacement.Font.Bold=0
app.ActiveDocument.Content.Find.Replacement.Text = replaceStr
VARIANT vFindText = "hi"
VARIANT vReplaceWith= "hello"
VARIANT vFormat=1
VARIANT vReplaceAll=Word.wdReplaceAll
app.ActiveDocument.Content.Find.Execute(vFindText @ @ @ @ @ @ @ vFormat vReplaceWith vReplaceAll)
app.ActiveDocument.Save
#5
Quote:4.VBA:(To manipulate specific words in the entire Microsoft Word document)

Const wdReplaceAll = 2
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
Set objSelection = objWord.Selection
objSelection.Find.Text = "Fabrikam"
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = TRUE
objSelection.Find.Replacement.Font.Bold = True
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
Macro Find and Replace 4
Code:
Copy      Help
;/exe 1

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._create
Word.Documents doc=app.Documents
str WordDoc="$desktop$\Scripts\Test.docx"
VARIANT vWordFile=WordDoc.expandpath
doc.Open(vWordFile)
app.Selection.Find.Text = "Fabrikam"
app.Selection.Find.Forward=1
app.Selection.Find.MatchWholeWord=1
app.Selection.Find.Replacement.Font.Bold=1
VARIANT vReplace=Word.wdReplaceAll
app.Selection.Find.Execute(@ @ @ @ @ @ @ @ @ @ vReplace)
app.ActiveDocument.Save
app.ActiveDocument.Close
app.Quit
#6
Thank you very much, you must be an office automation expert Smile

These examples I have to think about it carefully Tongue

In addition, I would like to ask, how to use QM software replacerx function to achieve the above operation? I feel the word software to find the replacement function is too long-winded, the code is relatively long Tongue


Attached Files Image(s)
   
#7
can You use the (pointer and array and replacerx) three functions to solve the above problems? the following picture is an example, the feeling is a good idea, Idea but I understand a bit difficult, poor programming level :oops:


Attached Files Image(s)
   
#8
How to solve the vba code used in QM, too repetitive problem? I can use the with statement in VBA Tongue


Attached Files Image(s)
   
#9
First off, I'm not good at regular expression so I could not help you much on this.
Secondly I know your point about the "so-much wording" in Word and how to reduce them similar as VBA code does. Frankly speaking I have no glue how to do it, and I think that's one of the advantages of VBA code to have such keyword as "With" or "Using", etc. But I have no clue how to implement it in QM2.
#10
Indeed, as you said, each programming language has its own advantages, I now think of a way to VBA code into VBS code, and then run the VBS code in QM, this may be a good choice, Like the following code Idea

Thank you very much for your help and advice, best wishes Smile

Examples
str code=
msgbox 1
msgbox 2
VbsExec code
#11
Do you know what tool software I can convert word VBA code into VBS code? If so, that's great Tongue
#12
No, I don't, since I don't use much of VBS either.
#13
I tried QM to find the replacement function, success, QM too powerful, than Word vba lookup replacement function, much more powerful :lol:
_________________________________________

Find the character and format it of code:

Macro Macro2
Code:
Copy      Help
;/exe 1
out

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive ;;connect to Word. Note: need the /exe 1.
Word.Document doc=app.ActiveDocument
str s=doc.Content.Text
;out s
s.findreplace("[13]" "[10]") ;;Word text newlines are [13]
str rx=
;(?xm)
;help
ARRAY(POINT) a; int i
if(!findrx(s rx 0 4 a)) end "failed, regular expression"
for i 0 a.len
,VARIANT v1(a[0 i].x) v2(a[0 i].y)
,Word.Range ran=doc.Range(v1 v2)
,ran.Bold=1
,ran.Font.Size=15

;BEGIN PROJECT
;main_function  Macro2
;exe_file  $my qm$\Macro2.qmm
;flags  6
;guid  {B5CA9DC3-252F-4349-AB94-A08505C19716}
;END PROJECT
#14
The above code is to find specific characters, but how to find a specific format? Hope that someone can help me, any suggestions and comments are welcome Tongue

I think there must be a way to solve this problem, my programming level is too poor, I hope the developer can guide me, thank you very much Smile

These examples are very valuable, and I hope that the developer will write this example to the QM help file, and I would like the office staff to see it and improve office productivity Big Grin
#15
kyjdp Wrote:I tried QM to find the replacement function, success, QM too powerful, than Word vba lookup replacement function, much more powerful :lol:

Macro Macro2
Code:
Copy      Help
;/exe 1
out

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive ;;connect to Word. Note: need the /exe 1.
Word.Document doc=app.ActiveDocument
str s=doc.Content.Text
;out s
s.findreplace("[13]" "[10]") ;;Word text newlines are [13]
str rx=
;(?xm)
;help
ARRAY(POINT) a; int i
if(!findrx(s rx 0 4 a)) end "failed, regular expression"
for i 0 a.len
,VARIANT v1(a[0 i].x) v2(a[0 i].y)
,Word.Range ran=doc.Range(v1 v2)
,ran.Bold=1
,ran.Font.Size=15

;BEGIN PROJECT
;main_function  Macro2
;exe_file  $my qm$\Macro2.qmm
;flags  6
;guid  {B5CA9DC3-252F-4349-AB94-A08505C19716}
;END PROJECT

______________________________________________
Replace the text of the code

Macro Macro2
Code:
Copy      Help
;/exe 1
out

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive ;;connect to Word. Note: need the /exe 1.
Word.Document doc=app.ActiveDocument
str s=doc.Content.Text
;out s
s.findreplace("[13]" "[10]") ;;Word text newlines are [13]
str rx=
;(?xm)
;help
ARRAY(POINT) a; int i
if(!findrx(s rx 0 4 a)) end "failed, regular expression"
for i 0 a.len
,VARIANT v1(a[0 i].x) v2(a[0 i].y)
,Word.Range ran=doc.Range(v1 v2)
,ran.Text="HELP"

Use the pointer to replace, when the replacement character is very long is unsuccessful, :oops:
#16
I tried the following method, find and replace the format, the result is a failure :oops:

Macro Macro2
Code:
Copy      Help
;/exe 1
out

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._getactive ;;connect to Word. Note: need the /exe 1.
Word.Document doc=app.ActiveDocument
str s=doc.Content.Text
;out s
s.findreplace("[13]" "[10]") ;;Word text newlines are [13]
str rx=
;;(?xm)
;;help
ARRAY(POINT) a; int i
if(!findrx(s rx 0 4 a)) end "failed, regular expression"
for i 0 a.len
,VARIANT v1(a[0 i].x) v2(a[0 i].y)
,Word.Range ran=doc.Range(v1 v2)
,ran.Find.Font.Bold=1
,ran.Find.Replacement.Font.Bold=0


Forum Jump:


Users browsing this thread: 1 Guest(s)