1:定義流的header , OleContainer要求流中要有Header type //流Header的結構 TStreamHeader = record Signature: Integer; //$434F4442 DrawAspect: Integer; //1 DataSize: Integer; //stream.size; end; 注:OleContainer要求的流=header + 內存流 2:從數據庫中取出字段到內存流: TBlobField(FADOQuery3.FieldByName('FContent')).SaveToStream(AMemoryStream); 保存: TBlobField(FADOQuery3.FieldByName('FContent')).LoadFromStream(AMemoryStream); 3:從流中讀出並顯示: procedure TFrameWord.DispFileContent(AMStream: TMemoryStream); var ft: string; ms: TMemoryStream; oMemoryStream : TMemoryStream; Header : TStreamHeader; begin ms := AMStream; if (ms = nil) or (ms.Size = 0) then Exit; ms.Position := 0; oMemoryStream := TMemoryStream.Create; try with Header do begin Signature := $434F4442; DrawAspect := 1; DataSize := ms.Size; end; ms.Position := 0; oMemoryStream.WriteBuffer(Header,SizeOf(Header)); oMemoryStream.CopyFrom(ms, 0); oMemoryStream.Position := 0; OleContainer.LoadFromStream(oMemoryStream); OleContainer.DoVerb(ovPrimary); finally oMemoryStream.Free; end; end; 4: 從文件中讀出: OleContainer.CreateObjectFromFile('D:\work\BidBuild_Client\doc\實用ASP組件介紹.DOC', False); OleContainer.DoVerb(ovPrimary); 5:設置只讀,並隱藏工具菜單和屏蔽右鍵菜單中的編輯功能: Word文檔: OleContainer.OleObject.Application.ActiveDocument.Protect(2); OleContainer.OleObject.application.CommandBars['Standard'].Visible:=false; OleContainer.OleObject.application.CommandBars['Formatting'].Visible:=false; OleContainer.OleObject.application.CommandBars['Reviewing'].Visible:=false; 或: OleContainer.OleObject.Application.ActiveDocument.Protect(2); OleContainer.OleObject.Application.ActiveWindow.ActivePane.DisplayRulers := False; Excel文檔: OleContainer.OleObject.Application.ActiveSheet.Protect; OleContainer.OleObject.application.CommandBars['Standard'].Visible:=false; OleContainer.OleObject.application.CommandBars['Formatting'].Visible:=false; OleContainer.OleObject.application.CommandBars['Reviewing'].Visible:=false; 注:以上代碼放到OleContainer.DoVerb(ovPrimary)之后 6:關閉 if OleContainer.State <> osEmpty then OleContainer.Close; 7:OLEContainer 控件的主要屬性和方法 1) AllowInPlace property AllowInPlace:Boolean; 這個屬性用於決定啟動OLE對象服務程序的方式,如果為假,那么運行其間激活OLE對象時,打開整個OLE服務程序,即單獨 開一個窗口,而為真時, 則把服務的菜單合並到應用程序中. 2) AutoActive type TAutoActivate=(aaManual,aaGetFocus,aaDoubleClick); property AutoActivate: TAutoActivate; 找開方式. 其中aaManual時,要激活OLE對象,必須在程序中調用方法DoVerb(OnShow) 3) CanPaste property CanPaste:Boolean; 只讀屬性,如果剪貼板中的內容適合粘貼到一個OLE對象,則為true, 否則為false 4) CopyOnsave property CopyOnsave:boolean; 為真則表示把OLE對象臨時寫到一個文件中,反之表示全部留在內在中. 5) Iconic property Iconic:Boolean; 是否以圖標方式顯示以節省屏幕上的空間 6) Linked property Linked:Boolean; 只讀屬性,返回真表示OLE對象是連接到文檔中,返回假表示OLE對象是嵌入到文檔中. 7) Modify property modified:Boolean; 當OLE對象發生了變化時(包括這個對象被刪除或被其他OLE對象所替代),這個屬性被設置為真 8) NewInserted property NewInserted:Boolean; 只讀,如果剛剛調用的InsertObjectDialog函數插入了一個OLE對象,返回真.這時可調用Doverb(OvShow)激活這個OLE對 象. 9) OleClassName property OleClassName:string; 只讀. 返回OLE對象的類名, 當程序中有多個OLE對象時,可以用這個屬性作為它們各自的標簽. 10)OleObject property oleobject:Variant; 只讀, 返回OLE容器中的OLE對象, 這個屬性很重要,通過這個屬性可以訪問OLE服務程序. 11) OleObjectInterface property OleObjectInterface:IOleObject; 只讀. 返回OLE對象的OleObject接口,在直接調用OLE的API需要用到這個接口. 12) OldStreamFormat property OldStreamFormat:Boolean; 如果為真, OLE對象就以OLE1的格式存儲, 為假就以OLE2的格式存儲. 13) PrimaryVerb property PrimaryVerb: integer; 只讀. 返回OLE對象可進行的操作中主操作的索引號(序號) 14) SizeMode type TSizeMode = (smClip, smCenter, smScale, smStretch, smAutoSize); property SizeMode:TSizeMode; smClip, 超過容器的部分將被裁減. smCenter, 中間. smScale, 自動適應容器的大小 smStretch, 自動撐滿 smAutoSize, 容器自動調整, 以適應OLE的大小. 15) State type TObjectState=(osEmpty,osLoaded,osRunning,osOpen,osInPlaceActive,osUIActive); property State: TObjectState; 只讀, 返回OLE對象的狀態,可以是以下值. osEmpty,容器中沒有OLE對象. osLoaded,容器中有OLE對象.但OLE服務程序沒有運行. osRunning,服務器正在運行. osOpen,對象正在運行,OLE服務程序單獨運行. osInPlaceActive,對象正在運行,OLE服務程序菜單將要被合並到客戶程序中運行. osUIActive,對象正在運行,OLE服務程序菜單已經被合並到客戶程序中運行. OLEContainer 控件的主要方法 1) ChangeIconDialog function ChangeIconDialog:Boolean; 調用這個函數將打開一個更改圖標的對話框. 當OLE對象以圖標顯示時,就以用戶選擇的圖標顯示. 2) Close 過程 procedure Close; 關閉. 如果OLE對象已修改,調用Close將首先保存OLE對象. 3) Copy 過程 procedure copy; 把OLE對象復制到剪貼板中 4) CteateLinkToFile方法 procedure CreateLinkToFile(FileName:string; Iconic:Boolean); 創建一個OLE對象,其內容從指定的文件中讀取,創建的OLE對象鏈接到OLE容器中. 如果OLE容器中已經有一個OLE對象, 這個已有的OLE對象將被刪除,未保存的修改也被作廢. Iconic參數設為True表示OLE對象以圖標顯示. 5) CreateObject 過程. procedure CreateObject(const OleClassName:string;Iconic:Boolean); 這個過程用於創建一個OLE對象,OleClassName參數指定對象的識別名,創建后的OLE對象嵌入到OLE容器中,如果OLE容器中已經有一個OLE對象, 這個已有的OLE對象將被刪除,未保存的修改也被作廢. Iconic參數設為True表示OLE對象以圖標顯示. 6) CreateObjectFromFile 過程 procedure CreateObjectFromFile(const FileName:string; Iconic:Boolean); 創建一個OLE對象,其內容從指定的文件中讀取,創建的OLE對象嵌入到OLE容器中. 如果OLE容器中已經有一個OLE對象, 這個已有的OLE對象將被刪除,未保存的修改也被作廢. Iconic參數設為True表示OLE對象以圖標顯示. 7) CreateObjectFromInfo 方法, procedure CreateObjectFromInfo(const CreateInfo:TCreateInfo); 創建一個OLE對象,其內容從CreateInfo參數年指定的記錄中讀取(這個參數是個記錄類型,包含了創建OLE對象所需要的信息) 8) DoVerb 方法. procedure DoVerb(Verb:Integer); 用於對OLE對象進行操作,verb參數指定操作類型. 9) ObjectPropertiesDialog函數 function objectPropertiesDialog:Boolean; 用來打開Windows OLE對象屬性對話框,用於修改OLE對象的屬性. 10) Run 方法.procedure Run; 用於運行OLE服務程序,但並不激活OLE對象本身,當服務程序處於運行狀態后激活OLE對象將非常快. 11) PasteSpecialDialog方法 function PasteSpecialDialog:Boolean; 該方法打開Windows的選擇性粘貼對話框. 8:OLE方式Word中插入圖片 我是在delphi中用olecontainer操作word,要向其中插入圖片,使用粘貼板。但是圖片放入后位置是這一行的開始位置,如下: { 將一副圖片粘貼到word中 } ClipBoard.Assign(aImage.Picture); { 以下為vba代碼--可在word中利用錄制宏的功能取得--------------------bagin } { 使用vba之前加 x.OleObject.application 例如:excel添加工作表 } { oc.OleObject.application.sheets.Add; } intInlineShapesIndex:=ocDoc.OleObject.application.ThisDocument.InlineShapes.Count+1; intShapesIndex:=ocDoc.OleObject.application.ThisDocument.Shapes.Count+1; //從剪貼板添加一個圖片 ocDoc.OleObject.application.Selection.Paste; //轉換為圖形 ocDoc.OleObject.application.ThisDocument.InlineShapes(intInlineShapesIndex).ConvertToShape; //設置文字環繞方式 ocDoc.OleObject.application.ThisDocument.Shapes(intShapesIndex).WrapFormat.Type := 'wdWrapThrough'; //將圖片中的白色設為透明色 ocDoc.OleObject.application.Selection.InlineShapes(intShapesIndex).PictureFormat.TransparentBackground := 'msoTrue'; ocDoc.OleObject.application.Selection.InlineShapes(intShapesIndex).PictureFormat.TransparencyColor:='RGB(255,255,255)'; ocDoc.OleObject.application.Selection.InlineShapes(intShapesIndex).Fill.Visible := 'msoFalse'; { 以上為vba代碼--可在word中利用錄制宏的功能取得----------------------end }