Results 1 to 9 of 9

Thread: Problem with first save of Excel workbook

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Problem with first save of Excel workbook

    It seems that the Save method of the Excel app object behaves differently than Word. What happens in Word, if you issue the Save method, and it is the first time the document has been saved, a SaveAs dialog appears, allowing the end user to select the file name and path.

    However, this is not the case in Excel for some reason. When you issue the Save method, it never displays a SaveAs dialog, and assumes you want to save it as the current default name (Book1.xls) in the current path. Yet when clicking the Save button in Excel, it DOES display the SaveAs dialog if this is the first time you're saving.

    So why is there an inconsistency here? I could find no method in the Excel object to display the SaveAs dialog. I know I can display my own dialog by using the SaveFileDialog class, but the bigger issue is, how do I know this is the first time the user is saving and therefore show the SaveAsDialog?

    Very weird inconsistency if you ask me...

    Visual Studio 2010

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with first save of Excel workbook

    It has a file save as method...

    Using a dialog box

    Code:
    Application.Dialogs(xlDialogSaveAs).Show
    or directly issuing the command

    Code:
    ActiveWorkbook.SaveAs Filename:= "C:\Book1.xls"
    Regarding the comment

    It seems that the Save method of the Excel app object behaves differently than Word.
    I never faced this problem... If the file is new and if you press say Ctrl+S or File~~>Save then it will pop a File SaveAs Dialog. However I will recheck that when I go home as in my office I have Office 2007.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Problem with first save of Excel workbook

    I don't think you understood completely what I was saying.

    Yes, when using Excel directly, it DOES pop open a SaveAs dialog if it is the first time you have saved that workbook.

    However, this is NOT the case when using the API. When using the API (VSTO), and you initiate the Save method, and it is the first time the file has been saved, it DOES NOT show the SaveAs dialog as you would expect. Rather, it just attempts to use the default name assigned, such as Book1.xls and complains that the file already exists.
    Last edited by softwareguy74; Jul 9th, 2010 at 11:14 AM.

    Visual Studio 2010

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with first save of Excel workbook

    Can I see the code that you are using? Let me test it for you.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Problem with first save of Excel workbook

    Yes, in Excel with wb.Save method, if wb was never saved before then it will be saved to the current folder (with the default filename as displayed in Title when it was created) without asking filename and folder for SaveAs.

    The trick is: If a workbook was never Saved before then its Path is blank. You can easily to check that.

    This code is within Excel:
    Code:
    Sub SaveWB()
        Dim wb As Workbook
        Dim sFName As String
        
        Set wb = ActiveWorkbook '-- or any workbook
        If Len(wb.Path) Then
            wb.Save
        Else '-- wb was never saved before
            sFName = Application.GetSaveAsFilename()
            If sFName <> "False" Then
                wb.SaveAs sFName
            End If
        End If
    End Sub
    Automation code (from outside of Excel)
    Code:
    '-- xlApp has been previously created as "Excel.Application"
    Sub SaveWB2()
        Dim wb As Excel.Workbook
        Dim sFName As String
        
        Set wb = xlApp.ActiveWorkbook
        If Len(wb.Path) Then
            wb.Save
        Else '-- wb was never saved before
            sFName = xlApp.GetSaveAsFilename()
            If sFName <> "False" Then
                wb.SaveAs sFName
            End If
        End If
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Problem with first save of Excel workbook

    Thanks anhn! That did the trick.

    Now, what is the equivalent to GetSaveAsFilename in PowerPoint as it doesn't seem to be available?

    Visual Studio 2010

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with first save of Excel workbook

    Code:
    Application.FileDialog(msoFileDialogSaveAs).Show
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Problem with first save of Excel workbook

    Quote Originally Posted by koolsid View Post
    Code:
    Application.FileDialog(msoFileDialogSaveAs).Show
    Thanks but for some reason, it is not actually saving the file when using this.

    Visual Studio 2010

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with first save of Excel workbook

    Sorry ... went out with my team for dinner...

    Try this...

    Code:
    Sub Sample()
        Dim dlgSaveAs As FileDialog
    
        Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
        
        With dlgSaveAs
            '~~> If user presses Save
            If .Show = -1 Then .Execute
        End With
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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