delphi操作word的问题。(50)

S

sxper

Unregistered / Unconfirmed
GUEST, unregistred user!
1.delphi操作excel相信大家都很上手。但是对于word就麻烦对了。主要是定位的问题和表格。也许可以使用vbs来实现。但是也是比较麻烦。因为不可见。2.我研究vbs后发现。如果要生成指定的word格式。最好的办法是使用替换。也就是先做好模板表格。替换指定的关键字。最后输出指定的格式。3 请教做过此类程序的人。这样做的可行性================vbs宏=================Sub Macro1()'' Macro1 Macro' 宏在 2009-2-6 由 Administrator 录制' Selection.MoveDown Unit:=wdLine, Count:=2 Selection.MoveRight Unit:=wdCharacter, Count:=3 Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend Selection.Copy Selection.ParagraphFormat.Alignment = wdAlignParagraphRight Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "内容" .Replacement.Text = "你好" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With With Selection If .Find.Forward = True Then .Collapse Direction:=wdCollapseStart Else .Collapse Direction:=wdCollapseEnd End If .Find.Execute Replace:=wdReplaceOne If .Find.Forward = True Then .Collapse Direction:=wdCollapseEnd Else .Collapse Direction:=wdCollapseStart End If .Find.Execute End WithEnd Sub
 
使用Delphi的Variant类型的变量. Var word:Variant; word:=createOleObject('word.basic');//''中的从注册表中查. //以下的word.后的命令可从宏语言VBA中得到. word.FileNew('test'); word.insert('this is a test'); word.run('宏名'); 2 楼 回复于 2001-03-07 14:59:00 得分 0 just like var word:Variant; begin word:=OleCreateObject('Word.Basic'); //word后的命令参照VBA(不同的Word版本写法不同) word.OpenFile('fileName'); word.Run('macroname') end; 欢迎转载,但请保留出处,本文章转自[华软网] 原文链接:http://www.huarw.com/program/bbs/Delphi/200103/226900.html====================================Try ExcelApplication1.Connect;//EXCEL应用程序 Except MessageDlg('本机可能没有正确安装Excel,请检查!',mtError, [mbOk], 0); Abort; End; try ExcelApplication1.Visible[0]:= cbView.Checked ; ExcelApplication1.Caption:='Excel Application'; excelapplication1.Workbooks.Open(edtSourceFile.text,null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件 except begin ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelApplication1.Quit; showmessage('请选择EXCEL电子表格!'); exit; end; end; try sCurPath := ExtractFilePath(Application.ExeName); //GetCurrentDir; if sCurPath[length(sCurPath)]='/' then delete(sCurPath, length(sCurPath), 1); //获得文件名 sFileBas := sCurPath+'/jsyd.bas' // 文件jsyd.bas为VBA脚本程序    intCount := excelapplication1.VBE.VBProjects.Count; excelapplication1.VBE.VBProjects.Item(intCount).VBComponents.Import(sFileBas); excelapplication1.Run('CovertFile',intB,intRow,intBh+1,edtCovertPath.text) // CovertFile是过程名,后面是这个过程的参数 ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelApplication1.Quit; showmessage('操作完成!'); chdir(ExtractFilePath(Application.ExeName)); except on e:exception do begin ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelApplication1.Quit; showmessage('请把EXCEL中的工具菜单->宏->安全性中可靠来源页的信任对于"Visual Basic 项目"的访问(V)'+e.Message); chdir(ExtractFilePath(Application.ExeName)); exit; end; end; end else begin showmessage('请选择文件路径和输出路径!'); chdir(ExtractFilePath(Application.ExeName)); exit; end;
 
我觉得还是用Vbs的好,以前都是这样弄的,参见我以前的帖子
 
还是问题多多
 

Similar threads

I
回复
0
查看
653
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
564
import
I
I
回复
0
查看
608
import
I
顶部