Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Indiana, USA
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Executing a macro on Document_Close

    This is probably a very simple macro, but I can't figure it out.

    Our users generate letters from templates. I need to have users save the new documents to a specific location so that others can access them. Since our users are not that adept with Word 97 and Windows, I want to automate the file/save process when they go to save a new document so they don't have to manually find the right location on the network to save their documents.

    I recorded a macro that opened the folder in which the new documents will be stored. But, in recording the macro, it also recorded a file(document) name in the save event. The users need to give a unique name to the new document each time they save. Does anyone have VBA code examples that can show me how to do this? (If I didn't explain myself clearly, please let me know.)

    Thanks.

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Executing a macro on Document_Close

    You could try something like this:

    <pre>
    Sub AutoClose()
    Dim strSaveName As String

    Do

    SaveAs:

    If ActiveDocument.Path = "" Then
    strSaveName = InputBox("Enter name:", "Save Document")
    If Trim$(strSaveName) = "" Then
    MsgBox "Enter a name"
    Else
    On Error GoTo ErrorHandler
    ActiveDocument.SaveAs "c:temp" & strSaveName
    On Error GoTo 0
    Exit Do
    End If
    Else
    ActiveDocument.Save
    Exit Do
    End If

    Loop

    Exit Sub

    ErrorHandler:

    MsgBox "Invalid name"
    Resume SaveAs
    End Sub
    </pre>


    This prompts for a name; checks to ensure a name has been entered- if not the loop gets executed again. If the file name is invalid ("On Error" processing) a message is displayed and the user is prompted for a name.

    I don't like using "On Error" for processing other than genuine errors; there must be a better way to check the validity of a file name- Perhaps a function?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Indiana, USA
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Executing a macro on Document_Close

    Thanks. I tried this code and it worked, sort of. I attached the code to a toolbar button. The first time it ran, it continued to loop between the input box and the message box. I cut out the If..Then..Else and tried it, but it gave an Error Code 5152-cannot recognize file name. Debug highlighted the line containing "c:temp". It seems that I read that path names in VBA need double back slashes, so I changed it to "c:temp" and it did save the document, but it also gave the 5152 error. I will keep working on it. It is real close.

    Thanks.

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Executing a macro on Document_Close

    Brumy,

    Edit alert!

    I did find a problem with the code as posted, and edited it, hoping you wouldn't notice the difference. The original did not exit the loop; the AutoClose also is initiated by closing a document you've already saved, so you need some code to stop that- hence the "ActiveDocument.Path".

    I don't get the 5152 error. I didn't need the double back slashes- I never have. I'm running Word 200 on WinNT. Can you post the code which isn't working?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,048
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Executing a macro on Document_Close

    If you are trying to make sure that the person is saving in the proper directory you could try the following in your AutoNew macro:
    ChangeFileOpenDirectory "F:ClientFiles"

    or whatever the folder you want the document save in is.

    Otherwise, you could write macros to intercept FileSave and FileSaveAs commands for your template.

    Putting this in the AutoClose or the document close event relies on your user not saving the document until they close it. The users where I work are trained to save the document as soon as they put something original into it. This can be well before they are finished with it and closing it.

    I would recommend putting this into a macro in a global template rather than in each letter form. That way if you change the location, you can change the macro in one place and have it work. You would then call the global macro from a macro in each of your letter templates.

    BTW in constructing your letter forms you may want to download the following:
    http://www.addbalance.com/word/downl...headsystem.zip.

    Hope this helps.
    Charles Kyle Kenyon
    Madison, Wisconsin

  6. #6
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Indiana, USA
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Executing a macro on Document_Close

    Geoff, you got it right. It works perfectly. I am sorry but I don't have a version of the code that didn't work.

    Chas, I did put it in a global template so it can be distributed to those that need it.

    This lounge is the greatest.

    Thanks to both of you.

Posting Permissions

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