Link to home
Start Free TrialLog in
Avatar of milesryoung
milesryoungFlag for Germany

asked on

Create a new Excel 2003 file based upon an existing one...

Hi,

I am using VB .NET (Windows Forms application) to create a new Excel file based upon an existing file ( which is an empty template style .xls file).

So far I can get as far as opening it and populating it with the data from a database. However, I want it to be presented to the user as an unsaved "Book1.xls" type of document like if I just open Excel manually myself and start working in an empty workbook.

I also have the path that the file should be saved to by default which I want Excel to use when the user presses "Save". I don't want to programmatically save the file as something because it could be that the user sees an error and just wants to discard without saving. I have put my own value into "ExcelApp.DefaultFilePath" in VB .NET but that isn't appearing.

Any help would be greatly appreciated. Points due to urgency.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
after creating the Excel.Application object, you need to set it's visible property to true for it to be presented to the user for interaction.


set ExcelApp = new Excel.Application
ExcelApp.Workbooks.Add(myTemplateFile)
'do the database stuff to load workbook
ExcelApp.Visible = true
Avatar of milesryoung

ASKER

Hi,

The first solution has sorted out the template issue/new file which is great thank you and I have trapped the WorkbookBeforeSave event in order to use GetSaveAsFilename.

This approach gives me the SaveAs dialogue twice though and each time there is a different filename. The first one is the template filename and the second one is the name I added in the WorkbookBeforeSave event.

I just want standard Office behaviour so the dialog is shown if the user wants to save it.

Thanks. You're already going to be getting points  Rgonzo1971!

This is what I'm using so far:

   
Private Sub myExcel_WorkbookBeforeSave(Wb As Microsoft.Office.Interop.Excel.Workbook, _
                                           SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles myExcel.WorkbookBeforeSave
        Try

            myExcel.GetSaveAsFilename(myCaseFolderPath & "\kalk.xls")

        Catch ex As Exception

            MsgBox(ex.Message)

        Finally

            Runtime.InteropServices.Marshal.ReleaseComObject(myExcel)

        End Try

    End Sub

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using Excel in .NET and trapping its events is much more complicated than just the "beforesave" event but this got me to the solution and answered my initial question. Much appreciated thanks.