How do I save this object to a file? - c#

I have some word documents with objects inside them. I am testing one that has 3 pdf-files (the wordApp.Selection.InlineShapes.Count matches this), But else I have trouble getting any info from the objects. How do I save it to a disk? Any help is appreciated, because the inlineShape.OLEFormat.IconLabel is an empty string in all 3 instances.
var wordApp = new Word.Application();
object confirmConversions = false;
object readOnly = true;
object missing = Type.Missing;
this.document = wordApp.Documents.Open(
ref fn, ref confirmConversions, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing);
string applicationName = null;
foreach (Microsoft.Office.Interop.Word.InlineShape inlineShape in this.document.InlineShapes)
{
applicationName = inlineShape.OLEFormat.IconLabel;
}

Here's a snippet from a post which I found here which explains the approach you need to take in order to do this:
Any file that can be embedded in a
document as an OLE object can be
extracted. However, we may not be
able to provide you with a simple code
example if the technology doesn't
belong to us (such as Adobe Acrobat
files).
What we are doing with Office objects
is activating the object and then
taking advantage of the exposed
IDispatch interface so that we can use
COM interop to communicate directly
with the object's programming model.
As it happens, the Office applications
generally expose SaveAs methods that
we can call to save the files in
question. Going through the Office
programming model in this fashion is a
handy shortcut that enables saving
embedded objects with very little
code.
I suspect that Adobe Acrobat exposes a
similar programming model because
there is an Adobe Acrobat Type
Library. You will have to browse the
Type Library to see if it exposes some
sort of Save or Save as method. If it
does, you can add it as a reference to
your project (via the COM References
tab of the Add Reference dialog in
Visual Studio) and take a similar
approach as Ji suggests in his post
above.
(continues...)

If you are open to using 3rd party controls, this can be easily accomplished using Aspose.Words:
Aspose.Words.Document d = new Document(#"C:\users\john\desktop\embeddedPDF.docx");
foreach (Aspose.Words.Drawing.Shape shp in d.GetChildNodes(NodeType.Shape, true))
{
shp.OleFormat.Save(#"C:\Temp\testoutput.pdf");
}

Related

Open Microsoft word document from web project to any local machine

Below is my code on click to button. If I will run project using localhost it will open word document very well but when I am going to host this project on IIS and try to open it from another machine by IP it will transfer my page to error message.
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object file = "D:\\poForM.docx";
object objFalse = false;
object objTrue = true;
object missing = System.Reflection.Missing.Value;
object emptyData = string.Empty;
object readOnly = false;
object visible = true;
wordApp.Visible = true;
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref file, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, visible, ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
Your code is relying on there being an instance of Microsoft Word on the server (Microsoft.Office.Interop.Word.ApplicationClass)... and having the object file at a specified location on the server.
If I understand correctly what you are trying to do you want to host the Word document on your IIS server but download it onto the local machine for editing..
You can do this by providing a link in your Web page to where the Word doc is, for example:
Open Word Document
Then when you click the link the browser will download the doc and open it in Word (assuming it's installed locally)
Of course if I've completely misunderstood what you're trying to do feel free to comment....
I believe you want to achieve SharePoint alike behaviour where the user can open the file and then save it back to server. Here is a similar thread Possible for Word to edit documents directly off an web server without Sharepoint? . The only bad thing about this solution is that AFAIK it is working only in IE. You can also try new ActiveXObject("Word.Application");instead of the new ActiveXObject("SharePoint.OpenDocuments");

Office 2007 PIA - Embed non-text files

I am working with the Office 2007 PIAs and am trying to generate a large document from x number of user selected documents. Most of these documents will likely be word docs, but we want to support any file type.
Text documents work fine when inserted using InsertFile. Pictures can be inserted using InlineShapes.AddPicture. How do I embed other document types? I am looking for the functionality equivalent to drag and dropping any file into Word. If I try to use the InsertFile method, it just writes out the binary content of non text files.
To embed non-text files in Word via Interop, you use InlineShapes.AddOLEObject
I am looking for the functionality equivalent to drag and dropping any file into Word.
This does the trick for me:
public void InsertFile(Microsoft.Office.Interop.Word.Selection CurrentSelection, string FileName)
{
object FileName = fileName;
object missing = Type.Missing;
CurrentSelection.InlineShapes.AddOLEObject(ref missing, ref FileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
Hope this helps.

Application.Documents.Add return null

I try to use Microsoft.Office.Interop.Word to export a word by a word template.
Some related code below:
//strTemppath is a path for my word template(.doc).
object oTemplate = strTemppath;
var WordDoc = WordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
This works well locally. However, after I move my application to the server the "WordDoc" object always return null. How could this happen? Is there anything to do with the configuration of the server? such as "permission" or "Com components" or anything else?
Thanks in advance

A .net winforms component to compare two documents?

I am looking for a .net winforms component that can compare two formatted documents (in .doc, .docx, .html, .rtf, any one of them will do) and visually spot changes. We prefer to see the changes as MS Word does when it shows the changes in its track changes mode
We expect short documents of only few pages long and not much editing (few words changed, a paragraph added/deleted, etc)
Are you aware of such a component that you can recommend free or otherwise
Thank you,
Kemal
Following code will compare two word doc and save the merging of changes in third doc.
Add reference of Microsoft Word 12.0 Object Library
using Microsoft.Office;
public static void comp()
{
object missing = System.Reflection.Missing.Value;
//create a readonly variable of object type and assign it to false.
object readonlyobj = false;
object filename = "C:\\romil1.docx";
//create a word application object for processing the word file.
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
//create a word document object and open the above file..
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
ref filename, ref missing, ref readonlyobj, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
string filenm = "C:\\romil2.docx";
object filenm3 = "C:\\romil3.docx";
doc.TrackRevisions = true;
doc.ShowRevisions = false;
doc.PrintRevisions = true;
doc.Compare(filenm);
doc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
app.ActiveDocument.SaveAs(ref filenm3, ref missing, ref readonlyobj, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
app.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges);
MessageBox.Show("Process complete");
}
You can use one of the following libraries to manipulate Word document and build the document comparison method yourself.
Microsoft Interop (Office installation required)
OpenXML SDK
Aspose.Words for .NET
Since this question is old, now there are more solutions available.
Groupdocs compare
Document Comparison by Aspose.Words for .NET
I work with Aspose as Developer Evangelist.

Comparing two RTF documents side-by-side in Word (VSTO)

For my VSTO Word solution, I need to programatically "compare" two documents side-by-side. In other words I need to, from code, perform the equivalent of clicking the View > Show Side by Side button.
I tried using the CompareSideBySideWith method after loading two documents. An exception is thrown: "The requested member of the collection does not exist". I am not the first to encounter this; see Microsoft's (boilerplate, not particularly helpful) replies in this thread. The MS rep ended up scratching her head and giving up.
I even tried opening two blank documents and comparing them. This time no exception, but the compare didn't happen and CompareSideBySideWith() returned false.
Document doc1 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
object doc2 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
doc1.Windows.CompareSideBySideWith(ref doc2);
Has anyone discovered a workaround for this? It seem a pretty basic piece of functionality to have a in a custom solution.
Note: We need to call the actual "Side by Side" compare, not just arrange the windows via Windows.Arrange(). This is partly because our ribbon contains an alias for the View Side by Side button, which won't be turned on (pressed in) unless the actual Side by Side command is called successfully.
Update: The exception was still thrown in the above example involving two new documents; Word swallowed the exception because I tried it outside of my try-catch block.
Per Otaku below I tried calling doc2.Windows.Compare(ref doc1) instead, and this worked for blank documents as well as test documents saved as .docx and .rtf from Word 2007.
However, we need to compare documents saved as RTF from another RTF editor. When I load one of our documents, it fails. To reproduce my error, try loading RTF documents saved from WordPad--these fail as well. I've tried tinkering with the Encoding and Format parameters of Documents.Open() to no avail. It would be nice to avoid having to convert and save the temp file as .docx, particularly for larger documents! Also note that I can click View Side by Side after opening the WordPad-saved RTF files manually, and it works.
Also, it only seems to matter what format the compare document (the document being passed as parameter to Windows.CompareSideBySideWith() is in. For example, if we are doing doc2.Windows.CompareSideBySideWith(ref doc1) as in Otaku's example, it works when doc1 is a regular docx but not when it's an RTF saved from WordPad. (Regardless of where doc2 came from).
Update 2:
As usual, one line of code resolves several days of chasing one's tail:
doc1.Convert(); // Updates the document to the newest object model (i.e. DOCX)
Can now compare side-by-side without a problem.
Reverse the compares of your documents and it should be fine:
For new documents
Document doc1 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Document doc2 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
object o = doc1;
doc2.Windows.CompareSideBySideWith(ref o);
For existing documents
object missing = System.Reflection.Missing.Value;
object newFilename1 = "C:\\Test\\Test1.docx";
Document doc1 = this.word.Documents.Open(ref newFilename1, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object newFilename2 = "C:\\Test\\Test2.docx";
Document doc2 = this.word.Documents.Open(ref newFilename2, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object o = doc1;
doc2.Windows.CompareSideBySideWith(ref o);
If your app isn't visible or you are launching a new instance of Word, you should set this.word.Visible = true; before running the opening of documents as CompareSideBySideWith is a UI routine.

Categories