Click to See Complete Forum and Search --> : [RESOLVED] How to load a word document into an RTB without getting wordapp an the screen ?


JonnyPoet
March 16th, 2009, 03:22 PM
Hi friends !
I want to read a worddoc into a richtextbox but when I do it like the followingpublic void ReadWordStream(string path) {
ApplicationClass wordApp = new ApplicationClass();
object file = path;
object readOnly = false;
object isVisible = false;
object missing = System.Reflection.Missing.Value;
wordApp.WindowState = WdWindowState.wdWindowStateMinimize;
wordApp.Visible = false;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file, 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);
wordApp.Visible = false;
doc.ActiveWindow.Selection.WholeStory();
string text = doc.ActiveWindow.Selection.Text;
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
rtbDoc.Paste();
object boolObj = (object)false;
wordApp.Documents.Close(ref boolObj, ref boolObj, ref boolObj);
wordApp.Quit(ref boolObj, ref boolObj, ref boolObj);
} The wordApplication opens and closes which looks terrible.

Is there a way to read out the text within formating to my RTB without opening the word Application. ( It's word 2003 ) As you can see I tried to minimize, I tried to get visible to false. Maybe there is a way to access the word document without opening word ?application

sotoasty
March 16th, 2009, 03:48 PM
have you tried...



Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;
object file = @"c:\test.doc";
object readOnly = false;
object isVisible = false;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file, 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);
wordApp.Visible = false;
doc.ActiveWindow.Selection.WholeStory();
string text = doc.ActiveWindow.Selection.Text;
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
rtbDoc.Paste();
object boolObj = (object)false;
doc.Close(ref boolObj, ref boolObj, ref boolObj);
wordApp.Quit(ref boolObj, ref boolObj, ref boolObj);


This worked for me. The first time I did see a flash but then I made a change the the bolded line and no flash. I think that would be the only line you might need to change.

JonnyPoet
March 16th, 2009, 06:45 PM
Wooow. This small difference has cost me one day and was solved now within a few minutes :thumb:

Really great help, I was really astonihed why this hasn't worked the way I wanted it to work