A
AliR \(VC++ MVP\)
Hi Everyone,
I am not sure if this is the correct newsgroup for this question.
What I am trying to do is open a word document and read some text out of it.
So far I have had a great start. I am using the Office object model, and I
am
able to retrieve alot of the information that I need except for one thing
the text with RTF formatting information! I am able to enumerate the
paragraphs and ask each one for their style, and their text, but the only
text I have been able to get out of it has been just the text without any
RTF formating. Anyone know how to get the RTF data. I even tried
CParagraph::get_FormattedText, but that returns plain text also. I wonder
if there is a way to tell it that I want CF_RTF.
Here is the quick and dirty test code so far.
CDocuments, CDocument0, CApplication, CParagraphs, CParagraph, and CRange
are wrapper classes for the interfaces created by VStudio. (Please overlook
the bad error handeling as this is just test code)
CDocument0 Doc;
COleVariant True( (short)TRUE),
False((short)FALSE),
Long((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
try
{
CFileDialog Dlg(true,"*.doc");
if (Dlg.DoModal() == IDOK)
{
CApplication App;
if(!App.CreateDispatch("Word.Application"))
{
AfxMessageBox("Coud Not Create The Application Object");
return;
}
App.put_Visible(FALSE);
CDocuments Documents(App.get_Documents());
Doc.put_UpdateStylesOnOpen(TRUE);
Doc.AttachDispatch(Documents.Open(COleVariant(Dlg.GetPathName()),Long,Long,L
ong,Long,
Long,Long,Long,Long,Long,Long,Long,Long,Long,Long,Long));
Doc.UpdateStyles();
CStyles Styles;
Styles.AttachDispatch(Doc.get_Styles());
CParagraphs Paragraphs;
Paragraphs.AttachDispatch(Doc.get_Paragraphs());
LPDISPATCH lpDisp = Paragraphs.get_First();
while (lpDisp != NULL)
{
CParagraph Paragraph;
Paragraph.AttachDispatch(lpDisp);
CRange wdRange;
wdRange = Paragraph.get_Range();
//here is where I get the text, but it doesn't have the RTF
stuff
CString Text = wdRange.get_Text();
Text.Trim();
if (!Text.IsEmpty())
{
CStyle Style;
Style.AttachDispatch(Styles.Item(&Paragraph.get_Style()));
Text += CString(" - ") + Style.get_NameLocal();
MessageBox(Text);
}
lpDisp = Paragraph.Next(Long);
}
}
}
catch (CException *e)
{
char Error[255];
e->GetErrorMessage(Error,254);
MessageBox(Error,"Exception Thrown");
e->Delete();
}
Doc.Close(False,Long,Long);
AliR.
I am not sure if this is the correct newsgroup for this question.
What I am trying to do is open a word document and read some text out of it.
So far I have had a great start. I am using the Office object model, and I
am
able to retrieve alot of the information that I need except for one thing
the text with RTF formatting information! I am able to enumerate the
paragraphs and ask each one for their style, and their text, but the only
text I have been able to get out of it has been just the text without any
RTF formating. Anyone know how to get the RTF data. I even tried
CParagraph::get_FormattedText, but that returns plain text also. I wonder
if there is a way to tell it that I want CF_RTF.
Here is the quick and dirty test code so far.
CDocuments, CDocument0, CApplication, CParagraphs, CParagraph, and CRange
are wrapper classes for the interfaces created by VStudio. (Please overlook
the bad error handeling as this is just test code)
CDocument0 Doc;
COleVariant True( (short)TRUE),
False((short)FALSE),
Long((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
try
{
CFileDialog Dlg(true,"*.doc");
if (Dlg.DoModal() == IDOK)
{
CApplication App;
if(!App.CreateDispatch("Word.Application"))
{
AfxMessageBox("Coud Not Create The Application Object");
return;
}
App.put_Visible(FALSE);
CDocuments Documents(App.get_Documents());
Doc.put_UpdateStylesOnOpen(TRUE);
Doc.AttachDispatch(Documents.Open(COleVariant(Dlg.GetPathName()),Long,Long,L
ong,Long,
Long,Long,Long,Long,Long,Long,Long,Long,Long,Long,Long));
Doc.UpdateStyles();
CStyles Styles;
Styles.AttachDispatch(Doc.get_Styles());
CParagraphs Paragraphs;
Paragraphs.AttachDispatch(Doc.get_Paragraphs());
LPDISPATCH lpDisp = Paragraphs.get_First();
while (lpDisp != NULL)
{
CParagraph Paragraph;
Paragraph.AttachDispatch(lpDisp);
CRange wdRange;
wdRange = Paragraph.get_Range();
//here is where I get the text, but it doesn't have the RTF
stuff
CString Text = wdRange.get_Text();
Text.Trim();
if (!Text.IsEmpty())
{
CStyle Style;
Style.AttachDispatch(Styles.Item(&Paragraph.get_Style()));
Text += CString(" - ") + Style.get_NameLocal();
MessageBox(Text);
}
lpDisp = Paragraph.Next(Long);
}
}
}
catch (CException *e)
{
char Error[255];
e->GetErrorMessage(Error,254);
MessageBox(Error,"Exception Thrown");
e->Delete();
}
Doc.Close(False,Long,Long);
AliR.