Solved

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

Posted on 2013-12-09
5
315 Views
Last Modified: 2013-12-11
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.
0
Comment
Question by:milesryoung
  • 2
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39706012
Hi,

First you should create a book.xlt (template 97-2003) to be able to have an unsaved file by creating new document from template
set wb = workbooks.add("x:\book.xlt")

EDIT


You can catch the event before_save  and then use GetSaveAsFilename if you specify the folder and Only the folder in InitialFileName you can direct your default save location.

Regards
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39707714
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
0
 

Author Comment

by:milesryoung
ID: 39708337
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

0
 
LVL 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 500 total points
ID: 39710642
Hi,

SaveAsUI As Boolean is True if you have saved as,  so it doesn't fire another time and False if only make Save

Regards
0
 

Author Closing Comment

by:milesryoung
ID: 39711533
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
My experience with Windows 10 over a one year period and suggestions for smooth operation
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now