Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-12-09
5
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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 53

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 2000 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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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