Embedding a word file as a resource - c#

I am trying to access a word file from my c# code. I want to embed it as part of a project so that when i call a word doc object to be read from an aspx page, the object should have access to that word doc no matter where it is being used from. So how can i include a word doc as a resource in my project? Also in order to open my word doc, i need a path for that doc. After i have included the file as a resource, how do i somehow get its "path" so the word doc object can be created? Is there someway to copy it over to a temp location on whatever machine is calling that object?
Thanks

Note: Do not use MSWord in server environment.
How to embed file as resource
After getting the stream use Stream.CopyTo to save it to FileStream created on temporary file Path.GetTempFileName or in temp folder .

Related

Add Image Stored in Project Folder to Microsoft Interop Word Document

I have a C# Web API and I am using Microsoft.Office.Interop to dynamically create a Word document. I have an image that I wish to add to said document. This image is stored within the project itself and not on a drive somewhere.When I add the image to the document I'm met with an error with states "This is not a valid file name". The file is definitely in the folder. Is there a special way to get C# to see the folder? I know in Javascript you would simply put ".\Images\Watermark.png". Is C# capable of handling something like that?
I ended up creating a Mail Merge document instead of trying to create the document via C#.

Accessing a .docx file's XML programatically?

If you take a .docx file, rename it to .zip, and unzip it, you can view its .xml files. I'm building a program to programmatically inspect these XML properties (no existing API seems to suffice as our company is using a 3rd party program that attaches custom XML to files, and that program does not have an API).
Is there a clean way to access this XML without programmatically saving copies of files as .zip files, opening them, taking out only the XML and then deleting the rest?
use openxml sdk to fetch all the xml elements
WordprocessingDocument document = WordprocessingDocument.Open(this.FilePath, true);
MainDocumentPart mainPart = document.MainDocumentPart;
List<OpenXmlElement> ParagraphElements = new List<OpenXmlElement>();
foreach (var i in mainPart.Document.ChildElements.FirstOrDefault().ChildElements)
{
ParagraphElements.Add(i);
}
Here is your complete solution,
From ParagraphElements all XML elements can be retrieved.
This's easy way to access XML elements present in it.
Have you tried the Open XML SDK for Office?
Allows you to access the xml files inside .docx files.

Create file info from resources c#.NET

I have in the resources of my visual studio C# projetc a xlsx file and I want to manipulate it by FastExcel library (https://github.com/mrjono1/FastExcel), but, like see you in the github's page code, I have to create a FileInfo object and it has only one constructor that wants a file's path but only link that I have of the file is the stream that I get by this line of code:
test.Properties.Resources.test1
How do I pass throught from link to resources to path for create a FileInfo?
Well you can write the Stream to disk and provide the file path to the FileInfo constructor. I would create a seperate utility that can accepts a Stream and returns a file path.

How to open Word document from memory stream

In our WPF application, we must create a Word document and open it automatically.
Today we ask the user to specify the file name and location BEFORE we create the document and them open the file using the Process.Start method.
Now, I want to create the Word document in memory stream, open it and eventually, when the user decides to save the document in Microsoft Word, he will have to specify the location and name. So I will not be using the SaveFileDialog in my app.
This is similar to when you start Microsoft Word. A default document is created and when you click save, you will guided to the "save as" function.
How can I do that?
I don't think you can do this purely on the memory stream. However, I would save the memory stream to a temporary file, by saving to the Temp folder or AppData\temp or something like that with a randomly-generated name, and then mark that file as read-only. Then open word on that file (with System.Diagnostics.Process or however you are doing it), and since it is read-only, it will ask the user to save changes when they exit.
Just programmatically create a new Word document using the standard Microsoft.Interop.Word .Net namespace:
How to: Programmatically Create New Documents
Note that you might need to install MS-Office to do this.
Note, too, that your application can display it's own "Save As" dialog (or, for that matter, could just use a hard-coded path) independent of Word. Your program chooses the path - and your program writes the Word document object if/when it's ready.
Finally, here's an alternative, open source library, DocX:
http://www.codeproject.com/Articles/660478/Csharp-Create-and-Manipulate-Word-Documents-Progra
How to: Open a word processing document from a stream (Open XML SDK)
ADDENDUM:
Thank you for your clarification:
You're using the OpenXML SDK (as an alternative to .Net Interop or DocX libraries I mentioned above). You've already created an in-memory document object.
Now you want the user to be able to open the document object in Word (presumably to review it), before saving it (to a filename of his/her own choosing).
You can't (easily) do that :)
One option, suggested by sovemp above:
a. OpenXML writes to a temp file (so it can be opened in Word)
b. Make the temp file read-only (to force the user to do an explicit "Save As")
c. Use .Net Process.Start() to invoke MSWord (specifying your temp file in your command line).
Another option might be to use a "Preview Handler":
http://www.codeproject.com/Articles/25465/Using-Vista-Preview-Handlers-in-a-WPF-Application
https://previewhandlers.codeplex.com/
Still another option might be to serve the memory stream in a web browser, using HTTP ContentType "application/msword" (for example)
Finally, if all you want is for the user to specify a filename (if "preview" isn't essential), you can always pop up your own "Save as" dialog and write document to disk directly from OpenXML.

Copy content from some other word file into currently active word document without opening the other word file

I am creating an Add In for Microsoft Word and I have an active document open in my Word Application object.
using tw = Microsoft.Office.Tools.Word;
using w = Microsoft.Office.Interop.Word;
using wp = DocumentFormat.OpenXml.Wordprocessing;
tw.Document doc =
Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
I need to copy some content from another word document file into this doc object at current cursor position, without opening that source document file.
Update:-
By saying, without opening the source file, I am making a work around for me, and yes, that means, I don't want to show that source file open in MS Word - with a Word Method that will do this automatically if it exist, without making a hack of opening the Source Word file invisibly.
Originally, what I need is - I have that source file saved as a Binary Object (BLOB) in my database, and would like to load it directly in a MemoryStream and copy contents from it to the Active Word Document. But, because I am not getting a way to do this, I thought I will save that source file temporarily on the disk, and then copy its contents into the active document, provided it is not visible in the MS Word while copying.
Is this possible?
Any API available for doing this?

Categories

Resources