Sign in to follow this  
Followers 0
BillyNelson

Change Text in existing Word Doc

5 posts in this topic

I copy and change the same 3 documents each week according to which customer I am working with. I have a script that creates a new directory and copies the documents. It renames the docs according to the customer name and now I want to add info into one particular document.

I have input boxes gathering data such as Project Start and End Dates and I want to auto populate the document with that info. I am having trouble figuring this out. I have everything working but the replacing of the text.

#include <Date.au3>

#include <File.au3>

#include <WindowsConstants.au3>

#NoTrayIcon

#include <Word.au3>

;------------------------------------------------------

; Getting Customer Name for Directory Creation

;------------------------------------------------------

$Cust = InputBox ("Customer Name", "Customer Name")

;------------------------------------------------------

; Create Directory for Document Storage

; -----------------------------------------------------

DirCreate ("D:\Documents\" & $Cust)

$Doclocale = ("D:\Documents\" &$Cust)

;------------------------------------------------------

; Copy Engagement Docs

;------------------------------------------------------

FileCopy ("C:\Users\bnelson\Downloads\ConsultingDocs\Documentation - Master\AllEngaments\*.*", $Doclocale)

FileMove ($DocLocale & "\Daily Status.xls", $DocLocale & "\" & $Cust & " Daily Status.xls")

FileMove ($DocLocale & "\Project Completion document.doc", $DocLocale & "\" & $Cust & " Project Completion document.doc")

FileMove ($DocLocale & "\Heclth Check.docx", $DocLocale & "\" & $Cust & " Health Check.docx")

;-------------------------------------------------------

; Edit Project Completion Document to reflect project information

;-------------------------------------------------------

$oProjDoc = ($DocLocale & "\" & $Cust & " Project Completion document.doc")

$oStartDate = Inputbox ("Start Date", "Start Date")

$oEndDate = InputBox ("End Date", "End Date")

$oLDConsultant = ("Billy Nelson")

$oSOWDate = InputBox ("SOW Date", "SOW Date")

$oWordApp = _WordCreate ($oProjDoc)

$oDoc = _WordDocGetCollection ($oWordApp, 0)

_WordDocFindReplace( $oProjDoc, "RefStartDate", $oStartDate)

_WordDocFindReplace( $oProjDoc, "RefEndDate", $oEndDate)

_WordDocFindReplace( $oProjDoc, "RefConsultant", $oLDConsultant)

_WordDocFindReplace( $oProjDoc, "RefSOWDate", $oSOWDate)

_WordDocSave ($oProjDoc)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Add some error checking so you know where and why your script fails:

; ...
$oWordApp = _WordCreate ($oProjDoc)
ConsoleWrite(@error & @CRLF)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
ConsoleWrite(@error & @CRLF)
_WordDocFindReplace( $oDoc, "RefStartDate", $oStartDate)
ConsoleWrite(@error & @CRLF)
_WordDocFindReplace( $oDoc, "RefEndDate", $oEndDate)
ConsoleWrite(@error & @CRLF)
_WordDocFindReplace( $oDoc, "RefConsultant", $oLDConsultant)
ConsoleWrite(@error & @CRLF)
_WordDocFindReplace( $oDoc, "RefSOWDate", $oSOWDate)
ConsoleWrite(@error & @CRLF)
_WordDocSave($oDoc)
ConsoleWrite(@error & @CRLF)

Edit:

Just noticed you get $oDOC returned by _WordDocGetCollection but then use $oProjDoc.

I changed my above example code to use $oDoc everywhere.

Edited by water

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

#3 ·  Posted (edited)

Water,

Thank you a million times over.

Edited by BillyNelson

Share this post


Link to post
Share on other sites

Glad to be of service ;)


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

Another approach is to have a Word template containing bookmarks, go to bookmarks in row and type text.

Small example:

global $oWordApp  = _wordcreate(@ScriptDir & "_template.doc",0 )
global $oDoc = _WordDocGetCollection($oWordApp,0)
global $w = $oDoc.Application

; move there
$oDoc.Bookmarks("whatisfor").Select
; insert content
$w.Selection.typetext("This is inserted at the bookmark")

Not sure how your milage in Word VBA is, but bookmarks can be both a "point" and "range".

What you choose is I guess not important. I like the template way because I "start from blank" each time.


I am just a hobby programmer, and nothing great to publish right now.

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
Sign in to follow this  
Followers 0