Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Sep 2009
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Opening Word template appears as 'Read-Only'

    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!

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,436
    Thanks
    3
    Thanked 125 Times in 118 Posts
    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).
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    Star Lounger
    Join Date
    Sep 2009
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  5. #4
    Star Lounger
    Join Date
    Sep 2009
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  6. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,436
    Thanks
    3
    Thanked 125 Times in 118 Posts
    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #6
    New Lounger
    Join Date
    Dec 2009
    Location
    Galveston TX USA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:\WordForms\CustomerSlip. 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?

  8. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,436
    Thanks
    3
    Thanked 125 Times in 118 Posts
    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?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #8
    Star Lounger
    Join Date
    Sep 2009
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 "".

  10. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,436
    Thanks
    3
    Thanked 125 Times in 118 Posts
    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")
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •