控制WORD问题(200分)

  • 主题发起人 主题发起人 gztrzj
  • 开始时间 开始时间
G

gztrzj

Unregistered / Unconfirmed
GUEST, unregistred user!
1、DELPHI如何运行带参数的WORD宏?
2、在DELPHI程序中如何得到WORD文档中控件的名称?
 
有点难,兄弟D
 
问题1、2我已解决,谢谢。
 
gztrzj,可以將你的心得講出來給大家參考參考嗎?
 
比如 WordApp.Run('宏命令'); //wordapp是servers页控件WordApplication1
 
unit Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics,Variants, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Registry, Buttons;

type
TMainForm = class(TForm)
lbl_filename: TLabeledEdit;
lbl_password: TLabeledEdit;
btnok: TButton;
btncancle: TButton;
chkbx_password: TCheckBox;
lbl_svrname: TLabeledEdit;
lbl_user: TLabeledEdit;
procedure FormDestroy(Sender: TObject);
procedure btnokClick(Sender: TObject);
procedure chkbx_passwordClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btncancleClick(Sender: TObject);
private
{ Private declarations }
FTick: UINT;
Fword: Variant;
function Openword: Boolean;

public
{ Public declarations }
end;

var
MainForm: TMainForm;



implementation

{$R *.DFM}
uses
ComObj;

function GetRegString(RootKey: HKEY; Section, Key, DefaultValue: string): string;
var
MyReg: TRegistry;
begin
Result := DefaultValue;
MyReg := TRegistry.Create;
if MyReg <> nil then
begin
MyReg.RootKey := RootKey;
if MyReg.OpenKeyReadOnly(Section) then
begin
Result := MyReg.ReadString(Key);
if Result = '' then Result := DefaultValue;
MyReg.CloseKey;
end;
MyReg.Free;
end;
end;

function TMainForm.Openword: Boolean;
var
Curword: string;
begin
Curword := GetRegString(HKEY_CLASSES_ROOT, '/word.Application/CurVer', '', '');
if Curword = '' then
begin
Application.MessageBox('请先安装 word 97 或 word 2000!',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;

if (Curword <> 'word.Application.8') and (Curword <> 'word.Application.9') then
begin
Curword := GetRegString(HKEY_CLASSES_ROOT, '/word.Application.8', '', '');
if Curword = '' then
begin
Curword := GetRegString(HKEY_CLASSES_ROOT, '/word.Application.9', '', '');
if Curword <> '' then Curword := 'word.Application.9';
end
else
begin
Curword := 'word.Application.8';
end;

if Curword = '' then
begin
Application.MessageBox('无法运行 word 97 或 word 2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
end;

try
Fword := CreateOleObject(Curword);
if VarIsEmpty(Fword) then
begin
Application.MessageBox('无法运行 word 97 或 word 2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
except
Application.MessageBox('无法运行 word 97 或 word 2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;

try
begin
Fword.DisplayAlerts := False;
copyfile(pchar(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.mdl'),pchar(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.doc'),true);
SetCurrentDir(ExtractFilePath(Application.Exename));
Fword.documents.Open(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.doc');
Fword.Visible := True;
Fword.Run('test',lbl_svrname.text,lbl_user.Text,lbl_filename.Text,lbl_password.Text);
//宏名称,后面都是宏的参数,有多少就跟多少
Result := True;
end;
except
Application.MessageBox('分析失败,',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
end;
end;



procedure TMainForm.FormDestroy(Sender: TObject);
begin
if not VarIsEmpty(Fword) then Fword.Quit;
end;

procedure TMainForm.btnokClick(Sender: TObject);
begin
if length(lbl_svrname.text)=0 then lbl_svrname.Text:='(local)';

if length(lbl_filename.Text)=0 then
begin
application.MessageBox(pchar('请输入文件名'),pchar(self.caption),MB_ICONWARNING);
exit;
end;

hide;
if not Openword then Close;

end;

procedure TMainForm.chkbx_passwordClick(Sender: TObject);
begin
lbl_password.Enabled:=chkbx_password.Checked;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
lbl_password.Enabled:=false;
chkbx_password.Checked:=false;
end;

procedure TMainForm.btncancleClick(Sender: TObject);
begin
close;
end;

end.
 
TO : 楼上的,我只能作到在本地的数据表连接WORD,怎么把ADO中的数据连接到WORD中?
我想使用WORD 的邮件合并功能,现也正在使用宏操作,而且宏只能保存在WORD的通用模板
中。。。。。。
您下面中的2句话我不是很懂,能不能帮解说一下,谢谢!我用的是BCB,对DELPHI不是很懂。:(
//----------------------------------------------------------
MyReg.RootKey := RootKey;
if MyReg.OpenKeyReadOnly(Section) then
begin
Result := MyReg.ReadString(Key);
if Result = '' then Result := DefaultValue;
MyReg.CloseKey;
end;
//------------------------------------------------
copyfile(pchar(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.mdl'),pchar(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.doc'),true);
SetCurrentDir(ExtractFilePath(Application.Exename));
Fword.documents.Open(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.doc');
//----------------------------------------------------------------------
 
后退
顶部