Avatar of RJHarvey72
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Looking for VBA code to insert a document (from a word template) with margins/formatting intact

It's probably easier if I explain why I want this and why I'm not just simply double clicking on the .dot file and then copying and pasting.

We have a system that will generate a blank document however this document is already 'linked' to save into exactly the right place within our document management system.

What I'd like is as follows. With the blank document open the typist will run a macro that will allow them to browse to our styles/templates directory where they will pick the correct word template and then the full contents of this will be inserted into the blank document. It needs to be exactly the same, so margins, styles etc etc Styles should be fine as it will be the same normal.dot for both the blank document and the document produced from the template.

So in basic terms we've replaced the blank document with the document produced from the word template.

I hope this makes sense but if not then just let me know and I'll try to clarify.

Microsoft WordProgramming

Avatar of undefined
Last Comment

8/22/2022 - Mon

Selection.Wholestory seems to include everything - headers, footers, page layout and text:
Sub CopyToActiveDocument()
    Dim docBlank As Document
    Dim docTemplate As Document
    Dim strFile As String
    Set docBlank = ActiveDocument
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .Filters.Add "Classic Templates", "*.dot"
        .Filters.Add "XML Templates", "*.dotx"
        If .SelectedItems.Count > 0 Then
            strFile = .SelectedItems(1)
            Exit Sub
        End If
    End With
    Set docTemplate = Documents.Open(strFile)
    docTemplate.Close wdDoNotSaveChanges
End Sub

Open in new window



Thanks very much for this. I won't have a chance to try this until Monday but will let you know how I get on.



We've had a test at this and it brings everything across except the margins. If we can get them then that will be perfect. One other thing that would be ideal would be if we could set the directory that the users starts browsing for the templates in if that is possible.

Thanks very much for your help on this I think we are almost there.

Kind regards

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question


Thanks very much for this. I have pretty basic VB skills so I'm struggling to incorporate the two new pieces of code into the main part you kindly provided.

I've tried tinkering about with it but I'm not able to get it to work.

Thanks again for your help much appreciated.