none
Word, Ask- en REF velden, Sjabloon, VBA, macro RRS feed

  • Question

  • L.S.,

    I work within a Word file with, among other things, Ask and Ref fields. By asking questions about this, I found out that saving the file as a Word template (.dotm) is not an option because the Ask and Ref fields are not included after opening the template. Is this correct?

    However, the operation of a template is desirable.

    I have now solved this by adding a macro to the file and saving the file as Word with enabled macros (.docm).

    Sub AutoOpen ()
    '
    AutoOpen Macro
    '
    '
        Selection.WholeStory
        Selection.Fields.Update
        ActiveDocument.Save

    End Sub

    The effect of this is that you get the 'Save as' framework in which three things still need to be arranged:

    1. Save as Word document (from .docm to .docx)
    2. A new name, for example: Doc(ument)1
        Added here: a template gives a sequential number for one name of a document. If that is possible within VBA, that would be      a further improvement.
    3. The path can be the same as where the template is

    Saving as Word Template with enabled macros (.dotm) turned out to be no solution. Yet the Ask and REF fields are not included.

    Can someone help me with that?

    Curious about a reaction.

    Harry
    Friday, June 22, 2018 2:12 PM

All replies

  • ASK & REF fields in a template do get replicated in any document based on that template. If you create a numeric Custom Document Property named "Counter" in your template, you could use code like:

    Private Sub Document_New()
    Dim strNm As String, i As Long
    With ThisDocument
      strNm = Split(.FullName, ".dot")(0)
      i = .CustomDocumentProperties("Counter") + 1
      .CustomDocumentProperties("Counter") = i
      .Save
    End With
    With ActiveDocument
      .Fields.Update
      .SaveAs2 FileName:=strNm & "_" & Format(i,"000") & ".docx", Fileformat:=wdFormatXMLDocument
    End With
    End Sub

    Provided the fields you want to update are in the document body, the ASK field will be triggered and the cross-references updated. The above code will also save the document to the same folder as the template, with the same name and the value of the counter as a suffix. You could, of course, re-code it to save to a different folder, etc.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, June 23, 2018 4:22 AM