• Opening Word template appears as ‘Read-Only’

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Opening Word template appears as ‘Read-Only’

    Author
    Topic
    #492815

    Hi,

    I have an Access database that interacts with a number of Word templates, fills in some bookmarks and presents the document to the user. This all works fine, however what happens is that when the database creates a document from the template and displays it to the user, the original ‘template’ gets marked as ‘Read-Only’ until the user closes the document that was generated on their computer. This is the code I’m using to create a document from the template:

    Set wdapp = CreateObject(“Word.Application”) ‘Create an instance of Word
    wdapp.Documents.Add Template:=TemplatePath & TemplateName ‘Open the template the user has chosen

    I’m using this method because I want the user to create a ‘version’ of the template, not the template itself.

    I also set a few variables to nothing at the end of the sub:

    Set wdapp = Nothing
    Set ActiveTemplate = Nothing

    Why would the template itself be set to read only if I’m just opening a document from it?

    Hope someone can help!

    Viewing 5 reply threads
    Author
    Replies
    • #1433275

      Word’s concept of a template is that when you open any document, the ‘attached template’ is also opened. This gives you instant access to the ribbon customisations, macros and building blocks contained within the template.

      If you want to release the template while you still have the document open then you need to detach the template after the document is opened eg
      ActiveDocument.AttachedTemplate = “”

      Note that all documents MUST have an attached template. If you set it to nothing then you are actually attaching the ‘Normal template’ as the attached template.

      You could also create your new document as a template (since templates can’t have an attached template).

      • #1433439

        Thanks Andrew for replying.

        When you say that ‘if you set it to nothing, you are actually attaching the ‘Normla template”, what does that actually mean for the document itself? Will it not appear correctly?

      • #1433499

        I’ve tested setting the attachtemplate to “” this morning but the underlying template still appears as read only until I close the document that I created. Any suggestions?

    • #1434163

      In my experience, detaching the template frees the template to allow it to close. Closing the document shouldn’t be necessary unless you have also added the template as an add-in (which serves no purpose when the template is attached to the activedocument).

      Are sure you (or someone else on the network if the template is on a network) don’t have any other documents open at the same time. The template is open while any document holds it open.

    • #1434465

      Similar code from Susan Harkins at TechRepublic is:

      Dim appWord As Word.Application
      Dim doc As Word.Document

      Set appWord = New Word.Application

      Set doc = appWord.Documents.Open(“C:WordFormsCustomerSlip.doc”, , True)

      where the last (3rd) argument of the Open method sets the template’s Read-only property to True.
      Is there something analogous happening with you? Some omitted default parameter??

      Would you be so kind as to share the code that passes the data from Access to Word?
      What versions of each are you using?

    • #1434508

      mlwonio
      A good old source of vba code with examples of code automating Word from Access is Helen Feddema’s CodeSamples. Although many samples there are 15 years old these methods haven’t changed much.

      jasonsas
      Is your ‘template’ actually a template or is it a document?

    • #1434954

      Hi mlwonio and Andrew,

      Here is the code I’m using to open the template:

      Set wdapp = CreateObject(“Word.Application”) ‘Create an instance of Word
      wdapp.Documents.Add Template:=TemplatePath & TemplateName ‘Open the template the user has chosen
      Set ActiveTemplate = wdapp.ActiveDocument ‘Set the ‘ActiveTemplate’ variable

      At the end of the sub I use this line:

      ActiveTemplate.AttachedTemplate = “”

      The document I’m opening is definitely saved as a Word Template. If I check the value of ‘AttachedTemplate’ before I get to that line, it’s set to the value of the template they’ve opened. If I check it just after that line execute, I get “Normal.dot”.

      Admittedly I’ve been testing this locally on my PC, but these are the results I’m getting:
      – If I try to open the template just before I execute ‘ActiveTemplate.AttachedTemplate = “”‘, the template is marked as ‘Read-Only’
      – If I try to open the template just after I execute ‘ActiveTemplate.AttachedTemplate = “”‘, the template is marked as ‘Read-Only’
      – If I try to open the template after I close the document I’ve generated, the template is NOT marked as ‘Read-Only’

      Does it matter that I’m testing this locally on my PC? Ie, I’m still trying to open the template on the same PC I’ve generated the document, even though I’ve set the ‘AttachedTemplate’ to “”.

    • #1434964

      It doesn’t matter that you are testing on a local PC – I would expect the same results on the network.

      Your code could be more succinct if you set the doc at the same time as you create it. This would have the happy side-effect of removing confusion over what is the ‘ActiveDocument’ at any point in the code.

      Dim aDoc as Document

      Set wdapp = CreateObject(“Word.Application”) ‘Create an instance of Word
      Set aDoc = wdapp.Documents.Add(Template:=TemplatePath & TemplateName) ‘Open the template the user has chosen
      aDoc.AttachedTemplate = “”

      It may be that Word is unhappy to release the first template if that is the first doc opened. Do you get the same result if you already have Word open and change your CreateObject line to
      Set wdapp = GetObject(, “Word.Application”)

    Viewing 5 reply threads
    Reply To: Opening Word template appears as ‘Read-Only’

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: