I'm able to generate Word documents without issue. I save the resulting *.docx file to a temporary location and then need to launch the file in Word.
The requirement is to not "open" the file in Word (easily done with a Process.Start) but to have load into Word as a new unsaved file. This is because certain propriety integrations for Word need to take over when a user saves the file and don't kick in if the file is ready saved but to a location on disk.
I've achieved this by using Interop calls to the Word application, adding the new document to Word's workspace. My problem is with Interop which tends to break on various client machines, particularly when Office upgrades take place (say a client had 32-bit office but upgraded with a 64-bit version).
I'm somewhat new to OpenXML, but can it be used to automate Word or is Interop my only real option?
object oFilename = tmpFileName;
object oNewTemplate = false;
object oDocumentType = 0;
object oVisible = true;
Document document = _application.Documents.Add(ref oFilename, ref oNewTemplate, ref oDocumentType, ref oVisible);
No, the Open XML technology has no way of interacting with the Office (Word) application - it's for file creation/manipulation, only. The interop is required in order to do anything with the Word application.
There is sort of a way around this - and it's only possible with Word, no other Office application has this - is to convert the Open XML content to the OPC flat-file format. This "concatenates" the various packages that make up the zip file to a pure text string, essetially a single XML file.
XML content in the OPC flat-file format can then be written to an already opened (even newly created) Word document using the Range.InsertXML method via "the interop". In a way, this "streams" the Open XML content into the opened Word document.
The problem with this approach is that certain document-level properties are not written to the target document, so not all aspects of the opened document can be changed. For example: page size, orientation, headers, footers... So if this kind of thing also needs to be affected the interop is required for such settings.
Related
I am building an extension that will open files from a remote server, but need to add some metadata into each document opened, which I will use later when the document is saved/closed.
Is there a way I can insert metadata into each document?
vscode.workspace.openTextDocument(filePath + fileName).then(doc => {
// Add some document specific metadata
vscode.window.showTextDocument(doc);
});
I understand that VSC does that, for example storing the caret and scroll positions, or selection if any, and so on, retrieving it when you reopen the file.
I believe it does that by storing data outside of the files, otherwise it would alter their data, or it would need to manipulate metadata according to the local system (Windows, macOS, Linux, etc.), which is not obvious, if even possible.
On Windows, in C:\Users<user name>\AppData\Roaming\Code, there are a number of databases which might fit this purpose, you can do a similar thing.
I have a Word 2016 VSTO that I developed using VS2015 and deployed to a Windows 10 machine using InstallShield. If I set the LoadBehavior to 3 it loads and works fine.
What I need is for it load on demand. I've tried just setting LoadBehavior to 9 but no dice. The web application that is creating the Word document (that ultimately loads the VSTO) was originally written for Word 2010 and is embedding the VSTO path in the Word document with an added GUID (C:\myAddIn.vsto|GUID|vstolocal). Opening the document created by the web application in Word 2016 throws this error when the VSTO tries to load:
Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException:
Could not create an instance of startup object myAddin.ThisAddIn
Any tutorials I've seen for VS2013/2015 do not reference embedding a GUID in the VSTO path; just set the LoadBehavior to 9.
There is no Publisher listed in File -> Options ->AddIns. The VSTO is listed under Inactive AddIns which I would expect until it is loaded on demand.
Any help is greatly appreciated.
If you want code to only execute with certain documents, such as you describe (from Comments)
I only want the AddIn to load for Word documents created by the web
application. Opening Word from the Start menu should not load the
AddIn.
then it is probably better to use a document-level customization, rather than an add-in.
The code attached to a document-level customization will load with the document, and unload when the document is closed. It's possible to create a document-level customization and distribute the document, or to attach the code at a later time using VSTO's ServerDocument class.
Since in your case the documents are being generated by a web app, using ServerDocument is indicated.
Here's the main content of the MSDN article:
=============================================
To attach managed code extensions to a document
In a project that does not require Microsoft Office, such as a console application or Windows Forms project, add a reference to the
Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll and
Microsoft.VisualStudio.Tools.Applications.Runtime.dll assemblies.
Add the following Imports or using statements to the top of your code file.
using Microsoft.VisualStudio.Tools.Applications;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
Call the static AddCustomization method.
The following code example uses the AddCustomization overload. This overload takes the full path of the document and a Uri that specifies the location of the deployment manifest for the customization you want to attach to the document. This example assumes that a Word document named WordDocument1.docx is on the desktop, and that the deployment manifest is located in a folder that is named Publish that is also on the desktop.
string documentPath = System.Environment.GetFolderPath(
Environment.SpecialFolder.Desktop) + #"\WordDocument1.docx";
int runtimeVersion = 0;
try
{
runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
// Make sure that this document does not yet have any Visual Studio Tools
// for Office customizations.
if (runtimeVersion == 0)
{
string deployManifestPath = System.Environment.GetFolderPath(
Environment.SpecialFolder.Desktop) + #"\Publish\WordDocument1.vsto";
Uri deploymentManifestUri = new Uri(deployManifestPath);
ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
}
else
{
System.Windows.Forms.MessageBox.Show("The document is already customized.");
}
}
catch (FileNotFoundException)
{
System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
}
catch (DocumentNotCustomizedException ex)
{
System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
ex.Message);
}
Build the project and run the application on the computer where you want to attach the customization. The computer must have the Visual Studio 2010 Tools for Office Runtime installed.
Did you try to set LoadBehaviour to 0x10 => Load first time, then load on demand? This should make Office load your addin the first time you execute it and internally cache it. It will then change the value to 0x9... The subsequent times, the app will load on demand.
I'm using OpenXml SDK to generate word 2013 files. I'm running on a server (part of a server solution), so automation is not an option.
Basically I have an xml file that is output from a backend system. Here's a very simplified example:
<my:Data
xmlns:my="https://schemas.mycorp.com">
<my:Customer>
<my:Details>
<my:Name>Customer Template</my:Name>
</my:Details>
<my:Orders>
<my:Count>2</my:Count>
<my:OrderList>
<my:Order>
<my:Id>1</my:Id>
<my:Date>19/04/2017 10:16:04</my:Date>
</my:Order>
<my:Order>
<my:Id>2</my:Id>
<my:Date>20/04/2017 10:16:04</my:Date>
</my:Order>
</my:OrderList>
</my:Orders>
</my:Customer>
</my:Data>
Then I use Word's Xml Mapping pane to map this data to content control:
I simply duplicate the word file, and write new Xml data when generating new files.
This is working as expected. When I update the xml part, it reflects the data from my backend.
Thought, there's a case that does not works. If a customer has no order, the template content is kept in the document. The xml data is :
<my:Data
xmlns:my="https://schemas.mycorp.com">
<my:Customer>
<my:Details>
<my:Name>Some customer</my:Name>
</my:Details>
<my:Orders>
<my:Count>0</my:Count>
<my:OrderList>
</my:OrderList>
</my:Orders>
</my:Customer>
</my:Data>
(see the empty order list).
In Word, the xml pane reflects the correct data (meaning no Order node):
But as you can see, the template content is still here.
Basically, I'd like to hide the order list when there's no order (or at least an empty table).
How can I do that?
PS: If it can help, I uploaded the word and xml files, and a small PowerShell script that injects the data : repro.zip
Thanks for sharing your files so we can better help you.
I had a difficult time trying to solve your problem with your existing Word Content Controls, XML files and the PowerShell script that added the XML to the Word document. I found what seemed to be Microsoft's VSTO example solution to your problem, but I couldn't get this to work cleanly.
I was however able to write a simple C# console application that generates a Word file based on your XML data. The OpenXML code to generate the Word file was generated code from the Open XML Productivity Tool. I then added some logic to read your XML file and generate the second table rows dynamically depending on how many orders there are in the data. I have uploaded the code for you to use if you are interested in this solution. Note: The xml data file should be in c:\temp and the generated word files will be in c:\temp also.
Another added bonus to this solution is if you were to add all of the customer data into one XML file, the application will create separate word files in your temp directory like so:
customer_<name1>.docx
customer_<name2>.docx
customer_<name3>.docx
etc.
Here is the document generated from the first xml file
Here is the document generated from the second xml file with the empty row
Hope this helps.
I have a document management system which stores files in a MS Word format. In my application, I would like to be able to open that document in Word.
I would like Word to handle all of the file system access out of the content management system. What I need to do is the following:
1) Create a new document based off a template, and then provide information that can be parsed and placed into specific fields.
I see I can do this as follows:
Runtime.getRuntime().exec("C:/Program Files (x86)/Microsoft Office/Office15/winword.exe /ttemplate_name");
My assumption here is that the template is installed on the local drive. However I would like to provide some data so that fields could be prepopulated and I am not sure how to do that?
2) I would like to be able to run a macro to open the document directly from the content management system. I think I can run a macro as follows:
Runtime.getRuntime().exec("C:/Program Files (x86)/Microsoft Office/Office15/winword.exe /mmacro_name");
However, in this case, I would need to provide the document id from the content management system so that it can retrieve it and open it.
I am unsure what switch or parameter I can use to provide the additional data for word?
Thanks!
Word provides no command-line facility to pass arguments or data when opening or creating a document.
As long as macro code is available, the macro can read data that's stored somewhere, such as in an XML file. But the file path would need to be hard-coded or derivable from a known location (path).
You don't necessarily need to call a macro in a document (or template attached to the document). If the macro is named AutoNew or AutoOpen it will execute automatically when a document is created from the template or, respectively, when a document is opened.
How can i open a file directly from stream which is exported through crystal report export to stream function? I am using vs2010 and sap crystal report.
MemoryStream m = (MemoryStream)(PReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
FileStream f = new FileStream(purchaseCombo.SelectedItem.ToString(),FileMode.Create,System.IO.FileAccess.Write);
byte[] bytes = new byte[m.Length];
m.Read(bytes, 0, (int)m.Length);
f.Write(bytes, 0, bytes.Length);
f.Close();
m.Close();
It depends on the file that you are exporting. Since Crystal Reports can export a number of different file types and each of them has a specific program that can handle it, you will need to search for the exact file type that you're interested. For example with a quick search on the internet, I found that a stream of an html file can be directly previewed inside a WebBrowser component (in a WPF app) by using the NavigateToStream method. Maybe there are some 3rd party components that expose methods for directly opening files from memory.
Though, the easiest way would be to export a temporary file to disk instead of memory by using the ExportToDisk method and delete it after its usage, since many components read from paths rather than memory. Before exporting the file, you can use the GetTempFileName method which creates and names a temporary file.