请教一个word文档读取与存储的问题

fall1979 2004-10-29 02:50:40
我做的一个程序要求在程序主界面上能读出word文档的内容,我用了一个TRichEdit控件来显示文档的内容。
基于word文档的操作我用了TWordApplication和TWordDocument两个控件,因为对这两个控件不熟悉,现在遇到这么几个问题:
1、如何从word文档中读出内容显示到RichEdit中,读取的过程中不能丢失文档内容本身的字体、大小、段落等信息;
2、如何生成一个word文档将RichEdit中的内容保存在该文档中,同样不能丢失内容文字的字体、大小、段落等信息;

...全文
249 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fall1979 2004-11-01
  • 打赏
  • 举报
回复
SOS,着急啊!
fall1979 2004-11-01
  • 打赏
  • 举报
回复
有人帮忙吗?
ktcserver 2004-10-30
  • 打赏
  • 举报
回复
blog.csdn.net/ktcserver
fall1979 2004-10-30
  • 打赏
  • 举报
回复
xrdsheng(旭日东升),你说的那个Demo我看了,但是里边没有我需要的保存之类的!
ktcserver(飘雪流风)提供的blog.csdn.net/ktcserver我原来也看过,里边关于word的还不够全面!
不过,还是要谢谢你们。^_^

我现在通过以下代码:
WordApp->Connect();
WordApp->Documents->Add();
WordDocument->ConnectTo(WordApp->Documents->Item(Variant(1)));
RichEdit->SelectAll();
RichEdit->CopyToClipboard();
WordDocument->Sentences->Last->Paste();
将RichEdit中的内容复制到了word文档中(字体什么的都不会改变),关键现在是如何将这个文档通过运行代码保存起来,而不需要终端用户的干预。我用了WordDocument的SaveAs函数
WordDocument->SaveAs(Variant("D:\\TT1.doc"));
但是没有保存起来。我看了SaveAs的函数定义如下:
void __fastcall SaveAs(TVariant* FileName/*[in,opt]*/= TNoParam(),
TVariant* FileFormat/*[in,opt]*/= TNoParam(),
TVariant* LockComments/*[in,opt]*/= TNoParam(),
TVariant* Password/*[in,opt]*/= TNoParam(),
TVariant* AddToRecentFiles/*[in,opt]*/= TNoParam(),
TVariant* WritePassword/*[in,opt]*/= TNoParam(),
TVariant* ReadOnlyRecommended/*[in,opt]*/= TNoParam(),
TVariant* EmbedTrueTypeFonts/*[in,opt]*/= TNoParam(),
TVariant* SaveNativePictureFormat/*[in,opt]*/= TNoParam(),
TVariant* SaveFormsData/*[in,opt]*/= TNoParam(),
TVariant* SaveAsAOCELetter/*[in,opt]*/= TNoParam())
我现在想问的是:
(1)如何通过SaveAs这个函数将文档保存起来啊?也就是说这个函数该怎么用。
(2)还有打开一个现有word文档是否用WordApp->Documents中的Open函数,这个函数又该如何用。
(3)WordDocument中如何将全部内容全部选定并复制到粘贴板上?是不是这样就行
WordDocument->Sentences->Last->Copy();
TypeCsdnLearn 2004-10-29
  • 打赏
  • 举报
回复
对,看例子就行
xrdsheng 2004-10-29
  • 打赏
  • 举报
回复
方法2:


Variant V;
AnsiString str;
int i;
V=CreateOleObject("Word.Basic");
V.Exec(Procedure("AppShow"));
V.OlePropertySet("Visible", (Variant)true);
V.Exec(Procedure("FileNew") << "Normal");
Query1->SQL->Add("select * from cmlogtb");
Query1->Open() ;
while(!Query1->Eof )
{
str="";
for(i=0;i< (Query1->FieldCount -1);i++)
str=str+Query1->Fields->Fields[i]->AsString + " " +13;
V.Exec(Procedure("Insert") << str);
Query1->Next() ;
}
Query1->Close() ;
V.Exec(Procedure("FileSaveAs") << Edit1->Text);
V.Exec(Procedure("AppClose"));
Button1->Caption = "±£´æÍê±Ï";
xrdsheng 2004-10-29
  • 打赏
  • 举报
回复
在Borland\CBuilder6\Example\PWordDemo有一例子,部份代码如下:

__fastcall TPWordForm::TPWordForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TPWordForm::btnCloseWordClick(TObject *Sender)
{
OleVariant SaveChanges = wdDoNotSaveChanges;
OleVariant OriginalFormat = Unassigned;
OleVariant RouteDocument = Unassigned;
try
{
WordApplication->Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication->Disconnect();
btnCloseWord->Enabled = FALSE;
btnPrint->Enabled = FALSE;
btnPreview->Enabled = FALSE;
}
catch (Exception &exception)
{
Application->ShowException(&exception);
WordApplication->Disconnect();
}
}
//---------------------------------------------------------------------------
void __fastcall TPWordForm::btnPrintClick(TObject *Sender)
{
WordDocument->PrintOut();
}
//---------------------------------------------------------------------------

void __fastcall TPWordForm::btnPreviewClick(TObject *Sender)
{
WordDocument->PrintPreview();
}

//---------------------------------------------------------------------------
void TPWordForm::SetFont()
{
/* Note: for most properties, only getters are exposed at the wrapper
class level. However, both getters and setters are usually exposed
on the interface itself. Thus, you can either call the setter function
directly: WordDocument->set_Prop ... or you can go through the interface:
WordDocument->GetDefaultInterface()->Prop. The two syntaxes are equivalent. */
WordFont->ConnectTo(WordDocument->Sentences->get_Last()->get_Font());
if (ChkBoxUnderline->Checked) WordFont->set_Underline(wdUnderlineWords);
if (ChkBoxBold->Checked) WordFont->set_Bold(1);
if (ChkBoxItalic->Checked) WordFont->GetDefaultInterface()->Italic = 1;
if (ChkBoxEmboss->Checked) WordFont->GetDefaultInterface()->Emboss = 1;
if (ChkBoxEngrave->Checked) WordFont->GetDefaultInterface()->Engrave = 1;
if (ChkBoxShadow->Checked) WordFont->GetDefaultInterface()->Shadow = 1;
if (ChkBoxDoublestrike->Checked) WordFont->GetDefaultInterface()->DoubleStrikeThrough = 1;
if (ChkBoxStrike->Checked) WordFont->GetDefaultInterface()->StrikeThrough = 1;
WordFont->set_Size(StrToInt(Size->Text));
if (Fonttype->ItemIndex >= 0) WordFont->set_Name(StringToOleStr(Fonttype->Items->Strings[Fonttype->ItemIndex]));
}


void __fastcall TPWordForm::BtnInsertRecordClick(TObject *Sender)
{
OleVariant Template = EmptyParam;
OleVariant NewTemplate = False;
OleVariant ItemIndex = 1;

try
{
try
{
WordApplication->Connect();
}
catch (Exception &exception)
{
MessageDlg("Word may not be installed", mtError, TMsgDlgButtons() << mbYes, 0);
Abort;
}
WordApplication->GetDefaultInterface()->Visible = True;
WordApplication->set_Caption(StringToOleStr("Borland automation"));
//Create new document
if (ChkBoxNewDoc->Checked)
{
WordApplication->Documents->Add(Template, NewTemplate);
//Assign WordDocument component
WordDocument->ConnectTo(WordApplication->Documents->Item(ItemIndex));
}
//Turn Spell checking off because it takes a long time if enabled and slows down Winword
WordApplication->Options->CheckSpellingAsYouType = False;
WordApplication->Options->CheckGrammarAsYouType = False;
//Insert data
DBImgFishImg->CopyToClipboard();
WordDocument->Sentences->Last->Paste();
WordDocument->Range(EmptyParam, EmptyParam)->InsertAfter(StringToOleStr("Common Name: " + Table->Fields->Fields[2]->AsString + '\n'));
SetFont();
WordDocument->Range(EmptyParam, EmptyParam)->InsertAfter(StringToOleStr("Species Name:" + Table->Fields->Fields[3]->AsString + '\n'));
WordDocument->Range(EmptyParam, EmptyParam)->InsertAfter(StringToOleStr("Length: " + Table->Fields->Fields[4]->AsString + '\n'));
WordDocument->Range(EmptyParam, EmptyParam)->InsertAfter(StringToOleStr("===\n"));
btnCloseWord->Enabled = True;
btnPrint->Enabled = True;
btnPreview->Enabled = True;
}
catch (Exception &exception)
{
Application->ShowException(&exception);
WordApplication->Disconnect();
}

}
//---------------------------------------------------------------------------


void __fastcall TPWordForm::FormCreate(TObject *Sender)
{
Fonttype->Items = Screen->Fonts;
}
//---------------------------------------------------------------------------

void __fastcall TPWordForm::FormClose(TObject *Sender, TCloseAction &Action)
{
Table->Close();
}
//---------------------------------------------------------------------------

void __fastcall TPWordForm::FormActivate(TObject *Sender)
{
Table->Open();
}
//---------------------------------------------------------------------------

void __fastcall TPWordForm::WordApplicationDocumentChange(TObject *Sender)
{
lbDocs->Items->Add(WordDocument->Name);
}
//---------------------------------------------------------------------------
fall1979 2004-10-29
  • 打赏
  • 举报
回复
比较着急,在线等!

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧