Solved

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

Posted on 2013-12-09
5
317 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 50

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 50

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

828 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