Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to update form fields in Word Document via script

This one could likely raise a lot of questions, so I will try to be as specific as possible. I am in charge of supporting a third party application that my job uses. One of the functions that this application does is pull up word documents from .dot files with information from a database pulled in. It uses MERGEFIELD fields to do this. When this data is pulled in, it allows normal date fields and printdate fields to remain, but createdate (the one I need) and savedate fields are totally messed up. It keeps the original template creation date there instead of inserting today's date as the document creation date like templates normally do, and invalidates the field so that it is plain text instead of a createdate field, so you can't update it. I couldn't find an exact answer on whether or not this is standard behavior when createdate fields are used with mail merge. I would use normal dates, but these documents could be edited over the course of a few days, with it finally being converted to PDF for a "permanent" copy. If you create the document from the program then open it the next day, it automatically updates the date...not what we want, needs to be that first day that it was opened.

I had an epiphany to use a powershell script to update the createdate of the template itself. Managed to do this in three lines with:

$a = Get-Date
$b = Get-Item "Q:\CUSTOM\DATETEST.dot"
$b.CreationTime = $a

This script works great, and the file information is updated. However, to get the actual words in the document to reflect this, you have to go into Word, refresh the fields in the form with F9, then save it. Even with the updated creation date, the word document text still shows the old date when you pull up the document in Word or from the other application until you refresh and manually save the templates. These forms aren't protected, so that is not an issue. I saw a few powershell scripts to edit word files, but couldn't exactly find one to refresh date fields. I tried this script with the modify date and using savedate fields in the form, but this also required going in and actually saving the form. Is there a way in powershell or batch (or any scripting language, don't care at this point) that would enable me to refresh the form fields in the documents and then save them, or can anyone think of any other way around this? I have several documents that would need this done, so doing it manually every morning is also impractical.

EDIT: I found the following code, as commented below. In this code I also found that I needed to change Documents.Add to Documents.Open. I will note that this worked for getting the save date manipulated the way I needed, but createdate still remains a mystery.

$word=new-object -com Word.Application 
$doc=$word.Documents.Add($filename) 
$word.Visible=$False 
$word.ActiveDocument.Fields.Update() 
$doc.SaveAs([REF]$filename) 
$doc.Close() 
$word.Quit()
like image 343
SausageBuscuit Avatar asked Nov 03 '22 07:11

SausageBuscuit


1 Answers

To save your document as a template, use the following code:

 $word = new-object -com Word.Application 
 $doc = $word.Documents.add($filename)
 $word.Visible = $False 
 $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat],"wdFormatTemplate")
 $doc.SaveAs([ref]$filename2,[ref]$saveFormat)
 $doc.Close() 
 $word.Quit()

This is untested for templates, but I have got it to work with saving as .doc rather then the default .docx. Also, it won't let you save the file if it is the same name as the open file. That's why I changed it to $filename2. You might have to do some renaming tricks to get this to work, but this should get you past the problem with how to save the file.

like image 186
Nick Avatar answered Nov 09 '22 11:11

Nick