Jump to content

Change Text in existing Word Doc


Recommended Posts

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)

Link to comment
Share on other sites

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Glad to be of service ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...