Results 1 to 12 of 12
  1. #1
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Identifying a New Unsaved Document (VBA, Word 2003 )

    Dear Longers,

    I want to be able to have a cancel button on a user form that as well as unloading the form will identify if the document (Word 2003) is a new document that has never yet been saved. The form will run as part
    of Autonew, if the user cancels the document can be scrapped when it is a new one i.e. closed without saving. However the form will also be availabel as a button on a toolbar for using with existing documents, in this circumstance the form must be unloaded but the document will remain for the user to work with.

    So the question is how do I considtently identify this is a brand new and as yet unsaved document?

    thank you...... liz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Identifying a New Unsaved Document (VBA, Word 2003 )

    You could look at ActiveDocument.Path. If the document has never been saved, this will be an empty string "".
    <code>
    If ActiveDocument.Path = "" Then
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    Unload Me
    Exit Sub
    End If</code>

  3. #3
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Hans,

    as usal elegant and simple - why didn't I think of this!

    thank you......liz

  4. #4
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Hans,

    Using this code the Save dialogue is displayed. I would like to supress it. I thought this was because I have the option for prompt to save set on but although I modified to this:

    If ActiveDocument.Path = "" Then ' therefore new unsaved document
    Options.SavePropertiesPrompt = False ' turn off the prompt in file options
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    Options.SavePropertiesPrompt = True ' turn back on the prompt in file options
    Exit Sub
    End If

    it still shows the save dialogue. How do I stop this?

    thank you.......................... liz

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    This code in itself should not cause the Save As dialog to be displayed (I tested it). Do you have other code that tries to save the document?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    1) If you *always* hide the form, you can use

    If ActiveDocument.Path = "" Then ' therefore new unsaved document
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    End If
    Me.Hide ' hide the form

    No need to use an Else clause.

    2) Once again, I suspect that you have other code you haven't told us about. Normally, the Initialize event wouldn't be repeated.

  7. #7
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Hans,

    Resolved it the code needs to read like this:

    If ActiveDocument.Path = "" Then ' therefore new unsaved document
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    CaptureFileProperties.Hide ' hide the form
    Else
    CaptureFileProperties.Hide ' hide the form
    End If

    I was doing hide before the if so it didn't work.

    The investigation has raised a query though. I have debug statement throughout at the moment and the debug log shows this:

    Initializing Form --- this is displayed at start of the form initialize procedure
    created new form --- displayed by the calling module after the new form has been created
    BooUserCancelled = False --- displayed by the calling module after setting the cancelled flag prior to showing the form
    User Selected OK --- displayed by the form click OK procedure
    One or More fields INVALID --- displayed by the form click OK procedure but invalid entry (I always do this prior to testing cancel)
    User Selected Cancel --- displayed by the form click Cancel procedure
    ActiveDocument.Path = --- displayed by the form click Cancel procedure
    new unsaved document so close & don't save --- displayed by the form click Cancel procedure prior to closing
    Initializing Form --- DON'T UNDERSTAND THIS this is displayed at start of the form initialize procedure

    Everything is as expected now and the form closes but the debug reports that the form initialize procedure is entered (last line above). In fact I know it doen't return to the calling module since there is a debug statement reporting the form is all done. This will be a problem since I will never get to prcess the data entered by the user which is performed in the calling module.

    Any idea why?

    thank you liz

  8. #8
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Hans,

    yes I realised after I'd posted when re-examining the code that the else was redundant so have removed it.

    As far as initialise goes. The cancel routine ends imeadiately after this code so I don't know what might cause the 2nd initialise. If I inadvertently loaded the form ywice would the second instnace wait till th efirst was complete and then try to run? (and since I've closed the document it goes nowhere?). If so what, apart from "show" might load a form?

    liz

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Could you post a stripped down and zipped copy of the template? You should remove sensitive or proprietary information from the copy.

  10. #10
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Hans,

    This is it. Ihave zipped it, wasn't sure what "stripped down" meant so have not done any more than zip, I hope that's OK. there's nothing sensitive in there. there are probably silly beginner erros as neither I nor Luca are programmers - till last week. I have some stuff I have developed over the years to automate style and template creation but that hardly counts. So please excuse any silly babyish errors.

    I should add that the form when run on its own (F5 in VB editor) behaves perfectly and does not re-enter the initialize procedure. It's when I run the macro as intended that I encounter the error (under any combination of conditions) it does therefore seem that I might be loading two versions of the form? But I can't see how or where.

    many thanks........................ liz

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    By "stripped down" I meant that you could have deleted those parts of the document that are not relevant for understanding the problem.

    You should replace both occurrences of CaptureFileProperties.Hide with Me.Hide in the module belonging to the userform. This means that the code applies to the currently open instance of the form. By using CaptureFileProperties you run the risk of creating a new instance, which causes the UserForm_Initialize event again.

  12. #12
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Identifying a New Unsaved Document (VBA, Word

    Hans,

    Ok that seems to have done it, I was attempting nort to have sloppy references! Sorry I left all the detail in, I was afraid I would remove something that would help diagnose the probelm. So now I'm on to the next hiccup!

    many thanks............................. liz

Posting Permissions

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