saywell

MS Word RTF problem

10 posts in this topic

#1 ·  Posted (edited)

I've been creating word docs using AutoIT, with no problems, thinking they were rtfs as I'd created them with the .rtf suffix. However, I've discovered [see thread passim if you can be bothered!] that in fact they are just 'do files with .rtf file extension. IE in 'doc' format rather than 'rtf format'

I now need to use proper RTFs which I can now do [previously I'd not used the $i_Format = 6 in the save as command].

Howvever, I've hit a snag in the search/replace functionality.

What I need to do is a search and replace on a group of letters [from the template] and replace it with an address block - ie several lines of address.

When I do this, the line breaks don't carry over into the final document.

I've tried using @CR, @CRLF, @LF and @CR & &LF but none works.

Can anyone help Please?

Here is a simulator - I tried to upload the template, but apparently "You aren't permitted to upload this kind of file". It's just a blank word document with the word 'This' about half-way down the page, saved in .dot format.

; *******************************************************
; RTF Test - [delete previous example when restarting]
; Create a word window,
; open a document, find "This",
; replace all occurrences with "THIS", save in RTF format.
; open to view result
; *******************************************************
;
#include <Word.au3>
Opt("WinTitleMatchMode", 2)

FileDelete (@ScriptDir & "Test.rtf") ; to remove the previous attempt



$oWordApp = _WordCreate ("")

WinClose ("Document 1") ; close extra window spawned when opening Word

$Template = @ScriptDir & "RTFtest_letter.dot"
$oOpenTemplate = _WordDocAdd ($oWordApp,0,$Template)
$oDoc = _WordDocGetCollection($oWordApp, 0)

$sReplace = "Line 1" &@CR&"Line2"
$oFind = _WordDocFindReplace($oOpenTemplate, "This", $sReplace)
If $oFind Then
MsgBox(0, "FindReplace", "Found and replaced.",1)
Else
MsgBox(0, "FindReplace", "Not Found")
EndIf
$Template = @ScriptDir & "referral_letter.dot"
_WordDocSaveAs ($oOpenTemplate,@ScriptDir & "Test.rtf",6)
_WordQuit ($oWordApp, 0)

ShellExecute (@ScriptDir & "Test.rtf") ; to see the final result

Thanks,

William

Edited by saywell

Share this post


Link to post
Share on other sites



According to this site you need to set "^p" for a hard return (paragraph mark) or "^|" for a soft return (manual line breaks) in your replacement text.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks, Water.

I tried

$sReplace = "Line 1" &"^|"&"Line2"

In my test script, but it just threw up an error in the [included] <Word.au3> that crashed the test script.

William

Share this post


Link to post
Share on other sites

Just tried it myself. "^|" crashes but "^p" works just fine. Give it a try.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Indeed it does - you're a star!!

Many thanks.

William

Share this post


Link to post
Share on other sites

Don't exaggerate ;)

I'm just good with Google.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Just tried it myself. "^|" crashes but "^p" works just fine. Give it a try.

Hi

 

I am using AutoIT 3.3.12.0 and after i read a MS Word document via _Word_DocTableRead() and tried to detect soft-returns via "^|" i found that the detection doesn't work (i want to replace all soft-returns by hard-returns).

Is there any hint for me? :)

 

Best,

Thomas

Share this post


Link to post
Share on other sites

Can you please post your code?
I'm not sure you can determine if it is a hard or soft break after _Word_DocTableRead.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi

 

The code is

   

$i=2
    While $i<=$iTableEntries-1

        If StringInStr($sLine,"^|") Then ; this approach to soft-return detection doesn't detect 29.06.2015
            $sLine=StringReplace($sLine,"^|",@CR)

       EndIf

      $i+=1

  Wend

 

 

Share this post


Link to post
Share on other sites

Will need to test. But as I'm on vacation right now it will take some time.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now