Solved

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

Posted on 2013-12-09
5
314 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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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)
Comment Utility
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
Comment Utility
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 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

772 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

11 Experts available now in Live!

Get 1:1 Help Now