Word Doc as TEMPLATE

Get help with using AutoHotkey and its commands and hotkeys
Drako
Posts: 27
Joined: 14 Jan 2016, 15:08

Word Doc as TEMPLATE

02 May 2016, 04:00

Hello!

I’m trying to have AutoHotkey to edit specific text into a word doc and print it out. I have a document with specific formatting which contains special codes in specific fields, this is my template. I’m trying to make a script that replaces those fields with the current time and then it prints it out.
I have no idea how to read a word doc, edit it and print it out so it does not loose the formatting.

Could someone help me out?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Word Doc as TEMPLATE

02 May 2016, 10:00

As an alternative to Find and Replace, you could use bookmarks as shown in a recent thread: https://autohotkey.com/boards/viewtopic ... 568#p84568
Drako
Posts: 27
Joined: 14 Jan 2016, 15:08

Re: Word Doc as TEMPLATE

02 May 2016, 13:58

well, I got a bit lost now... Blackholyman, those are cool references but I somehow don’t manage to get what I want :-( Im getting better but Im still not that expert with programming yet to figure out the additional coding I need to get what I what.
Kon’s reference is more like what Im looking for but I dont understand what are these bookmarks. I mean, I do understand what they are but what code do I need to put in the word doc around the text that i want to replace?
Per what I understand, this from Kon places text into a specific location into the word FROM an other word doc, I would like to make it that the new text comes from the script: I would like to have 2 bookmarks in my template and replace one with the current date and time, and some text.
At last, if possible I would like to make it so it simply replace the text and print (close with no save), without even showing the word file.

Wow, my post really look like a shopping list :-P
Well, if you have time to show me how to do this it would be awesome!! TKS!!!!!!!!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Word Doc as TEMPLATE

02 May 2016, 17:20

Try this. There is a template in the same dir as this script named "MyDocument.dotx". The script opens the template and inserts text in two places. The template has two bookmarks named "MyBookmark1" and "MyBookmark2."

Code: Select all

; .dotx is a Word template, but .doc and .docx will also work
FilePath := A_ScriptDir "\MyDocument.dotx"
wdApp := ComObjCreate("Word.Application") ; Create an instance of Word

; You can remove this after testing so that Word stays invisible. 
; Confirm that the script closes Word at the end so you don't get a bunch of
; invisible Word applications open in the background.
wdApp.Visible := true

MyDocNew := wdApp.Documents.Add(FilePath) ; Open the template
FormatTime, MyTime,, h:mm tt dddd MMMM d, yyyy

; Put the text into the bookmarks
MyDocNew.Bookmarks("MyBookmark1").Range.Text := MyTime
MyDocNew.Bookmarks("MyBookmark2").Range.Text := "Hello World!"

MyDocNew.PrintOut() ; Print the document with the default printer
MyDocNew.Close(0) ; Close the document without saving
wdApp.Quit() ; Quit Word
return
Add a bookmark - https://support.office.com/en-us/articl ... 99c2a0#bm1
How to quickly show or hide bookmarks in Word? - https://www.extendoffice.com/documents/ ... marks.html
Drako
Posts: 27
Joined: 14 Jan 2016, 15:08

Re: Word Doc as TEMPLATE

03 May 2016, 01:58

WOW! Perfect :-) EXACTLY WHAT I WANTED! :-) thank you so much!!
sollermun
Posts: 1
Joined: 19 Jul 2018, 14:18

Re: Word Doc as TEMPLATE

12 Aug 2018, 03:38

How do you add text to a bookmark without losing the bookmark? I have references that are not working because the original bookmark is eliminated when replaced with text.

I'd like to do something like this: https://wordmvp.com/FAQs/MacrosVBA/Inse ... okmark.htm

Return to “Ask For Help”

Who is online

Users browsing this forum: au6, BushMange, CEA6597, howardb1, VACO BenQ, w0z and 190 guests