我用OleContainer控制Word,在关闭时,程序经常会出现询问是否保存Normal.dot的提示,请问这是怎么回事,怎么能够去掉?
解决方案 »
- 如何使用Delphi判断.NET的版本是否是3.0以上
- 救命!使用tcpclient向tcpserver传送内容,判断内容来控制mediaplay播放或暂停
- 如何字符串中取出字符?
- 请教高手,delphi mdi应用里 ShowModal(模式窗体)能否在任务栏上不显示单独图标?
- ehlib打印时如何显示图片
- 请问 怎样把字符串转换成变量
- 好久没有登录csdn了,看到了这个问题,发个帖子望大家多多参与,顺便散分
- 多表SQL语言查询问题,Up有分
- ***这是为什么?***
- 我是一个pb程序员,想学习delphi,然后再学习c++builder,请推荐基本学习delphi的好书,谢谢诶!
- 求教: 将DBImage1上的图象拷贝到image1中,用copyrect(rect2,dbimage1.picture.bitmap.canvas,rect1)不出错但没效果,怎样实现呢?只能通过
- dbgrid 下拉菜单疑问。
// For that reason, you will have to import the MSWord8.OLB, residing in
// the Microsoft Office directory, prior to compiling this demo otherwise
// you will get the error "File not found: Word_TLB.dcu"uses
Windows, Classes, ActiveX, Word_TLB;type
TWordEventSink = class(TInterfacedObject, IUnknown, IDispatch)
private
FOwner : TObject;
FAppDispatch: IDispatch;
// FDocDispatch: IDispatch;
FAppDispIntfIID: TGUID;
FDocDispIntfIID: TGUID;
FAppConnection: Integer;
// FDocConnection: Integer;
FOnQuit : TNotifyEvent;
FOnDocumentChange : TNotifyEvent;
FOnNewDocument : TNotifyEvent;
FOnOpenDocument : TNotifyEvent;
FOnCloseDocument : TNotifyEvent;
protected
{ IUnknown }
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
{ IDispatch }
function GetTypeInfoCount(out Count: Integer): HRESULT; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HRESULT; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HRESULT; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HRESULT; stdcall;
public
constructor Create(AnOwner: TObject; AnAppDispatch: IDispatch; const AnAppDispIntfIID, ADocDispIntfIID: TGUID);
destructor Destroy; override;
property OnQuit : TNotifyEvent read FOnQuit write FOnQuit;
property OnDocumentChange : TNotifyEvent read FOnDocumentChange write FOnDocumentChange;
property OnNewDocument : TNotifyEvent read FOnNewDocument write FOnNewDocument;
property OnOpenDocument : TNotifyEvent read FOnOpenDocument write FOnOpenDocument;
property OnCloseDocument : TNotifyEvent read FOnCloseDocument write FOnCloseDocument;
end; TWordObject = class
private
FWordApp : _Application;
FEventSink : TWordEventSink;
function GetCaption : String;
procedure SetCaption(Value : String);
function GetVisible : Boolean;
procedure SetVisible(Value : Boolean);
function GetOnQuit : TNotifyEvent;
procedure SetOnQuit(Value : TNotifyEvent);
function GetOnDocumentChange : TNotifyEvent;
procedure SetOnDocumentChange(Value : TNotifyEvent);
function GetOnNewDocument: TNotifyEvent;
procedure SetOnNewDocument(Value : TNotifyEvent);
function GetOnOpenDocument: TNotifyEvent;
procedure SetOnOpenDocument(Value : TNotifyEvent);
function GetOnCloseDocument: TNotifyEvent;
procedure SetOnCloseDocument(Value : TNotifyEvent);
public
constructor Create;
destructor Destroy; override;
procedure NewDoc(Template : String);
procedure CloseDoc;
procedure InsertText(Text : String);
procedure Print;
procedure SaveAs(Filename : String);
procedure FindText(AText, RText :String);
procedure GetCellsHW(AText:String;var BHeight,BWidth,BFontSize:Single;BFontName:String);
procedure AddNewPage;
published
property Application : _Application read FWordApp;
property Caption : String read GetCaption write SetCaption;
property Visible : Boolean read GetVisible write SetVisible;
property OnQuit : TNotifyEvent read GetOnQuit write SetOnQuit;
property OnDocumentChange : TNotifyEvent read GetOnDocumentChange write SetOnDocumentChange;
property OnNewDocument : TNotifyEvent read GetOnNewDocument write SetOnNewDocument;
property OnOpenDocument : TNotifyEvent read GetOnOpenDocument write SetOnOpenDocument;
property OnCloseDocument : TNotifyEvent read GetOnCloseDocument write SetOnCloseDocument;
end;implementationuses
ComObj;
{ TWordEventSink implementation }constructor TWordEventSink.Create(AnOwner : TObject; AnAppDispatch: IDispatch; const AnAppDispIntfIID, ADocDispIntfIID: TGUID);
begin
inherited Create; FOwner := AnOwner;
FAppDispIntfIID := AnAppDispIntfIID;
FDocDispIntfIID := ADocDispIntfIID;
FAppDispatch := AnAppDispatch; // Hook the sink up to the automation server (Word97)
InterfaceConnect(FAppDispatch,FAppDispIntfIID,Self,FAppConnection);
end;destructor TWordEventSink.Destroy;
begin
// Unhook the sink from the automation server (Word97)
InterfaceDisconnect(FAppDispatch,FAppDispIntfIID,FAppConnection); inherited Destroy;
end;function TWordEventSink.QueryInterface(const IID: TGUID; out Obj): HRESULT;
begin
// We need to return the two event interfaces when they're asked for
Result := E_NOINTERFACE;
if GetInterface(IID,Obj) then
Result := S_OK;
if IsEqualGUID(IID,FAppDispIntfIID) and GetInterface(IDispatch,Obj) then
Result := S_OK;
if IsEqualGUID(IID,FDocDispIntfIID) and GetInterface(IDispatch,Obj) then
Result := S_OK;
end;function TWordEventSink._AddRef: Integer;
begin
// Skeleton implementation
Result := 2;
end;function TWordEventSink._Release: Integer;
begin
// Skeleton implementation
Result := 1;
end;function TWordEventSink.GetTypeInfoCount(out Count: Integer): HRESULT;
begin
// Skeleton implementation
Count := 0;
Result := S_OK;
end;function TWordEventSink.GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HRESULT;
begin
// Skeleton implementation
Result := E_NOTIMPL;
end;function TWordEventSink.GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HRESULT;
begin
// Skeleton implementation
Result := E_NOTIMPL;
end;function TWordEventSink.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HRESULT;
begin
// Fire the different event handlers when
// the different event methods are invoked
case DispID of
2 : if Assigned(FOnQuit) then
FOnQuit(FOwner);
3 : begin
{ if Assigned(FOnDocumentChange) then
FOnDocumentChange(FOwner);
// When we see a document change, we also need to disconnect the
// sink from the old document, and hook it up to the new document
InterfaceDisconnect(FDocDispatch,FDocDispIntfIID,FDocConnection);
try
FDocDispatch := _Application(FAppDispatch).ActiveDocument;
InterfaceConnect(FDocDispatch,FDocDispIntfIID,Self,FDocConnection);
except;
end;}
end;
4 : if Assigned(FOnNewDocument) then
FOnNewDocument(FOwner);
5 : if Assigned(FOnOpenDocument) then
FOnOpenDocument(FOwner);
6 : if Assigned(FOnCloseDocument) then
FOnCloseDocument(FOwner);
end; Result := S_OK;
end;{ TWordObject implementation }constructor TWordObject.Create;
begin
// Fire off Word97 and create the event sink
FWordApp := CoApplication_.Create;
FEventSink := TWordEventSink.Create(Self,FWordApp,ApplicationEvents,DocumentEvents);
end;destructor TWordObject.Destroy;
var
SaveChanges,
OriginalFormat,
RouteDocument : OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
FWordApp.Quit(SaveChanges,OriginalFormat,RouteDocument);
except
end;
FEventSink := nil;
inherited Destroy;
end;function TWordObject.GetVisible : Boolean;
begin
Result := FWordApp.Visible;
end;procedure TWordObject.SetCaption(Value : String);
begin
FWordApp.Caption := Value;
end;function TWordObject.GetCaption : String;
begin
Result := FWordApp.Caption;
end;procedure TWordObject.SetVisible(Value : Boolean);
begin
FWordApp.Visible := Value;
end;function TWordObject.GetOnQuit : TNotifyEvent;
begin
Result := FEventSink.OnQuit;
end;procedure TWordObject.SetOnQuit(Value : TNotifyEvent);
begin
FEventSink.OnQuit := Value;
end;function TWordObject.GetOnDocumentChange : TNotifyEvent;
begin
Result := FEventSink.OnDocumentChange;
end;procedure TWordObject.SetOnDocumentChange(Value : TNotifyEvent);
begin
FEventSink.OnDocumentChange := Value;
end;function TWordObject.GetOnNewDocument : TNotifyEvent;
begin
Result := FEventSink.OnNewDocument;
end;procedure TWordObject.SetOnNewDocument(Value : TNotifyEvent);
begin
FEventSink.OnNewDocument := Value;
end;function TWordObject.GetOnOpenDocument : TNotifyEvent;
begin
Result := FEventSink.OnOpenDocument;
end;procedure TWordObject.SetOnOpenDocument(Value : TNotifyEvent);
begin
FEventSink.OnOpenDocument := Value;
end;function TWordObject.GetOnCloseDocument : TNotifyEvent;
begin
Result := FEventSink.OnCloseDocument;
end;procedure TWordObject.SetOnCloseDocument(Value : TNotifyEvent);
begin
FEventSink.OnCloseDocument := Value;
end;procedure TWordObject.InsertText(Text : String);
begin
FWordApp.Selection.TypeText(Text);
end;procedure TWordObject.NewDoc(Template : String);
var
DocTemplate,
NewTemplate : OleVariant;
//If Word is 2000, add two lines:
DocumentType: OleVariant;
MyVisible: OleVariant;
begin
DocTemplate := Template;
NewTemplate := False;
//If Word is 97 :
{*FWordApp.Documents.Add(DocTemplate,NewTemplate);*}
//If Word is 2000:
FWordApp.Documents.Add(DocTemplate,NewTemplate,DocumentType,MyVisible);
end;procedure TWordObject.CloseDoc;
var
SaveChanges,
OriginalFormat,
RouteDocument : OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
FWordApp.ActiveDocument.Close(SaveChanges,OriginalFormat,RouteDocument);
end;procedure TWordObject.Print;
begin
OleVariant(FWordApp).PrintOut;
end;procedure TWordObject.SaveAs(Filename : String);
begin
OleVariant(FWordApp).ActiveDocument.SaveAs(FileName);
end;procedure TWordObject.FindText(AText, RText:String);
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms,
Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza,
MatchControl: OleVariant;
begin
FWordApp.Selection.SetRange(0, 0);
FindText:=AText;
MatchCase:=False;
MatchWholeWord:=False;
Forward:=True;
ReplaceWith:=RText;
Replace:=wdReplaceOne;
FWordApp.Selection.Find.Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl);
end;procedure TWordObject.GetCellsHW(AText: String;var BHeight,BWidth,BFontSize:Single;BFontName:String);
var
ATable:Table;
ACell:Cell;
ACells:Cells;
i, j:Integer;
Str:String;
begin
for j:=1 to FWordApp.ActiveDocument.Tables.Count do begin
ATable:=FWordApp.ActiveDocument.Tables.Item(j);
ACells:=ATable.Range.Get_Cells;
for i:=1 to ACells.Count do begin
ACell:=ACells.Item(i);
Str:=ACell.Range.Text;
if Pos(AText, Str)>0 then begin
BHeight:=ACell.Height;
BWidth:=ACell.Width;
BFontName:=ACell.Range.Font.Name;
BFontSize:=ACell.Range.Font.Size;
Exit;
end;
end;
end;
end;procedure TWordObject.AddNewPage;
var
BreakType:OleVariant;
begin
FindText('{Break Page}', ' ');
BreakType:=wdSectionBreakNextPage;
FWordApp.Selection.InsertBreak(BreakType);
end;end.