Question

I have a program where the user selects some data and the program then opens and populates a template Excel Workbook saved in the program directory for the user to then be able to modify further, write to a DB, etc.

The code looks something like this:

Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
...
ExcelWorkbook = ExcelApp.Workbooks.Open(PathToTemplateSheet)
...
ExcelWorkbook.SaveAs(Filename:=TempFileNameToSave)

By saving the workbook to a temp location, I'm ensured that the user doesn't over-write the template, but this still creates issues for me since no 2 users can work on this workbook at the same time since the workbook, TempFileNameToSave, will already be open by the other user.

My question is what is the best way to be able to open multiple copies of the template sheet without creating any problems for the users / over-writing an old one in case they want it?

My ideal solution would be to just give the user a new, unsaved workbook only open on their desktop and then they can save it whereever they want / not save it at all if they don't want to.

I imagine I could do that by:

  1. Creating a new, blank workbook: ExcelApp.Workbooks.Add
  2. Opening the template as above
  3. Copying all the worksheets from one to the other
  4. closing the template

But that would be very processor intensive if the template is of any decent size and I'm hoping there is a nicer way to accomplish this.

Thanks!!

Was it helpful?

Solution

For those of you who come across the same issue, I figured out how to do what I wanted.

The trick for me was to save my original template sheet literally as an excel template (.xlst). Then, all that I needed to change was to change this line of code:

ExcelWorkbook = ExcelApp.Workbooks.Open(PathToTemplateSheet)

To:

ExcelWorkbook = ExcelApp.Workbooks.Add(PathToTemplateSheet)

What this does is it creates a new workbook based upon the template and so can be created as many times as needed and never causing the actual, original template file to be opened by a specific user.

Hope this helps others too!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top