I am trying open a word document for manipulation. However in the code below 'wordApp.Selection' is always null
object missing = System.Reflection.Missing.Value;
Word._Application wordApp = new Word.Application();
Word._Document aDoc =wordApp.Documents.Open(ref fPath1, 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);
Word.Selection selection = wordApp.Selection
Any ideas on how what I am missing here. Why is wordApp.Selection getting set as null.?
Thanks.
I believe that you have to set the selection before it will have a value -- when you open a document in Word, it does not automatically select the entire document, you would have to do that programmatically before Selection has a value.
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 code below -I try to save Word document file as .htm. Can anybody solve this problem?
objWord.Documents.Open(ref FileName, 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, ref missing);
Document oDoc = objWord.ActiveDocument;
oDoc.SaveAs(ref FileToSave, ref fltDocFormat, 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);
I was able to solve the problem by creating Desktop folder in C:\Windows\SysWOW64\config\systemprofile\
and C:\Windows\System32\config\systemprofile\
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.
It must be able to handle css and javascript as the page contains "highcharts" javascript charts.
It doesn't have to be an external process, a library would be great too.
Basically I need to be able to create a word document containing highcharts as images along with some text and other graphics.
Any ideas would be great.
Look at this as an example
It depends a lot on the nature of the html document you are trying to convert. One simple way is just to use the Word automation to open an .html document and then save it as a .doc document.
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value; // Values we don't care about
object fileName = "C:/webpage.htm";
object newFileName = "C:/webpage.doc";
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// word.Visible = true; // To see what's happening
Microsoft.Office.Interop.Word.Document document = word.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 missing, ref missing, ref missing, ref missing, ref missing);
document.Activate();
object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
document.SaveAs(ref newFileName, ref saveFormat, 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);
document.Close(ref missing, ref missing, ref missing);
Note
You have to add a reference to Microsoft.Office.Interop.Word or something similar
The number of ref missing arguments depends on wich version of Word you are using
You have to use full paths in the filename as the Word instance starts from the System folder.