none
Saving document variables in .dot RRS feed

  • Question

  • Hello,

    I've run into unexpected behavior with a Word project and I have been unable to find an explanaition so far, and I would appreciate if anyone here may have some insight.

    I have a word document containing a userform (call it doc A), and I have added document variables that serve to save the state of the userform between open/close. On its own, everything works as expected.

    I then made a template out of the document as I would like to be able to distribute it, and thats where I noticed something strange. After using the template, when I click to close the file, I am prompted once to save the "document1" based off of the template, but then after responding to this I am prompted again to save the original document "doc A". After some experimenting, I've narrowed this second prompt down (I beleive) the the document variables, and this second prompt is asking whether or not to save these values as well as the document.

    Can anyone explain what is happening here? I would like to avoid this second prompt to save, but the fact that it is there makes me think I'm doing something incorrectly.

    Developing with Word 2003.

    Thanks.

    Wednesday, February 2, 2011 7:03 PM

Answers

  • Hi Silvers

    <<EDIT: I tried a few more tests, and I've narowed the problem down to something that looks much simpler. As in the code below, I am creating a button on open that show/hides my userform. If I exclude this call, I am not prompted when closing an instance of the template, and after some research I found that this is a somewhat common issue.

    So I guess my question now is simply how to prevent my addition to the toolbar from causing this prompt on close. I've found that putting

    ThisDocument.Saved = true

    in AutoClose does the trick, but I'm not sure if this will have any negative side-effects? My toolbar button is created on open and destroyed on close, so there should be no need to save anything.

    /edit>>

    Ah, OK, this does explain a lot :-)

    When you use CommandBars stuff in Word, these changes can be saved to any number of places: the current document, its template, or a "global template" (such as Normal.dot). If you don't specify where the change should be made (Application.CustomizationContext) then Word chooses something. This will, of course "dirty" whichever document file this turns out to be, and thus the prompt.

    In any case, you should add Application.CustomizationContext = [target file] to your code. You should think about what [target file] should be. Examples:

    = ActiveDocument
    = ActiveDocument.AttachedTemplate
    = NormalTemplate 'usually NOT appropriate for a dev

    If you use ActiveDcoument.AttachedTemplate (which is apparently what Word is choosing for you at least some of the time), then I'd also use ActiveDocument.AttachedTemplate.Saved = True. (ThisDocument is the code container, which in this case is the same, but it's better to be more specific.)

    As long as you're not making any other changes that you'd want saved back to the template, there shouldn't be any ill effects.


    Cindy Meister, VSTO/Word MVP
    Friday, February 11, 2011 8:18 AM
    Moderator

All replies

  • Is the original document (doc A) really a template?

    One would have to see the code that is being used to offer pertinent advice.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Silversn95" wrote in message news:680748fe-7be2-463c-a724-36d99a2e8ea5@communitybridge.codeplex.com...

    Hello,

    I've run into unexpected behavior with a Word project and I have been unable to find an explanaition so far, and I would appreciate if anyone here may have some insight.

    I have a word document containing a userform (call it doc A), and I have added document variables that serve to save the state of the userform between open/close. On its own, everything works as expected.

    I then made a template out of the document as I would like to be able to distribute it, and thats where I noticed something strange. After using the template, when I click to close the file, I am prompted once to save the "document1" based off of the template, but then after responding to this I am prompted again to save the original document "doc A". After some experimenting, I've narrowed this second prompt down (I beleive) the the document variables, and this second prompt is asking whether or not to save these values as well as the document.

    Can anyone explain what is happening here? I would like to avoid this second prompt to save, but the fact that it is there makes me think I'm doing something incorrectly.

    Developing with Word 2003.

    Thanks.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Thursday, February 3, 2011 11:26 AM
  • Hi Silversn

    I agree with Doug: we'd need to see your code. It wouldn't hurt for you to explain the steps you used to make a template from the document (just to be sure we're on the same page with that).

    FWIW the document properties in a template are available when a document linked to the template is open. So it is possible that your code might be writing back to the template (as well as, or instead of the document). That would explain the prompt. But without seeing your code we can't be certain.


    Cindy Meister, VSTO/Word MVP
    Thursday, February 3, 2011 6:18 PM
    Moderator
  • EDIT: I tried a few more tests, and I've narowed the problem down to something that looks much simpler. As in the code below, I am creating a button on open that show/hides my userform. If I exclude this call, I am not prompted when closing an instance of the template, and after some research I found that this is a somewhat common issue.

    So I guess my question now is simply how to prevent my addition to the toolbar from causing this prompt on close. I've found that putting

    ThisDocument.Saved = true

    in AutoClose does the trick, but I'm not sure if this will have any negative side-effects? My toolbar button is created on open and destroyed on close, so there should be no need to save anything.

    /edit

     

    Thanks for the replies. I believe this to be a two-part issue since I first posted after doing some experimenting. A detail I didnt mention in my original post is that my userform will update form fields in the document depending on changes. It looks like these form fields that are changed by the macro will not save either unless I save the second prompt.

    Cindy, it seems to me that what you are suggesting is what must be happening, but I'm not sure why... In a seperate module than below, I declare

    Dim myDoc As Word.Document       - which is set to Active Document in AutoNew and AutoOpen. I thought this would ensure nothing is being written back to the document. This is my first stab with userforms/doc variables though so there is a good chance I'm missing something.

    By template I just mean that I was developing the document as A.doc. Once I was satisfied, I saved a A.dot from it through File>SaveAs etc. I then went to test the A.dot by double clicking (or right-click New), which is what ideally would be distributed, and ran into the issue.

     Thanks,

    Public Sub SaveFormVars()
    'save public variable values into document variables that will be preserved after word is closed.
    
    With myDoc
     .Variables("QB_DD").Value = QB_DDv
     .Variables("IN_TB").Value = IN_TBv
    End With
    
    End Sub
    
    
    Public Sub LoadFormVars()
    'Load value of saved document variables into temporary public variables for access by form
    
     QB_DDv = myDoc.Variables("QB_DD").Value
     IN_TBv = myDoc.Variables("IN_TB").Value
    
    End Sub
    
    
    Public Sub initFormVars()
    'Create document variables for saving forms state. Initialize with these default values.
    
    With myDoc
     
     .Variables.Add Name:="QB_DD", Value:=0
     .Variables.Add Name:="IN_TB", Value:="Name"
      
    End With
    
    End Sub
    
    Public Sub deleteFormVars()
    'Deletes document variables used for saving state
    
    'On Error Resume Next
    
    With myDoc
     .Variables("QB_DD").Delete
     .Variables("IN_TB").Delete
     
    End With
    
    End Sub
    
    
    Public Sub addButton()
     'Add button to word toolbar for user to display form.
     
     Dim cmbBar As CommandBar
     Dim cmbControl As CommandBar
     Dim toolControl As CommandBarControl
     Dim test As Boolean
      
    
     Set cmbControl = Application.CommandBars("Tools")
     test = False
    
     With cmbControl
     
      For Each toolControl In .Controls 'Check for existence of custom control
       If toolControl.Caption = "Display Show/Hide form" Then
        test = True
       End If
      Next toolControl
    
      If test = False Then    'If not already in toolbar...
       With .Controls.Add(Type:=msoControlButton, Before:=2) 'adds a dropdown button to the menu item
        .Caption = "Display Show/Hide form"   'adds a description to the menu item
        .OnAction = "displayShowHideForm"      'runs the specified macro
        .FaceId = 1098          'assigns an icon to the dropdown
       End With
      End If
      
     End With
    End Sub
    
    
    Public Sub AutoNew()
    
     Set myDoc = ActiveDocument
     MsgBox myDoc.Name
     
     wasProtected = False
     'Unprotect the file
     If myDoc.ProtectionType <> wdNoProtection Then
      myDoc.Unprotect Password:=""
      wasProtected = True
     End If
     
     Call initFormVars
     Set SHForm = New ShowHideForm
     
     'MsgBox "New"
     
     Call addButton
     
     If wasProtected Then
      myDoc.Protect _
      Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
     End If
     
    End Sub
    
    Public Sub AutoOpen()
    
     Set myDoc = ActiveDocument
     
     wasProtected = False
     'Unprotect the file
     If myDoc.ProtectionType <> wdNoProtection Then
      myDoc.Unprotect Password:=""
      wasProtected = True
     End If
     
     'MsgBox myDoc.Path
     
     Set SHForm = New ShowHideForm
     
     Call addButton
     
     If wasProtected Then
      myDoc.Protect _
      Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
     End If
     
    End Sub
    
    Public Sub AutoClose()
     wasProtected = False
     'Unprotect the file
     If myDoc.ProtectionType <> wdNoProtection Then
      myDoc.Unprotect Password:=""
      wasProtected = True
     End If
     
     
     Call SaveFormVars
     
     On Error Resume Next 'in case the menu item has already been deleted
      Application.CommandBars("Tools").Controls("Display Show/Hide form").Delete
     On Error GoTo 0
     
     
     Unload SHForm
     Set SHForm = Nothing
    
     
     'Reprotect the document.
     On Error Resume Next
     If wasProtected Then
      myDoc.Protect _
      Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
     End If
     On Error GoTo 0
     
     Set myDoc = Nothing
     
     ActiveDocument.AttachedTemplate.Save
     'ThisDocument.Save
     'ThisDocument.Saved = True
     
    End Sub
    
    Public Sub displayShowHideForm()
     wasProtected = False
     'Unprotect the file
     If myDoc.ProtectionType <> wdNoProtection Then
      myDoc.Unprotect Password:=""
      wasProtected = True
     End If
    
     SHForm.Show
    End Sub
    
    
    Tuesday, February 8, 2011 6:30 PM
  • Hi Silvers

    <<EDIT: I tried a few more tests, and I've narowed the problem down to something that looks much simpler. As in the code below, I am creating a button on open that show/hides my userform. If I exclude this call, I am not prompted when closing an instance of the template, and after some research I found that this is a somewhat common issue.

    So I guess my question now is simply how to prevent my addition to the toolbar from causing this prompt on close. I've found that putting

    ThisDocument.Saved = true

    in AutoClose does the trick, but I'm not sure if this will have any negative side-effects? My toolbar button is created on open and destroyed on close, so there should be no need to save anything.

    /edit>>

    Ah, OK, this does explain a lot :-)

    When you use CommandBars stuff in Word, these changes can be saved to any number of places: the current document, its template, or a "global template" (such as Normal.dot). If you don't specify where the change should be made (Application.CustomizationContext) then Word chooses something. This will, of course "dirty" whichever document file this turns out to be, and thus the prompt.

    In any case, you should add Application.CustomizationContext = [target file] to your code. You should think about what [target file] should be. Examples:

    = ActiveDocument
    = ActiveDocument.AttachedTemplate
    = NormalTemplate 'usually NOT appropriate for a dev

    If you use ActiveDcoument.AttachedTemplate (which is apparently what Word is choosing for you at least some of the time), then I'd also use ActiveDocument.AttachedTemplate.Saved = True. (ThisDocument is the code container, which in this case is the same, but it's better to be more specific.)

    As long as you're not making any other changes that you'd want saved back to the template, there shouldn't be any ill effects.


    Cindy Meister, VSTO/Word MVP
    Friday, February 11, 2011 8:18 AM
    Moderator