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");
Related
I want to add gridview in the following code.
How do I add a Gridview into a word document?
My Word document creation code ;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document aDoc = null;
DateTime today = DateTime.Now;
object readOnly = true;
object inVisible = true;
aDoc = wordApp.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 inVisible, ref missing, ref missing, ref missing, ref missing);
this.FindAndReplace(wordApp, "##formkodu##", TextBox1.Text);
this.FindAndReplace(wordApp, "##sirketadi##", DropDownList11.SelectedItem.Text);
this.FindAndReplace(wordApp, "##il##", ddliller.SelectedItem.Text);
this.FindAndReplace(wordApp, "##isletme##", ddlisletmeler.SelectedItem.Text);
this.FindAndReplace(wordApp, "##yüklenicifirma##", ddlyükleniciler.SelectedItem.Text);
wordApp.Visible = false;
aDoc.Activate();
aDoc.SaveAs(ref saveAs, 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);
wordApp.Quit(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(aDoc);
Gridview is not a COM ActiveX control, so it cannot be hosted on Word's document surface - at least, not directly.
If you were able to use VSTO then you could use VSTO's built-in tools to wrap the Gridview in a COM ActiveX control in order to place it on the document surface. VSTO is not supported in ASP.NET, however.
A possible way around this could be do develop a VSTO Add-in that's installed on the machines where the document you create is opened. That could take care of wrapping, inserting and managing the ActiveX control + Gridview.
But you might be better off to simply generate a Word table in the document? That works fine using the Interop (or Open XML)...
There is some old information about the basics for creating an ActiveX control on MSDN, by Geoff Darst: https://social.msdn.microsoft.com/Forums/vstudio/en-US/71a75dc4-dcea-454a-9e4a-011a2f811994/vsto-activex-and-powerpoint?forum=vsto
https://social.msdn.microsoft.com/Forums/vstudio/en-US/4282a65c-ccd7-4fd4-a56c-75f84615fff6/embedding-active-x-control-in-office-application-using-vsto-2005?forum=vsto
Instead of interops i would prefer using OpenXML (*.docx) if it possible. How to create a table is documented here: How to: Insert a table into a word processing document (Open XML SDK). With this you don't need interops, which can cause a lot of trouble if the wrong office version is installed or any other issue. Hope this helps.
I am using Microsoft.Office.Interop to open, manipulate and save a Word document file (.doc).
I can get all Text contents but no success in loading added controls (i.e. TextBoxes) in the opened word document.
I get the text using following command
Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document oWordDoc = oWordApp.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);
oWordDoc.Activate();
oWordApp.Selection.TypeParagraph();
string test = oWordDoc.Content.Text;
How can I have access to all controls included in the base word document?
Thanks.
check this:
Word.Document oDoc=...;
foreach (Word.Shape shape in oDoc.Shapes)
{
//do some thing with shape
}
By changing
oWordApp.Selection.TypeParagraph();
To
oWordApp.Selection.WholeStory();
And digging in oWordDoc.shapes, I gained access to all controls.
I need to be able to change the name of the default document from Document1 to Report when a Word document is started from my application. The problem is that the name property in the Document object is read only. Any idea on a method I can call at startup that changes the name?
You might be interested in this snippet of code:
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
object missing = System.Reflection.Missing.Value;
object fileName = "Report";
object isReadOnly = false;
object isVisible = true;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref isVisible);
doc.SaveAs2(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref isReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wordApp.Visible = true;
This will pop open a new Word document named "Report" as you specified. Notice this uses the concept I mentioned in the comment, that is, it saves the file first with a new name then opens it. In this case, the default location is probably your User's "Documents" folder, but you can specify the path as needed.
Don't forget to close and release the COM objects "doc" and "wordApp" as needed. Sometimes the GC doesn't mop it all up appropriately, especially if the application closes unexpectedly or if you forget to close any of them when you're done.
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
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.