Consulting

Results 1 to 11 of 11

Thread: Word prompt to save template

  1. #1
    VBAX Regular
    Joined
    Jun 2011
    Posts
    9
    Location

    Word prompt to save template

    (That title should be "Word prompts to save template".)

    I'm automating Word 2013 from Excel 2013. Excel directs Word to:

    - Create a new document based on a template I had previously set up.
    - Update the links in the Excel chart objects embedded in the document.
    - Break the links in those Excel chart objects (so that those charts become part of a static report and don't update with someone else's data).

    The user can run the project in two modes: running a single report, and running a batch of reports. In batch mode, it saves and closes those files. There are no problems with the Word docs made during batch mode.

    The problem is in one-by-one mode, which leaves the report open and unsaved for the user. If the user does nothing but close the document without saving it, they are prompted to save changes in the template. Then, after the document closes, they are prompted AGAIN to save changes in the same template, while Word is showing that it has no documents open.

    I've tried setting the template's Saved property to True, but it doesn't help.

    (BTW if they save the new document, close it, reopen it, and close it again, it doesn't prompt them to save the template then.)

    If I manually double-click on the template file, so that Word opens a new document based on it, and then manually close the new document, Word does NOT prompt to save the template.

    I could give the user a browse dialog and prompt them to save the file, but one possible reason for running the one-by-one mode in the first place is if they just want to take a quick look at a report without saving it.

    What can I do to make Word stop prompting to save the template?

    Also, why would it prompt TWICE to save the same template??

    Here's the code. Everything is declared, though some of these are declared
    further up; I have Option Explicit on. full_path_to_renewal_report_word_template
    is passed in to the function:

        Dim oWord As Object, oDoc As Object, oEmbeddedExcelChart As Object, oLinkFormat As Object, oInlineShape As Object
        Dim dblOldWidth As Double, dblOldHeight As Double, dblNewHeight As Double
        Dim iChartsCount As Long, iChartIndex As Long
        Const SET_LINKED_OJBECTS_WIDTH As Long = 468
        Set oWord = GetWord
        Set oDoc = oWord.Documents.Add(Template:=full_path_to_renewal_report_word_template)
        If bIsBatchMode Then oWord.WindowState = 0: DoEvents 'wdWindowStateNormal
        oWord.Visible = True
        iChartsCount = oDoc.InlineShapes.Count
        For iChartIndex = 1 To iChartsCount 'can't use for/next loop through inlineshapes collection; doesn't work with embedded xl chart objects; error after first item in collection.
        Set oInlineShape = oDoc.InlineShapes(iChartIndex)
        Set oLinkFormat = oInlineShape.LinkFormat
            If Not oLinkFormat Is Nothing Then
                oLinkFormat.Update
                DoEvents
                dblOldWidth = oInlineShape.Width: dblOldHeight = oInlineShape.Height
                oInlineShape.Width = SET_LINKED_OJBECTS_WIDTH
                oInlineShape.Height = SET_LINKED_OJBECTS_WIDTH * (dblOldHeight / dblOldWidth)
                DoEvents
                oDoc.InlineShapes(iChartIndex).LinkFormat.BreakLink
                DoEvents
            End If
        Next iChartIndex
    
        'set the template to saved so it won't prompt to save: (this doesn't help though)
        Dim oTemplate As Object
        Set oFile = FSO.GetFile(full_path_to_renewal_report_word_template)
        For Each oTemplate In oWord.Templates
        If oTemplate.Name = oFile.Name Then
            oTemplate.Saved = True
            Exit For
        End If
        Next oTemplate
    
        '1x1 mode; leave the word doc open and unsaved for the user:
        oWord.WindowState = 0: DoEvents 'wdWindowStateNormal
        oDoc.Activate: DoEvents
        oWord.Activate: DoEvents
    
        Set oWord = Nothing: Set oDoc = Nothing: Set oTemplate = Nothing: Set oEmbeddedExcelChart = Nothing: Set oLinkFormat = Nothing: Set oInlineShape = Nothing

  2. #2
    VBAX Regular
    Joined
    Jun 2011
    Posts
    9
    Location
    Due to the urgency of this issue, and not getting a response on my first post of it, I've posted wherever it seemed relevant:
    - stackoverflow post #41205505
    - msofficeforums post #post108010
    - excelforum #post4545449

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Are you using a global template add-in as a reference in your project? I've had this issue before and never could figure out why. I handle it like this:

    Sub AutoClose()
      On Error Resume Next
      If ActiveDocument.Path = vbNullString Then
        ThisDocument.Saved = True
      End If
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Regular
    Joined
    Jun 2011
    Posts
    9
    Location
    gmaxey, no, I am not using a global template add-in as a reference in my project. It's just a standard Word template -- a standard Word document saved as Template (.docx) file type. Not an add-in, and not being used as a reference in my project.

    Greg

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    You might try the AutoClose macro in your template anyway. It works here to eliminate the problem.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Regular
    Joined
    Jun 2011
    Posts
    9
    Location
    gmaxey, my template does not have macros. I would have to make it a .dotm (Word Macro-Enabled Template) to have an AutoClose macro. But I had tried that along the way and it prompted to be saved under even more circumstances, not less.

    Also, your code sets the ActiveDocument's Saved property to true. Word isn't prompting to save the active document, nor any other document. The active document was already saved and closes without any prompt.

    The template is never the active document when this happens -- never visible as a document in Word -- it's only the active document when I open it as a document to edit the template itself. When the problem happens, it's only open as a template, so never active. Only the new document created from it is active.

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Greg,

    I assume you given name isn't GregL65. Mine isn't gmaxey.

    The code in the AutoClose is setting ThisDocument.Saved = True not ActiveDocument but that is a mute point.


    I just tried this from Excel and got no prompt when I just closed the document without saving. Do you get a prompt with a simple test like this?

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oApp As Object
    Dim oDoc As Object
    
      Set oApp = CreateObject("Word.Application")
      Set oDoc = oApp.Documents.Add("D:\Test.dotx")
      oApp.Visible = True
    lbl_Exit:
      Exit Sub
      
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Regular
    Joined
    Jun 2011
    Posts
    9
    Location
    Greg, I got no prompt with that code, using the same template.

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Greg,

    I have been having a similar issue with a template that has a reference to a global template. It is really weird. I can open the template file, Alt+F11 to open the VBE, click Tools>References, "Just Look" then click Cancel and then close the template and about 90% of the time (randomly) I get the prompt to save the template when I have made no known changes. Apparently something is triggering the Saved property to false in that process and yours.

    I don't have anything else to offer. Sorry.
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Regular
    Joined
    Jun 2011
    Posts
    9
    Location
    Greg,

    Thanks, but I don't see that it has any references to a global template. If I select the template in the VBA editor, and go to Tools > References, only the 4 usual items are checked:

    Visual Basic for Applications
    Microsoft Word 15.0bject Library
    OLE Automation
    Microsoft Office 15.0 Object Library


    Greg

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Greg,

    Sorry for in confusion. Of course you don't. I only made that comment to show that when doing something with not apparent processes that would change the template just looking at the listed references (in my case anyway) is apparently setting the saved property to false.

    If you find a solution in one of the other forums, please update here.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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