使用CreateOleObject方法对WORD文档操作具有先天所具备的优势,与delphi所提供的那些控件方式的访问相比,CreateOleObject方法距离WORD核心的操作“更近”,因为它直接使用OFFICE所提供的VBA语言对WORD文档的操作进行编程。
以下是我在本机上所做的实验,机器软件配置如下:
Windows XP+delphi7.0+OFFICE 2003
这个程序很简单,在页面上放置了一个edit和一个button,每单击一次按钮,就会自动把edit中的内容添加在后台中的word文档中,程序关闭时文件自动保存在当前程序的主目录中。
unit main;
interface
//如果要使用CreateOleObject的办法对WORD文档进行操作,应该在uses
//语句中加入Comobj声明和WordXP声明
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Comobj, WordXP, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
// procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//把这两个变量声明为全局变量
FWord: Variant;
FDoc: Variant;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
FWord.Selection.TypeParagraph;
FWord.Selection.TypeText(Text := form1.Edit1.Text);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//首先创建对象,如果出现异常就作出提示
try
FWord := CreateOleObject('Word.Application');
//WORD程序的执行是否可见,值为False时程序在后台执行
FWord.Visible := False;
except
ShowMessage('创建word对象失败!');
Exit;
end;
//先在打开的Word中创建一个新的页面,然后在其中键入"Hello,"+回车+"World!"
try
FDoc := FWord.Documents.Add;
FWord.Selection.TypeText(Text := 'Hello,');
FWord.Selection.TypeParagraph;
FWord.Selection.TypeText(Text := 'World! ');
except
on e: Exception do
ShowMessage(e.Message);
end;
end;
//在程序关闭时把文件内容保存到当前目录中,并以test.doc命名
//同时关闭WORD程序
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FDoc.SaveAs(ExtractFilePath(application.ExeName) +'test.doc');
FWord.Quit;
FWord := Unassigned;
end;
end.
此外,对OFFICE其他文件的操作都比较类似,不在赘述。通过对WORD文件中更复杂的VBA宏的引用,这个方法还可以完成更复杂的文档操作。
MAPX中修改图元的属性值
|
作者/收集者:lfeng2010
开发环境:Delphi + MapX
代码:
procedure TForm1.Map1ToolUsed(ASender: TObject; ToolNum: Smallint; X1, Y1, X2, Y2, Distance: Double; Shift, Ctrl: WordBool; var EnableDefault: WordBool); Var i : Integer; fID : String; searchType : Integer; dist : OleVariant; pt : Variant; curFeatures : Variant; rc : Variant; obj : Variant; featureSearchLayer : Variant;
ftrNew: CmapXFeature; arrVals: Variant; //leee 030621 idx,NodeCnt:integer; NodeString:String; ds: CMapxDataset; rv: CMapxRowvalue; rvs: CMapxRowvalues; ftr :CMapxFeature; ftrLayer :CmapxLayer; begin pt := CreateOleObject('MapX.Point.5'); // Clear Search Results list
if ToolNum <> SEARCH_POINT_TOOL then begin // Set SearchType based on what key user may have down if (Shift And Not Ctrl) then searchType := miSearchTypePartiallyWithin else if (Ctrl And Not Shift) then searchType := miSearchTypeEntirelyWithin else searchType := miSearchTypeCentroidWithin; end; if(ToolNum=miSelectTool ) then begin ftrLayer:=Map1.Layers.Item[1]; if (ctrl) then
ftrLayer.Selection.SelectByPoint(X1, Y1, miSelectionAppend,emptyparam) else if (Shift) then ftrLayer.Selection.SelectByPoint(X1, Y1, miSelectionRemove,emptyparam) else ftrLayer.Selection.SelectByPoint(X1, Y1, miSelectionNew,emptyparam);
if (ftrLayer.selection.count >= 1) //0306 lee 设计 then begin ds := Map1.Datasets.Add(miDataSetLayer, ftrLayer,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam);
obj:=ftrLayer.Selection.Item[ftrLayer.selection.count]; //此处示例取得最后一个Feature ,也可用循环语句取得所有Feature
// ftrLayer.BeginAccess( miAccessReadWrite); rvs:=ds.Rowvalues[obj];
rvs._Item(1).value:=aaaaa; //为第一个字段赋值(本例图元有三个属性) rvs._Item(2).value:=bbbbb;//为第二个字段赋值
rvs._Item(3).value:=ccccc;//为第三个字段赋值
obj.Update(true,rvs); // ftrLayer.EndAccess( miAccessEnd);
end;
end;
end;
再在一个BUTTON的CLUCK事件中加入以下代码: map1.CurrentTool:=miSelectTool;
当点击这个BUTTON后,再点选图元,被选中图元的属性就可以被修改
Delphi 操作 PDF -- 使用 Acrobat SDK 初探
小弟前陣子尋找用Delphi列印PDF solution, 發現Acrobat SDK 提供蠻多API可用的.. 需要完整的API 可到下面網址尋找: Adobe Solutions Network http://partners.adobe.com/asn/tech/pdf/acrobatsdks.jsp
以下列出簡單範例 由於小弟只試了一下, 若有謬誤 請各位大大指教
AcroApp,AVDoc,PDDoc: variant; PageNum:integer; begin try AcroApp := CreateOleObject('AcroExch.App'); // 建立Acrobat View application AVDoc := CreateOleObject('AcroExch.AVDoc'); // 建立Acrobat View 視覺化物件 PDDoc := CreateOleObject('AcroExch.PDDoc'); // 建立Acrobat View 非視覺化(控制)物件
AVDoc.Open('D:\upload\test.pdf', 'PDF title'); // Open document // 參數1, full path // 參數2, windows title AVDoc := AcroApp.GetActiveDoc; // Get frontmost document PDDoc := AVDoc.GetPDDoc; // Get associated PDDoc
AcroApp.Show; //AcroApp.Hide; // 決定是否顯示 Acrobat View PageNum := PDDoc.GetNumPages; // Get PDF total pages
AVDoc.PrintPagesSilent(0, PageNum, 0, 0, 0); // 列印特定的Page // 參數1, 起始頁數, 0 為第一頁 // 參數2, 結束頁數
showmessage('ok'); finally PDDoc.Close; //AVDoc.Close; //If PDDoc & AVDoc are constructed the same file, //Close PDDoc will destroy both. AcroApp.exit; end;
|