Link to home
Start Free TrialLog in
Avatar of RJHarvey72
RJHarvey72Flag 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.

Thanks
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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"
        .Show
        If .SelectedItems.Count > 0 Then
            strFile = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    Set docTemplate = Documents.Open(strFile)
    docTemplate.Select
    Selection.WholeStory
    Selection.Copy
    docBlank.Select
    Selection.WholeStory
    Selection.Paste
    docTemplate.Close wdDoNotSaveChanges
End Sub

Open in new window

Avatar of RJHarvey72

ASKER

Graham,

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.

Richard
Graham,

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

Richard
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Graham,

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.

Richard