Jump to content
Jury

Formatting uisng _Word_DocFindReplace

Recommended Posts

Jury

I've failed to find an example of _Word_DocFindReplace which searches for formatted text (I'm looking for stand alone paragraph marks that are formatted other than normal i.e. Bold Italic, Underlined). 

The reason being that when converting a Word document to html one of the main problems in the results is that a stand alone paragraph mark is converted to an html space that retains the formatting ...>&nbsp;<... thus showing up as a underline _  in a browser when it should be blank.  I've played around with the script and got it to at least un-bold  the first paragraph mark regardless if it was bold or not but I'd like to clear all formatting from any stand alone paragraph marks in the whole document.  Below is what I've done so far (not much more than in the help file I'm afraid) .  Way down at the bottom of the _Word_DocFindReplace  help  text is this parameter but without any examples to be found :

$bFormat   [optional] True to have the find operation locate formatting in addition to or instead of the find text (default = False)
#include <MsgBoxConstants.au3>
#include <Word.au3>

$processing = @MyDocumentsDir & '\AutoIt_code\getter\processing\'

Global $oWord = _Word_Create()
Global $sTestfile = $processing & "Testing.docx"
ConsoleWrite($sTestfile & @CRLF)
Global $oDoc = _Word_DocOpen($oWord, $sTestfile)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "ERROR", "Error opening file = '" & $sTestfile & "'" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
$oRangeFound = _Word_DocFind($oDoc, "^p", Default, Default)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _
        "Error locating paragraph control character in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
$oRangeFound.Bold = False
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _
        "Error inserting text after the paragraph control character in the document." & @CRLF & "@error = " & @error & _
        ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", "Paragraph control character successfully replaced." & @CRLF & _
        "Text inserted in paragraph 2.")

 

Edited by Jury

Share this post


Link to post
Share on other sites
water

We are having a similar discussion going on in another thread. At the moment I'm in the investigation stage.
As I have no Windows system available at the moment I need to check when I'm back in my office.

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Jury

Thanks, I don't know if this should be a new discussion be part of the other you mentioned.  I had hoped that the formatting of the searched for and replacement strings (^p in my case) could be handled by defining these things prior to doing the search replace something like (which doesn't change the Font from Bold to Normal as I'd hoped):

Global $oWord = _Word_Create()
Global $sTestfile = $processing & "Testing.docx"
ConsoleWrite($sTestfile & @CRLF)
Global $oDoc = _Word_DocOpen($oWord, $sTestfile)
With $oWord.Selection.Find
    .Text = "New Line"
    .Replacement.Font.Bold = False
    .Replacement.Text = "STEW LINE"
    .Forward = True
    .Wrap = $wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
EndWith
_Word_DocFindReplace($oDoc, $oWord.Selection.Find.Text, $oWord.Selection.Find.Replacement.Text, $wdReplaceAll)

 

Share this post


Link to post
Share on other sites
water

I'm fine with two threads. I just wanted to let you know that we are already investigating the subject.

Your approach doesn't work because _Word_DocFindReplace removes all formatting from the source and target range. I don't know why because I copied the function from somewhere else.

I will let you know as soon as we have found a solution.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

I just posted my working examples here:

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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

  • Similar Content

    • Jemboy
      By Jemboy
      Yesterday I had to make some little changes to an old Autoit program we use at my work.
      The program reads some data and convert it to Excel.
      Before writing the cell, it is changed to text and later on I slap the column with an autofitwidth.
      Furthermore weI execute a conditional format on the sheet, to make the data more readable.
      I quickly found out that because of the breaking changes Excel.udf had starting from AutoIt 3.3.12.0,
      a lot of things had to been changed.
      The changes I had to do, only took 10 minutes.
      After trying to adjust the script for over 5 hours, to get it working with the new Excel.udf, I gave up.
      I stopped changing the script, uninstalled the my Autoit and went looking for an older version.
      Luckilly I was able to find Autoit v3.3.8.1 (with corresponding Scite) in my software repository.
      Installing Autoit V3.3.8.1 and compiling the file, now took me  10 minutes .
       
      So why did I not get the old script working with the new Excel.udf?
      There are several reasons I failed getting the old script working with the new Excel UDF.
       I had some pressure from management to fix it ASAP (and got a little anxious)  Most all resources on the internet point to the working of the old EXcel.UDF And offcourse there were thosing "breaking changes",
      with new functions using diffrent parameters or using parameters in different order. One of the column's on the sheet is used to store EAN13 (barcode) and was formatted like 1,23E12.
      I couldn't change the cell to text, also autofitwidt was not working and using conditional formatting was also a no-no.
      So in the end I could use the new Excel UDF, but not desapointed management.
       
      What would I like to ask?
      I understand that sometimes you want to rewrite a program to make it better. I even understand that one has to make breaking changes sometimes.
      But in this case because of lacking examples/resources my day went completely down the drain.
      I would like to ask the Excel.udf developpers to:
      Make more functions available to do things like changing cell properties easily, changing cell color, do an autofit columnwidth, format data conditionally. 
        Or write an Example using the (new) Excel UDF, making examples how to format a cell, do conditional format, changing cell colors etc.
        I probably am more of an example guy.
      Having a good Excel.UDF Example showing a lot of common things normally makes, programming things easy for me.
      Because I can keep tweaking snippets until I get it working the way I want it.
       
      So dear developpers, could you help me and other future user out?
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    • lavascript
      By lavascript
      I have a Word document containing a 9-column table where row 1 is the column headers. My goal is to read the table into a 2d array, remove some rows, update some fields, and add a few rows to the end. The resulting array will likely be a different length. Next, I want to write the data back into the table. If it's easier, I can write the data to a new document from a template containing the same table header with a blank 2nd row.
      Here's my early attempt:
      Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, $sFile) Local $aData = _Word_DocTableRead($oDoc, 1) $aData[3][5] = "Something else" Local $oRange = _Word_DocRangeSet($oDoc, 0) $oRange = _Word_DocRangeSet($oDoc, $oRange, $wdCell, 9) _Word_DocTableWrite($oRange,$aData) This, unfortunately, writes the entire array into the first cell of row 2. What am I doing wrong?
       
    • Subz
      By Subz
      Backstory:
      Our Microsoft Office Templates shared folder was changed from a DFS share to an Isilon share. example:
      Old Server: \\Domain.com\Office\Templates
      New Server: \\Templates.domain.com\Office\Templates
      The team making the changes overlooked that several hundred thousand documents, had been attached to the old template documents.  So when you open a document which has been attached, it will take a couple of minutes to open, while it tries to locate the old server path.  I've been asked to come in and fix it, so after several hours found that the data is being held in document.zip\word\_rels\settings.xml.rels, I now need to replace the old server path with the new server path.  I didn't want to use dom as that would take too long and found a tool wtc https://github.com/NeosIT/wtc which  works perfectly, takes about 8 minutes to scan a single directory with 4000 documents and fix them.  The problem is the documents are all held on sharepoint and they want to retain the file timestamp, which is easy enough, but they also don't want to keep the "Modified By" apparently they don't like seeing all the documents appearing as "Modified by: Subz"  Anyone know of way to retain the "Modified By" info,
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
    • Benandro
      By Benandro
      Hello,
      im working on a Script that should change a high amount of Word Templates at once.
      Target is to open each Templatefile (.dotx) in a specific folder and do the following steps:
      Add a page break at the end of the document (works) Add a text on the created Page (works) Change the headerstyle to blank for the new page and the following (missing) Add a heading between two specific headings (missing) Can please someone help me to add the 2 functions to the script?
       
      #include <word.au3> #include <File.au3> #include <array.au3> ; wdGoToDirection Const $wdGoToNext = 2 ; wdGoToItem Const $wdGoToPage = 1 ; Created a logfile for tracking/error reporting on my local desktop, though anywhere would work. Needs to be changed or it will error. Global $LogFile = FileOpen("c:\logfiles\test.log", 1) ; This is the network path, change it or this will error as it is. ListFiles ("D:\Templates\") Global $loopend=$aFileList[0] ; Creates an instance of Word for the program to use. Logs any errors associated. Global $oWord = _Word_Create(False, False) If @error <> 0 Then Exit _FileWriteLog($LogFile, "Error creating a new Word application object. @error = " & @error & ", @extended = " & @extended & @crlf) If @extended = 1 Then _FileWriteLog ($LogFile, "MS Word was not running when _Word_Create was called." & @CRLF) Else _FileWriteLog ($LogFile, "MS Word was already running when _Word_Create was called." & @CRLF) EndIf ; Logs and begins loop _FileWriteLog ($LogFile, "Beginning Loop." & @CRLF) For $looper = 1 to $loopend Step +1 _FileWriteLog ($LogFile, "Modifying file: " & $aFileList[$looper], " ") OpenAndModify ("D:\Templates\" & $aFileList[$looper]) Next ; Closes instance of Word _Word_Quit ($oWord) _FileWriteLog ($LogFile, "Program Completed.") ; Begins Function section ; Two functions, Listfiles and OpenAndModify Func ListFiles($FolderPath) ; Function puts all files in the network folder into an array. Logs any errors. _FileWriteLog ($LogFile, "Getting File Information for: " & $FolderPath & @crlf) Global $aFileList = _FileListToArray($FolderPath, "*") If @error = 1 Then _FileWriteLog($LogFile, "Path was invalid." & @crlf) EndIf If @error = 4 Then _FileWriteLog ($LogFile, "No file(s) were found." & @crlf) EndIf EndFunc Func OpenAndModify ($sDocument) ; Function opens file and changes the Page Setup ; Opens the Document Local $oDoc = _Word_DocOpen ($oWord, $sDocument, Default, Default, Default) If @error <> 0 Then _FileWriteLog ($LogFile, "Error opening " & $sDocument & " @error = " & @error & ", @extended = " & @extended & @crlf) & Exit ; Changes Tray Settings ;$oDoc.PageSetup.FirstPageTray = 0 ;$oDoc.PageSetup.OtherPagesTray = 0 ; Add a link to the end of the document and set parameters ; ScreenTip and TextToDisplay Local $oRange = _Word_DocRangeSet($oDoc, -2); Go to end of document $oRange.InsertBreak($wdPageBreak) ;MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "Inserted a break.") $oRange.Text = "«Text»" ; Add a space at the end of the document $oRange = _Word_DocRangeSet($oDoc, -2) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocLinkAdd Example", _ "Error adding a link to the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocLinkAdd Example", "Baustein wurde an das Ende des Dokuments eingefügt.") ; Saves the document _Word_DocSave($oDoc) _FileWriteLog ($LogFile, "Modification of" & $sDocument & " complete." & @CRLF) EndFunc  
×