Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save QM code as a word file
#1
hello everyone,

I want to save the QM code as a word file

The following code, can achieve this goal,

Operation steps:

1. Open the QM code[Eg, test] to be saved, press Ctrl + A to select all, and then press the right mouse button to select Other Formats-->Copy HTML--->With Tabs and Spaces

2. run Macro9 , The created word file will be opened automatically

but there are a few places, not very convenient

1.
How to automate the first step above? Call a function to implement it?

2.
In word file, I need to delete the underline of tab stop, The method I use is to Set the color of the underline to white
Code Line13-16
but it has no effect, The result is HTML code

3.
In word file, I need to set the width of the tab stop,
Code Line8
but it has no effect

4.
In word file, Set the font
Courier New for all characters
Code Line9-11
but it has no effect

Is there a better solution? Any suggestions are welcome. Thanks in advance


[Image: pic.png]
Macro Macro9
Code:
Copy      Help
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._create

str WordDoc="$desktop$\\test.docx"
VARIANT vWordFile=WordDoc.expandpath
Word.Document doc = app.documents.Add()

doc.DefaultTabStop=app.Application.CentimetersToPoints(0.74) ;;Default tab width
app.Selection.Font.Name = "Courier New" 
app.Selection.Font.NameAscii = "Courier New" 
app.Selection.Font.Size = 14

;_s.getclip
;;out _s
;_s.findreplace("e0e0e0" "ffffff") ;;Set the underline color to white
;_s.setclip

app.Selection.Paste() ;;Paste text

doc.PageSetup.LeftMargin=app.CentimetersToPoints(1)
doc.PageSetup.TopMargin=app.CentimetersToPoints(1)
doc.PageSetup.RightMargin=app.CentimetersToPoints(1)
doc.PageSetup.BottomMargin=app.CentimetersToPoints(1)

doc.SaveAs(vWordFile)
app.Quit()

run vWordFile


Macro test
Code:
Copy      Help
int h = id(2210 "Quick Macros")
int y = 10
;Click 5 times, each time adding 16 to y
rep 5
,lef 50 y h
,0.5 ;;wait 0.5 s
,y + 16
;Select text
lef+ 20 15 h
lef- 20 80 h
#2
Now, in any QM code, pressing the ALT + W hotkey saves the code as a word file  Smile

There is only two problem left unsolved
1.Sets the color of tab underline
line 12-15

2.Set paragraph border to none
line 26-34
Does anyone know some details of converting VBA code into QM code? thanks a lot


Macro Macro9
Trigger Aw     Help - how to add the trigger to the macro
Code:
Copy      Help
int w1=id(2213 win("" "QM_Editor"))
men 33354 w1 ;;Copy HTML

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._create

str WordDoc="$desktop$\\test.docx"
VARIANT vWordFile=WordDoc.expandpath
Word.Document doc = app.documents.Add()

;_s.getclip
;;out _s
;_s.findreplace("e0e0e0" "ffffff") ;;Set the underline color to white
;_s.setclip

app.Selection.Paste() ;;Paste text

doc.Content.Font.Name = "Courier New" ;;Set Font
doc.Content.Font.NameAscii = "Courier New" 
doc.Content.Font.Size = 10

doc.Content.ParagraphFormat.TabStops.ClearAll ;;Set tab width
doc.DefaultTabStop=app.Application.CentimetersToPoints(0.74)

;;;;Selection.WholeStory
;;;;With Selection.ParagraphFormat
;;;;;;;;.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
;;;;;;;;.Borders(wdBorderRight).LineStyle = wdLineStyleNone
;;;;;;;;.Borders(wdBorderTop).LineStyle = wdLineStyleNone
;;;;;;;;.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
;;;;;;;;.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
;@Set paragraph border to none, Above is word VBA code
;doc.Content.ParagraphFormat.Borders(wdBorderLeft).LineStyle = wdLineStyleNone

doc.PageSetup.LeftMargin=app.CentimetersToPoints(1) ;;set margins
doc.PageSetup.TopMargin=app.CentimetersToPoints(1)
doc.PageSetup.RightMargin=app.CentimetersToPoints(1)
doc.PageSetup.BottomMargin=app.CentimetersToPoints(1)

doc.SaveAs(vWordFile)
app.Quit()

run vWordFile
#3
for  #1 and #2
 
Macro Macro36
Trigger Aw     Help - how to add the trigger to the macro
Code:
Copy      Help
men 33354 id(2213 _hwndqm) ;;Copy HTML

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._create

str WordDoc="$desktop$\\test.docx"
VARIANT vWordFile=WordDoc.expandpath
Word.Document doc = app.documents.Add()

_s.getclip("HTML Format")
_s.findreplace(".i{color:#e0e0e0;text-decoration:underline}" ".i{color:#ffffff;text-decoration:underline}") ;;Set the underline color to white
_s.setclip("HTML Format")

app.Selection.Paste() ;;Paste text

doc.Content.Font.Name = "Courier New" ;;Set Font
doc.Content.Font.NameAscii = "Courier New" 
doc.Content.Font.Size = 10

doc.Content.ParagraphFormat.TabStops.ClearAll ;;Set tab width
doc.DefaultTabStop=app.Application.CentimetersToPoints(0.74)

app.Selection.WholeStory()
app.Selection.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderRight).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderBottom).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderHorizontal).LineStyle = wdLineStyleNone
doc.Content.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle = wdLineStyleNone

doc.PageSetup.LeftMargin=app.CentimetersToPoints(1) ;;set margins
doc.PageSetup.TopMargin=app.CentimetersToPoints(1)
doc.PageSetup.RightMargin=app.CentimetersToPoints(1)
doc.PageSetup.BottomMargin=app.CentimetersToPoints(1)

doc.SaveAs(vWordFile)
app.Quit()

run vWordFile
#4
The job is perfect Smile . Thank you so much @kevin

This Macro can replace the QM printing function


@Kevin
QM use Com operate word faster than VBA code?
How to calculate execution time?
#5
measure using perf example
Code:
Copy      Help
long t1=perf
int i ii
for i 0 1000000
,ii= i
long t2=perf
out t2-t1;;difference in microseconds
;; convert to seconds
double d= t2 - t1
out d/1000000;;difference in seconds
#6
@Kevin
A lot of time is wasted loading word
Add an underline above the sub function, Is it difficult to achieve?
Smile
[Image: pic.png]
#7
How to convert the following VBA code into QM code?

 
Code:
Copy      Help
 
Sub Addpagenumber()
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
      Set rng = .Range
      rng.Text = "Page "
      rng.Collapse wdCollapseEnd
      ActiveDocument.Fields.Add rng, wdFieldPage, "Page"
      Set rng = .Range
      rng.Collapse wdCollapseEnd
      rng.Text = " of "
      rng.Collapse wdCollapseEnd
      ActiveDocument.Fields.Add rng, wdFieldNumPages, "Pages"
      Set rng = .Range
      rng.Collapse wdCollapseEnd
      rng.Text = " total"
      .Range.Fields.Update
      .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End Sub
#8
Code:
Copy      Help
Word.Range rng=doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range
rng.Text = "page "
rng.Collapse(wdCollapseEnd)
doc.Fields.Add(rng wdFieldPage "Page")
rng.Text = " of "
rng.Collapse(wdCollapseEnd)
doc.Fields.Add(rng wdFieldNumPages "Page")
rng.Collapse(wdCollapseEnd)
rng.Text = " total"
doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.Fields.Update
doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

Collapse([`&Direction]).
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
This parameter cannot be set
#9
somethings need to be  in a VARIANT

this works not sure if it is 100% correct.

Macro Macro36
Trigger Aw     Help - how to add the trigger to the macro
Code:
Copy      Help
Word.Range rng=doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range
rng.Text="page "
VARIANT FieldPage=wdFieldPage
VARIANT Direction=wdCollapseEnd
VARIANT FieldNumPages=wdFieldNumPages
rng.SetRange(rng rng)
rng.Collapse(Direction)
rng.Fields.Add(rng FieldPage)
rng.Collapse(Direction)
rng.SetRange(rng rng)
rng.InsertAfter(" of ")
rng.SetRange(rng rng)
rng.Collapse(Direction)
rng.Fields.Add(rng FieldNumPages)
rng.Collapse(Direction)
rng.SetRange(rng rng)
rng.InsertAfter(" Total ")
rng.SetRange(rng rng)
rng.Collapse(Direction)
doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.Fields.Update
doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
#10
@Kevin
The work is perfect. Thank you very much for your help Wink
This is a very good case for Word VBA code conversion to QM code


Macro Macro3
Code:
Copy      Help
men 33354 id(2213 _hwndqm) ;;Copy HTML

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application app._create

str WordDoc="$desktop$\\test.docx"
VARIANT vWordFile=WordDoc.expandpath
Word.Document doc = app.documents.Add()

_s.getclip("HTML Format")
_s.findreplace(".i{color:#e0e0e0;text-decoration:underline}" ".i{color:#ffffff;text-decoration:underline}") ;;Set the underline color to white
_s.setclip("HTML Format")

app.Selection.Paste() ;;Paste text

doc.Content.Font.Name = "Courier New" ;;Set Font
doc.Content.Font.NameAscii = "Courier New"
doc.Content.Font.Size = 10

doc.Content.ParagraphFormat.TabStops.ClearAll ;;Set tab width
doc.DefaultTabStop=app.Application.CentimetersToPoints(0.74)

app.Selection.WholeStory()
app.Selection.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderRight).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderBottom).LineStyle = wdLineStyleNone
app.Selection.ParagraphFormat.Borders.Item(wdBorderHorizontal).LineStyle = wdLineStyleNone
doc.Content.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle = wdLineStyleNone

doc.PageSetup.LeftMargin=app.CentimetersToPoints(1) ;;set margins
doc.PageSetup.TopMargin=app.CentimetersToPoints(1)
doc.PageSetup.RightMargin=app.CentimetersToPoints(1)
doc.PageSetup.BottomMargin=app.CentimetersToPoints(1)
doc.PageSetup.HeaderDistance = app.CentimetersToPoints(0.5)
doc.PageSetup.FooterDistance = app.CentimetersToPoints(0.5)

Word.Range rng=doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range ;;Add page number
rng.Text="page "
VARIANT FieldPage=wdFieldPage
VARIANT Direction=wdCollapseEnd
VARIANT FieldNumPages=wdFieldNumPages
rng.SetRange(rng rng)
rng.Collapse(Direction)
rng.Fields.Add(rng FieldPage)
rng.Collapse(Direction)
rng.SetRange(rng rng)
rng.InsertAfter(" of ")
rng.SetRange(rng rng)
rng.Collapse(Direction)
rng.Fields.Add(rng FieldNumPages)
rng.Collapse(Direction)
rng.SetRange(rng rng)
rng.InsertAfter(" Total ")
rng.SetRange(rng rng)
rng.Collapse(Direction)
doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.Fields.Update
doc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
doc.SaveAs(vWordFile)
app.Quit()

run vWordFile
#11
The following VBA code can add a horizontal line in front of the line where # sub is located
The following code sometimes works unsteadily, why?
________________________________________________________________________________

Word.Find f=app.Selection.Find
f.ClearFormatting
f.Text="#sub"
rep
    if(!f.Execute)
        break
    app.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle = wdLineStyleSingle

 
Code:
Copy      Help
 
Sub FindSubAddline()
    Selection.Find.ClearFormatting
    Selection.Find.Text = "#sub"
    Selection.Find.Execute
    Selection.ParagraphFormat.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
End Sub
#12
The above code, will search all the results #sub , there will be errors, like the following picture
How to use QM regular expressions to solve this problem?



[Image: pic.png]
#13
this should work for you uses wildcards
Code:
Copy      Help
Word.Find f=app.Selection.Find
f.ClearFormatting
f.Text="#sub *"
f.MatchWildcards = 1
rep
,if(!f.Execute)
,,break
,app.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle = wdLineStyleSingle

or maybe even better .. finds #sub that is a specific color
Code:
Copy      Help
Word.Find f=app.Selection.Find
f.ClearFormatting
f.Font.Color = 4210943
f.Text="#sub"
rep
,if(!f.Execute)
,,break
,app.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle = wdLineStyleSingle
#14
@kevin
thanks,
finds #sub that is a specific color
This method is good. How to get the value of color ?
f.Font.Color = 4210943
#15
i got the rgb value from qm and then did a macro record in word and looked at the value stored in the word macro
#16
Good idea Wink
#17
or can get the rgb value and run this macro to see the color value
Code:
Copy      Help
int red green blue color
red=255
green=64
blue=64

color=ColorFromRGB(red green blue)
out color

or can simply get the qm color value which is in format 0xBBGGRR and do this
Code:
Copy      Help
int color2=0x4040FF
out color2
#18
thanks, QM has many powerful functions Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)