以下是我测试 宏 与delphi得转换时写的一段代码
功能是写入word及保存。delphi6+word2000下通过
在窗体上放一个wA1: TwordApplication;
Button4: TButton;
SaveDialog1: TSaveDialog;
即可
我是先在word中进行某项操作同时录下宏,然后在delphi中进行转换;
同理,读取excel也可以根据自己的需要来先录取宏,然后转化的方法。
用:TExcelApplication即可。
当然读取excel也可用ado 。查查大富翁以前的纪录吧,很多的。
我机器上忘了装excel所以没试
procedure TForm1.Button4Click(Sender: TObject);
var empDoc,NewTempDoc,TempWord,fileformat,TempEmpty,temp1,temp2,unit1,count,Extend:OleVariant;
pagefoot,TempDoc,numrows,DocumentType,DefaultTableBehavior,AutoFitBehavior:OleVariant;
i,j:integer;
wordsnap:wordbool;
InputString,dir,dir1:string;
year,month,day:word;
begin
TempEmpty:=EmptyParam;
TempDoc:= EmptyParam;
FileFormat:=wdFormatDocument;
do
cumentType:=wdNewBlankDocument;
DefaultTableBehavior:=wdWord9TableBehavior;
AutoFitBehavior:=wdAutoFitFixed;
NewTempDoc:= True;
temp1:=false;
unit1:=wdLine;
temp2:=true;
Extend:=wdExtend;
try
wa1.Connect;
except
Application.Messagebox('word2000不知有点啥问题,无法启动!','记账本',mb_Ok);
exit;
end;
WA1.Visible :=true;
count:=1;
InputString:= InputBox('签名框', '清把大名写在下面啦!', '');
savedialog1.Execute;
if SaveDialog1.FileName<>'noname' then
dir1:=SaveDialog1.FileName+'.doc'
else
dir1:='c:/ljh.doc';
for i:=length(dir1)-1do
wnto 0do
begin
if dir1='/' then
begin
dir:=copy(dir1,1,i);
delete(dir1,1,i);
break;
end;
end;
wa1.Documents.Add(TempEmpty,TempEmpty,DocumentType,temp2);
wa1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;//居中
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;//粗体互换
wa1.ActiveWindow.Selection.Font.Size:=16;
wa1.Selection.TypeText('收支纪录');//写入
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.ActiveWindow.Selection.Font.Size:=12;
wa1.Selection.TypeParagraph;//换行,相当于 回车
//建表20*4
wa1.ActiveDocument.Tables.Add(wa1.ActiveWindow.Selection.Range,20,4,DefaultTableBehavior,AutoFitBehavior);
//以下是两种插入表格数据的方法。
for j:=1 to 4do
//第一种先移动光标,然后写入
begin
wa1.Selection.Tables.Item(1).Cell(1,j).Select;
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeText('哈哈'+inttostr(j));
end;
for j:=2 to 20do
//第二种,直接确定那一个格,写入。快点
for i:=1 to 4do
wa1.Selection.Tables.Item(1).Cell(j,i).Range.Text:='ljh'+inttostr(j)+','+inttostr(i);
wa1.Selection.Tables.Item(1).Cell(j-1,1).Select;//光标选定到表格最后一行第一格
unit1:=wdline;
wa1.ActiveWindow.Selection.MoveDown(unit1,count,TempEmpty);//光标下移
wa1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphJustify;
wa1.Selection.TypeParagraph;
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeText('各项收支统计结果:');
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeParagraph;
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeText('收支总计:100000');
wa1.Selection.TypeParagraph;
wa1.Selection.TypeText(' 大写:人民币十万');
wa1.Selection.TypeParagraph;
//换行
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphRight;
wa1.ActiveWindow.Selection.Font.Size:=10;
wa1.Selection.TypeText(InputString);
wa1.Selection.TypeParagraph;
decodedate(now,year,month,day);
wa1.Selection.TypeText(format('%4d年%2d月%2d日',[year,month,day]));
//页眉页角
If (wa1.ActiveWindow.ActivePane.View.Type_ = wdNormalView) Or (wa1.ActiveWindow.ActivePane.View.Type_ = wdOutlineView) then
wa1.ActiveWindow.ActivePane.View.Type_:= wdPrintView;
wa1.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;//定位到页眉
wa1.Selection.TypeText(InputString+'报收支纪录');
wa1.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageFooter;//切换到页角
wa1.Selection.TypeText('第');
pagefoot:=wdfieldpage;
wa1.Selection.Fields.Add(wa1.Selection.Range,pagefoot,TempEmpty,TempEmpty);//插入页码
wa1.Selection.TypeText('页');
wa1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;//居中
wa1.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;//返回到主页面
wa1.ChangeFileOpenDirectory(dir);
TempWord:=dir1;
wa1.ActiveDocument.SaveAs(TempWord,FileFormat,temp1,TempEmpty,temp2,TempEmpty,temp1,temp1,temp1,temp1,temp1);
Application.Messagebox(pchar('数据成功导出' + dir+dir1),'哈哈!',mb_Ok);
wa1.Disconnect;
end;