How to copy a word doc, delete a sentence and paste it keeping format
Posted: 16 Mar 2018, 06:58
Hi guys, I have a script that opens a word document, copies the contents and then after deleting a sentence pastes the content on another document. I use com functions to do it and everything works fine until I use the line "oWord.Selection.TypeText("text")" which makes the document lose the format (italic). I switched that line to "send ^v" and that works, the format is kept.
The problem now is that I need to delete the first 100 characters of the document (or any given sentence) and when I use stringmid to delete what I need, the document loses format again even if I use ^v.
Is there any way I can do it without losing format?
The process would be:
- open or read a word document
- copy the whole text
- delete characters 1-100
- close that doc, open another word doc
- paste the text keeping the format
here's the code:
A second minor problem, though I could for now live with it, is that when I paste the text the margins are different from the text already existing in the new doc. I would need the new text to have the exact same margins as the previous text. Can that be done??
Any help would be much appreciated.
The problem now is that I need to delete the first 100 characters of the document (or any given sentence) and when I use stringmid to delete what I need, the document loses format again even if I use ^v.
Is there any way I can do it without losing format?
The process would be:
- open or read a word document
- copy the whole text
- delete characters 1-100
- close that doc, open another word doc
- paste the text keeping the format
here's the code:
Code: Select all
Run, test.doc
Sleep 2500
clipboard=
oWord := ComObjActive("Word.Application")
oDoc := oword.documents[1]
oDoc.Range.FormattedText.Copy
ClipWait
winclose, a ; close document
StringGetPos , delst,clipboard, firstword
StringMid ,text2del,clipboard,1,%delst%
run document2.doc
WinActivate ,ahk_class OpusApp
try
oWord := ComObjActive("Word.Application")
catch
return
oDoc := oword.documents[1]
oWord.Selection.Font.Bold := 1
oWord.Selection.TypeText("`n`nsometext")
oWord.Selection.Font.Bold := false
oWord.Selection.ParagraphFormat.Alignment := 3
oWord.Selection.TypeText("`n`n")
;~ oWord.Selection.TypeText("`n`n"newtext) ;THIS LOSES FORMAT
send ^v ;THIS LOSES FORMAT TOO AFTER USING STRINGMID, OTHERWISE IT WORKS
A second minor problem, though I could for now live with it, is that when I paste the text the margins are different from the text already existing in the new doc. I would need the new text to have the exact same margins as the previous text. Can that be done??
Any help would be much appreciated.