问题
I am trying to write a line of code to open an excel template (xltx) file as an excel template file not a new workbook.
I simply want to allow users to view and edit the templates they've side in a library folder on our network.
However everything I've tried opens a new workbook not a template.
I tried interop:
Workbooks.Open(selectedxltxFile, Missing.Value, Missing.Value, Excel.XlFileFormat.xlOpenXMLTemplate, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
and also the Process.Start method:
System.Diagnostics.Process.Start(@"C:\MYTemnplate.xltx")
Is this even possible or am I barking up the wrong tree?
回答1:
Workbooks.Open(Filename: @"C:\MYTemnplate.xltx", Editable: true);
I couldn't find this anywhere. When all else fails (this isn't pretty) record a macro containing the action you're trying to accomplish, and then look at the generated VBA.
From the docs:
Editable
Type: System.Object
Optional Object. If the file is a Microsoft Excel 4.0 add-in, this argument is True to open the add-in so that it’s a visible window. If this argument is False or omitted, the add-in is opened as hidden, and it cannot be unhidden. This option doesn't apply to add-ins created in Microsoft Excel 5.0 or later. If the file is an Excel template, use True to open the specified template for editing or False to open a new workbook based on the specified template. The default value is False.
In other words, this parameter can mean different things depending on the context, and none of them are intuitive. If you specify Editable: false
then a new workbook is opened based on the template. Is it editable? Yes.
来源:https://stackoverflow.com/questions/47059056/opening-an-excel-template-xltx-as-an-excel-template-not-a-workbook