|
-
Sep 19th, 2008, 12:20 PM
#1
Thread Starter
Lively Member
Word template automation
Hi everyone!
I got some problem and i need some help. I don't ask you guys to write codes for me what i need is some helpful input to help me get on the right track.
So, I created a word file and put some graphics header and footer. Now, I want to use that word template for some 400 word documents.
So basically open the template, open the other document, copy the content and paste it to the template, lastly save as the template to the original filename of the document.
It's easily said than done i know. can you guys give me some input on where to start?
Many thanks!
-
Sep 19th, 2008, 12:27 PM
#2
Re: Word template automation
First, most things can be figured out of how to program by recording macros.
http://www.vbforums.com/showthread.php?t=402032
Next, you can open the documents using the template instead of having to copy/paste.
You may want to check out my Office FAQ for tips and code on automating Word from VB6.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 19th, 2008, 02:22 PM
#3
Thread Starter
Lively Member
Re: Word template automation
Thanks for the reply. But how can i open the documents with my customized header? There are about 400 word files with no header and footer yet. I want to put those header and footer on those 400 word docs. Is there any easier way to automate this?
-
Sep 19th, 2008, 03:39 PM
#4
Re: Word template automation
You can open each document one at a time and attach your template to it, saveas and close. Then repeat for the next document.
ActiveDocument.AttachedTemplate = "C:\MyTemplate.dot"
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 19th, 2008, 03:54 PM
#5
Thread Starter
Lively Member
Re: Word template automation
wow great! let me try that. i got so many things to learn.
-
Sep 19th, 2008, 04:04 PM
#6
Thread Starter
Lively Member
Re: Word template automation
Hey Rob, I tried but its not working.
Code:
oWord.Documents.Open "C:\Somefile.doc"
oWord.ActiveDocument.AttachedTemplate = "C:\Template.dot"
oWord.ActiveDocument.Save
oWord.ActiveDocument.Close
Is there something wrong with the code?
-
Sep 20th, 2008, 07:49 AM
#7
Re: Word template automation
depending on the content of your headers /footers, it might be easier to copy the document content into a new document based on the new template
you can use the code below in a loop through your documents
vb Code:
Dim olddoc As object, newdoc As object Set olddoc = oword.Documents.Open("c:\somefile.doc") Set newdoc = oword.Documents.Add("template.dot") olddoc.Range.Copy newdoc.Range.PasteSpecial fname = olddoc.FullName olddoc.Close False newdoc.SaveAs fname ' overwrite old file newdoc.Close ' loop to next file
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 22nd, 2008, 01:47 PM
#8
Thread Starter
Lively Member
Re: Word template automation
Works like a charm! Cheers mate!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|