Search code examples
vbams-wordword-template

Is it possible to retrieve page margin definitions from an attached template in Word 2013 using Word VBA?


I've built an addin template (dotm) for Word 2013 that contains various macros to automate repetitive and laborious tasks on the Active document. They are run from controls on a userform. All work fine but there is one thing I can't seem to do.

The scenario is I receive a document not based on a specific template (different page setup, for example), one macro attach's a template (dotx) to the active document. What I'd like to able to do is to use a macro to get the page margin values from the attached template and apply them to the active document. I can't figure out how to get this to work, or if it is possible.

Any advice would be greatly welcomed. Thanks.


Solution

  • Try this ...

    Sub TemplateMargin()
    Dim doc As Word.Document, tDoc As Word.Document
    Set doc = ActiveDocument
    Set tDoc = ActiveDocument.attachedTemplate.OpenAsDocument
    With doc.PageSetup
        .TopMargin = tDoc.PageSetup.TopMargin
        .BottomMargin = tDoc.PageSetup.BottomMargin
        .LeftMargin = tDoc.PageSetup.LeftMargin
        .RightMargin = tDoc.PageSetup.RightMargin
    End With
    tDoc.Close wdDoNotSaveChanges
    End Sub