Results 1 to 8 of 8

Thread: Word template automation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Arrow 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!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    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?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Word template automation

    wow great! let me try that. i got so many things to learn.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    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?

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Dim olddoc As object, newdoc As object
    2. Set olddoc = oword.Documents.Open("c:\somefile.doc")
    3. Set newdoc = oword.Documents.Add("template.dot")
    4. olddoc.Range.Copy
    5. newdoc.Range.PasteSpecial
    6. fname = olddoc.FullName
    7. olddoc.Close False
    8. newdoc.SaveAs fname  ' overwrite old file
    9. newdoc.Close
    10. ' 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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    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
  •  



Click Here to Expand Forum to Full Width