Saving a file to same file name

  • I want to open 1-Invoice-Template-1.xlsm and have it saved with the new invoice number right away so that when I open the file the next time the invoice number increments by 1. I have programmed the invoice number to increment by 1 when the file is opened.



    Right now, I press the Save File Box in the M5 area of the InputData page to save it to the new invoice name which is in M1. That works fine. But if I don't save the file first without any data entered yet on the InputData page, the next time I open the file, it stays the same invoice number.



    Can I use VBA to SAVE the file as the same file name once it is opened? Or do I need to use a Macro and another image like in the M5 area to save it first?

  • Re: Saving a file to same file name


    If I follow what you're wanting, and I'm not sure I do,
    maybe adding a sub to a standard module

    Code
    Public Sub Save_Right_Away()
        ThisWorkbook.Save
    End Sub


    and adding an instruction to the Workbook_Open sub that will run that sub 3 seconds after the workbook opens ?


    Code
    Application.OnTime Now + TimeValue("00:00:03"), "Save_Right_Away"
  • Re: Saving a file to same file name


    I cannot get this to work.


    Here is what I am trying to do. It works if I manually save the file each time I open it. This is an automotive work order template. I open 1-Invoice-Template-1.xlsm and it has an invoice number, say 1446. I save the invoice ( I am doing this manually right now - File Save ) before I put any information (Name, Address, Car, Mileage, Work to be Done, etc.) onto the invoice. After I put the information into the work order invoice, I do a save as by pressing on the Blue Save File Box. Then when I open 1-Invoice-Template-1.xlsm the next time, the invoice number becomes 1447, as I have it programmed to add 1 to change the invoice number from 1446 ( this is what the invoice was saved with) to invoice number 1447 when I open the file. I then would manually go File Save and it would be saved for the next time I open 1-Invoice-Template-1.xlsm. But guess what, being human, I don't always remember to go File Save manually. I am looking for a way to save the file automatically when I open the file. Basically I am just trying to have the invoice number increment automatically. What other ways can I go about incrementing the invoice number, if I cannot get this to work.



    Here is where I put this programming in the sheet 1 tab:
    Application.OnTime Now + TimeValue("00:00:03"), "Save_Right_Away"



    Code
    Sub sbVBA_To_Open_Workbook()
    Dim wb As Workbook
    ' Open the template with updated invoice number
    Set wb = Workbooks.Open("C:\Users\My Computer\Documents\Circle A\Invoices\1-Invoice-Template-2.xlsm")
    Application.OnTime Now + TimeValue("00:00:03"), "Save_Right_Away" 
    End Sub



    I put this programming in the This Workbook tab:


    Code
    Private Sub Save_Right_Away()
    ThisWorkbook.Save
    End Sub


    and I tried programming this in the sheet 1 tab as public like this:


    Code
    Public Sub Save_Right_Away()
    ThisWorkbook.Save
    End Sub



    What am I doing Wrong?
    Greg

  • Re: Saving a file to same file name


    Procedures called by an OnTime Statement must be Public Subs in a standard code module. After adding the module and code remove the 2 other Save_Right_Away procedures.


    Already mentioned in post #2...

    Quote

    maybe adding a sub to a standard module

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!