Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Word doc 'saved' (W97)

    Does anyone know a way (probably VBA) to get Word to differentiate between a document that has been changed and saved in this session, a document that hasn't been changed and is therefore (or should be) still saved, and one that hasn't been saved after changes?

    With the help of recent posts, I have AutoClose code to say if the doc is saved, change the view, mark it as dirty and save it again, but if it isn't, offer to save it, which works fine until I get a document that I've opened and, without changing it, want to close it again - then it crashes on the 'save it' line, as follows:

    <pre>Sub autoclose()
    Dim vResult
    If ActiveDocument.Saved = True Then
    With ActiveDocument
    .ActiveWindow.View.Type = wdNormalView
    .Saved = False
    .Save
    End With
    Else
    vResult = MsgBox("This document has not been saved yet. Do you want to save " _
    " the changes?" + Chr(13) + "(No = don't save changes, so set view back" _
    " yourself)", vbYesNo)
    If vResult = vbNo Then
    ActiveDocument.Saved = True
    Else
    With ActiveDocument
    .ActiveWindow.View.Type = wdNormalView
    .Saved = False
    .Save
    End With
    End If
    End If
    End Sub</pre>


    The bold line is where it crashes. Any suggestions would be gratefully received! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Beryl M


  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Word doc 'saved' (W97)

    Beryl,

    Why is this all necessary? <g>

    Not sure if I understand what's going on, but possibly saying Saved=False is not exactly the same thing as dirtying the document.
    One clumsy workaround (that might work anyway) would be to ensure the document actually is dirty rather than (or in addition to) just saying .Saved=False.

    You could, for example, go to the end of the ActiveDocument, insert a space, then delete it. That seems like a safe way to dirty a doc.

    Gary

  3. #3
    BAM
    Guest

    Re: Word doc 'saved' (W97)

    Well... Saved=False is the VBA equivalent to the WordBasic command: SetDocumentDirty 1, so it would be correct as it turns off the Save flag and Word will rewrite the document.

    I'm not sure why Beryl's code is generating an error. I'm wondering if perhaps it isn't a rogue temp file causing the problem.

    Beryl - I don't understand what you want to accomplish either. <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

    If I'm not mistaken, your message box is acting the same way the built-in functionality operates. If Word sees a document as dirty it will prompt you to save the changes. If you say "Yes" then the file is saved and closed. If you say "No" then all changes are discarded.

    So it seems to me all you need is:

    <pre>If ActiveDocument.Saved = True Then
    With ActiveDocument
    .ActiveWindow.View.Type = wdNormalView
    .Saved = False
    .Save
    End With
    End If
    </pre>


    Or perhaps what you really want is an AutoClose macro that asks which View you want to save the document in? Something like:

    <pre>Sub AutoClose()
    Dim vResult As String
    Dim Msg As String
    Dim btype As String
    Msg = "Do you wish to save the document in Normal View?" _
    & vbCr & "Select Cancel to close without saving"
    btype = vbYesNoCancel
    vResult = MsgBox(Msg, btype)
    If vResult = vbYes Then
    With ActiveDocument
    .ActiveWindow.View.Type = wdNormalView
    .Saved = False
    .Save
    End With
    ElseIf vResult = vbNo Then
    With ActiveDocument
    .ActiveWindow.View.Type = wdPageView
    .Saved = False
    .Save
    End With
    ElseIf vbCancel Then
    With ActiveDocument
    .Saved = True
    End With
    End If
    End Sub
    </pre>

    ~~~~~~~~~~~~~
    Cheers! <img src=/S/flags/USA.gif border=0 alt=USA width=30 height=18>

  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Word doc 'saved' (W97)

    Well, to be absolutely accurate, it's not 'necessary', just as I explained in my original post that I prefer to work in page layout view while the templates I'm working on are for our users who work in normal view. All I wanted was to have them open in my view but be saved back into their view when I was finished, with a caveat to cancel out if I didn't want the 'last edited' date to change.
    Beryl M


  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Word doc 'saved' (W97)

    Thanks, BAM, but I don't see where your second piece of code differs from mine, except that you've named a couple of the options as variables ...? <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    Beryl M


  6. #6
    BAM
    Guest

    Re: Word doc 'saved' (W97)

    Hi Beryl,

    The code I gave you is just a variation of what you already had, it just includes Page/Print Layout view as a saving choice.

    I think that perhaps the main difference is how you are accustomed to closing a file. If you religiously Save and Close then I can see where they appear to be similar. If you are one (like me <img src=/S/grin.gif border=0 alt=grin width=15 height=15> ) who often Closes a file and then decides to Save or Discard the changes then mine is different, it allows you to choose which view you want to save the changes in.
    ~~~~~~~~~~~~
    Cheers! <img src=/S/flags/USA.gif border=0 alt=USA width=30 height=18>

Posting Permissions

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