Consulting

Results 1 to 9 of 9

Thread: Solved: Run macro after file save

  1. #1
    VBAX Regular
    Joined
    Feb 2007
    Posts
    68
    Location

    Question Solved: Run macro after file save

    Hi,
    How to run a macro just after the workbook is saved?
    Regards,
    Dan

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Caters for both Save and SaveAs

    [vba]

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
    Boolean)
    Dim sFile
    Application.EnableEvents = False
    '<Optional - this would be before save code>
    Cancel = True
    If SaveAsUI Then
    sFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
    If sFile <> False Then
    ThisWorkbook.SaveAs sFile
    '<Optional - this would be after save code>
    End If
    Else
    ThisWorkbook.Save
    '<Optional - this would be after save code>
    End If
    Application.EnableEvents = True
    End Sub
    [/vba]

  3. #3
    And an alternative to Bob's solution:

    [vba]Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.OnTime Now, "MyMacro", TimeValue("2099.12.31 23:59:59"), True
    End Sub
    [/vba]
    Explanation:
    Excel is not ready while save is in process. Setting OnTime to Now will wait until Excel is ready (i.e. save is finished), but no longer than December 31th, 2099. (long enough, I think ) then execute the sub called "MyMacro"

    You may have to alter "2099.12.31 23:59:59", as I'm not sure if this datetime format is compatible with your reginal settings.

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  4. #4
    VBAX Regular
    Joined
    Feb 2007
    Posts
    68
    Location
    Hi everyone,
    I found some error in Jimmy's code but thanks Jimmy for help.
    And Thankyou very much xld for a great help . that code worked !

    dan

  5. #5
    Quote Originally Posted by dansam
    Hi everyone,
    I found some error in Jimmy's code but thanks Jimmy for help.
    Can you tell me where the error was? And what kind of error?
    Because it worked for me OK, so I wouldn't like to discard the idea so easily.

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  6. #6
    VBAX Regular
    Joined
    Feb 2007
    Posts
    68
    Location
    Hi jimmy,
    the error was "Error 13 : Type Mismatch"

    Am I doing any thing wrong?
    Please point me to that !!
    Dan

  7. #7
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,322
    Location
    Sounds like you need to change this part to match your macro that you wish to run:
    [VBA]"MyMacro"[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Or, as I pointed out earlier, TimeValue("2099.12.31 23:59:59") might have to be altered. I don't know what date/time format is accepted by US Excel.
    Maybe TimeValue("12/31/2099 23:59:59"). I really don't know.
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  9. #9
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,322
    Location
    The problem is fixed by post #8 for me. The format was incorrect.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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