Open Microsoft word document from web project to any local machine - c#

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");

Related

Open and modify Word Document

I want to open a word file saved in my server using "Microsoft.Office.Interop.Word".
This is my code:
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object fileName = "http://localhost:52099/modelloBusta/prova.dotx";
Microsoft.Office.Interop.Word.ApplicationClass applicationWord = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document modelloBusta = new Microsoft.Office.Interop.Word.Document();
try
{
modelloBusta = applicationWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing, ref missing, ref missing, ref missing);
modelloBusta.Activate();
}
catch (COMException eccezione){
Console.Write(eccezione);
modelloBusta.Application.Quit(ref missing, ref missing, ref missing);
}
In the windows task manager the process is present, but the "word document" doesn't appear (the application does not start).
What is the problem?
Thanks in advance.
You need to make sure that the Word application window actually is made visible when automating Word like that:
var applicationWord = new Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
first add the dll of office.interop by adding directly to the resources then add this using directive:
using Microsoft.Office.Interop.Word;
and use the following code
Application ap = new Application();
Document document = ap.Documents.Open(#"C:\invoice.docx");;
http://support.microsoft.com/kb/257757
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
http://freeword.codeplex.com/
Document document = new Document();
document.LoadFromFile("test.doct");

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

Transfer Word Document Contents to Web Environment on Server Side Using C# ASP.NET

We have some contents in Ms Word .docx formats, prepared by our customers.
These documents may have equations, images, etc.
We want to transfer these contents to our web environment.
Firstly, I plan to use TinyMCE "paste from word" plugin and fmath editor plugin. No use...
Then I decide to put upload button to transfer ms word contents and showing resulting web contents into TinyMCE editor. Actually something like writing a new plugin.
I am using Microsoft.Office.Interop.Word.Document class's "SaveAs" method.
But I have following problems:
1) I can not change document resources folder path. It generate "..._files" folder same with generated html file. I want to transfer all resources to appropriate places on the server.
2) I can not change the image source paths as absolute paths.
3) Too many garbage styles, codes on generated html file.
I may totally in wrong way to achieve this purpose. So I decided to get your advices, before continue in this directions. I am open any suggestion.
Regards,
I am adding draft version of this code:
var fileName = Request["docfilename"];
var file = Request.Files[0];
var buffer = new byte[file.ContentLength];
file.InputStream.Read(buffer, 0, file.ContentLength);
var root = HttpContext.Current.Server.MapPath(#"~/saveddata/_temp/");
var path = Path.Combine(root, fileName);
using (var fs = new FileStream(path, FileMode.Create))
{
using (var br = new BinaryWriter(fs))
{
br.Write(buffer);
}
}
Microsoft.Office.Interop.Word.ApplicationClass oWord = new ApplicationClass();
object missing = System.Reflection.Missing.Value;
object isVisible = false;
word.Document oDoc;
object filename = path;
object saveFile;
oDoc = oWord.Documents.Open(ref filename, 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);
oDoc.Activate();
object path2 = Path.Combine(root, "test.html");
object fileFormat = word.WdSaveFormat.wdFormatFilteredHTML;
oDoc.SaveAs(ref path2, ref fileFormat, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing);
oDoc.Close(ref missing, ref missing, ref missing);
oWord.Application.Quit(ref missing, ref missing, ref missing);
This is a delicate matter. I was facing the same problem as doc has a lot of style tags. If you notice, try to share a url (which has word doc content) on facebook, then in the description/summary of url, the unwanted tags used to come :) So I guess the issue is persistent there too. I would suggest, go through the basics of Information Retrieval and try to intelligently strip the style tags. You will be required to write most of your stripping code with regular expressions

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.

How do I save this object to a file?

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");
}

Categories