网上文摘 小说 Flash游戏 最近更新 下载排行 资源分类 下载指南
经典编程资源 精彩不容错过
设为首页
加入收藏
联系我们
当前位置:Delphi园地技巧文章编程心得 → 不使用剪帖板从Word获取数据
不使用剪帖板从Word获取数据
日期:2005年1月13日 作者: 人气: 查看:[大字体 中字体 小字体]
Are you looking for a way to get the entire text of a word document into a RichEdit without using the Clipboard?
Here you go:

Answer:


The Word Document object supports the IDataObject Interface. To get data from Word (RTF, text, structured storage etc) the IDataObject must be used. To get a pointer to the IDataObject Interface use QueryInterface. Word documents support the standard formats CF_TEXT and CF_METAFILEPICT as well as a number of other specific formats including RTF and structured storage. For the standard formats the constant values can be used for the value of cfFormat, but for the other formats the Document must be queried using the function EnumFormatEtc. This function will return a list of supported formats. The required format from this list is then passed to the GetData function of the IDataObject interface. It is important to note that the value of cfFormat for the proprietary formats (RTF etc.) is not constant between machines so it must always be found using EnumFormatEtc and not hard coded. For more information on IDataObject and its methods refer to the Win32 programming help files.

uses
Word_TLB, ActiveX, ComObj;

function GetRTFFormat(DataObject: IDataObject; var RTFFormat: TFormatEtc): Boolean;
var
Formats: IEnumFORMATETC;
TempFormat: TFormatEtc;
pFormatName: PChar;
Found: Boolean;
begin
try
OleCheck(DataObject.EnumFormatEtc(DATADIR_GET, Formats));
Found := False;
while (not Found) and (Formats.Next(1, TempFormat, nil) = S_OK) do
begin
pFormatName := AllocMem(255);
GetClipBoardFormatName(TempFormat.cfFormat, pFormatName, 254);
if (string(pFormatName) = 'Rich Text Format') then
begin
RTFFormat := TempFormat;
Found := True;
end;
FreeMem(pFormatName);
end;
Result := Found;
except
Result := False;
end;
end;

function GetRTF: string;
var
DataObject: IDataObject;
RTFFormat: TFormatEtc;
ReturnData: TStgMedium;
Buffer: PChar;
WordDoc: _Document;
WordApp: _Application;
begin
Result := '';
try
GetActiveOleObject('Word.Application').QueryInterface(_Application, WordApp);
except
ShowMessage('Error: MSWord is not running');
Exit;
end;
if (WordApp <> nil) then
try
WordDoc := WordApp.ActiveDocument;
WordDoc.QueryInterface(IDataObject, DataObject);
if GetRTFFormat(DataObject, RTFFormat) then
begin
OleCheck(DataObject.GetData(RTFFormat, ReturnData));
//RTF is passed through global memory
Buffer := GlobalLock(ReturnData.hglobal);
//Buffer is a pointer to the RTF text
Result := StrPas(Buffer);
GlobalUnlock(ReturnData.hglobal);
ReleaseStgMedium(ReturnData);
end;
except
// Error occured...
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ss: TStringstream;
rtfText: string;
begin
rtfText := GetRTF;
ss := TStringStream.Create(rtfText);
try
ss.Position := 0;
Memo1.Text := rtfText; // Show rtf in Memo1
RichEdit1.Lines.LoadFromStream(ss); // load rtf into richedit1
finally
ss.Free
end;
end;

(出处:delphi3000.com)

相关文章:
·使用Delphi开发Office Word插件
·Delphi与Word之间的融合技术(1)
·Delphi与Word之间的融合技术(2)
·Delphi与Word之间的融合技术(3)
·解决word打开文档提示错误“恢复我的工作并重启microsoft office word”
 → 特别推荐
 → 热点TOP10
关于我们 | 广告服务 | 发布资源 | 联系站长 Copyright © 2002-2006 Delphi园地 All Rights Reserved