Results 1 to 4 of 4

Thread: Excel Save Continued/...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    31

    Excel Save Continued/...

    Can anyone help by telling me what is wrong with my code. I am trying to automate saving an excel file once data has been input. The problem is I still get a command asking if I wish to save changes to the file that already exists even although i've just saved it as this filename!
    VB Code:
    1. strPath = "C:\Test\"
    2. strFile = frmStartScreen.Text1.Text & ".xls"
    3. strFileName = strPath & strFile
    4.  
    5. On Error Resume Next
    6. Set objExcel = GetObject(, "Excel.Application")
    7. On Error GoTo ErrRoutine
    8. If objExcel Is Nothing Then
    9. Set objExcel = New Excel.Application
    10. End If
    11.  
    12. If Text1.Text = "1" Then
    13. With objExcel
    14. .Workbooks.Open "C:\Test\Test.xls"
    15. .Cells(1, 1) = Text3.Text
    16. .Cells(2, 1) = Text2.Text
    17. .Cells(3, 1) = Text4.Text
    18. .Cells(4, 1) = Text5.Text
    19. .Cells(5, 1) = Text6.Text
    20. .Cells(6, 1) = Text7.Text
    21. .Cells(7, 1) = Text8.Text
    22. .Cells(8, 1) = Text9.Text
    23. .ActiveWorkbook.SaveAs strFileName
    24. .ActiveWorkbook.Close savechanges:=True
    25. End With
    26. End If

    I receive the message:

    A file named 'C:\Test\Test.xls' already exists in this location. Do you want to replace it?

    with the options to say Yes, No, Cancel

    Can anyone tell me why it is asking me to save again? Have I forgotten some code or can anyone suggest a fix?



    Edit: Added [vbcode][/vbcode] tags for clairty. - Hack

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Save Continued/...

    When you do a .SaveAs your still left with the original wb in Excel and the new SaveAs wb. So the first issue is when you run this multiple times your file will alread exist and you will get the overwrite prompt (no way to surpress it without accepting the default of 'No') You could delete the preexisting file just before your .SaveAs so it will never prompt you for the overwrite. Second issue is the .SaveAs saving a duplicate file as whatever you name it. The solution is to tell Excel that the original copy has alread been saved witht the .Saved = True property assignment.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    31

    Re: Excel Save Continued/...

    Can you perhaps suggest some code thiat would allow me to delete the file? I am pretty new to vb.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Save Continued/...

    Sure, use the Kill function.
    VB Code:
    1. Kill "C:\Book1.xls"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width