如何灵活控制word文档(300分)

  • 主题发起人 主题发起人 lijunwunihao
  • 开始时间 开始时间
L

lijunwunihao

Unregistered / Unconfirmed
GUEST, unregistred user!
我用Delphi创建了一些word文档,但是无法用代码控制它的文字区域。请问如何控制它的焦点转移。谢谢!本人奉上300块。
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=737517
Delphi与Word之间的融合技术 ( 积分:0, 回复:233, 阅读:17047 )

到这个贴子看看去?
 
如何控制Word
启动Word时用如下代码:
begin
try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';
end;
让Word打开一个指定的文件,需要先放置OpenDialog,然后调用WordApplication.Documents.Open:
var
ItemIndex :OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
begin
if not dlgOpen.Execute then
Exit;

{Open document}
FileName := dlgOpen.FileName;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;

WordApplication.Documents.Open( FileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );

{Assign WordDocument component}
ItemIndex := 1;
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));

{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
end;

判断Word是否运行
先在Form上添加一个Server组件中的WordApplication,命名为WordApplication1,然后在"关闭WORD"按扭中输入如下代码:
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication1.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication1.Disconnect;
except
on E: Exception do
begin
Showmessage(E.Message);
WordApplication1.Disconnect;
end;
end;
end;
准成,当然了,请按实际情况修改 SaveChanges, OriginalFormat, RouteDocument的内容.

如何输出到Word
如果用的是MICROSOFT 旗下的数据库管理系统生成的表例如*.DBF等,可以通过OLE技术直接在WORD文档中显示完整的数据表。
如果用PARADOX表,可以用下面的方法:
var
msword:variant;
begin
try
msword:=createoleobject('word.basic');
msword.filenew;
msword.appshow;
table1.disablecontrols;
try
bookmark:=table1.getbookmark;
try
msword.bold;
msword.insert('报表标题'#13#10);
msword.insert('字段名1'+#9+....+'字段名n'#13#10);
table1.first;
while not table1.eof do
begin
msword.insert(table1.fieldbyname(字段名1).asstring+#9+.....+table1.fieldbyname(字段名n).asstring+#13#10);
msword.next;
end;
finally
table1.gotobookmark(bookmark);
table1.freebookmark(bookmark);
end;
finally
table1.enablecontrols;
end;
except
showmessage('没有发现WORD,请安装!');
end;
end;
//在OLE服务器WORD启动后自动写了一个制表文件,此时只要选择全部数据(除标题),然后选插入表格即可作出WORD报表。
//如果有其他好方法请与我联系。
//我想解决的是在程序中自动画出表格。


控制Word
var
tbl : Table;
i,j:integer;
un_Var,ex_Var,cnt_Var:OleVariant;
row_num,col_num:integer;
st:string;
begin
// 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小
WordApplication1.Connect;
WordApplication1.Visible := True;
WordApplication1.Documents.Add(EmptyParam,EmptyParam);
WordDocument1.Connect;
WordApplication1.ActiveWindow.View.Type_:= wdNormalView;
WordApplication1.Selection.Font.Name :='黑体';
WordApplication1.Selection.Font.Size := 16;
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApplication1.Selection.TypeText('昆明市土地信息系统表格输出');
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.Font.Name := '宋体';
WordApplication1.Selection.Font.Size :=12;
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
WordApplication1.Selection.TypeText('日期'+formatdatetime('yyyy"年"mm"月"dd"日"',now));
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeParagraph;//回车
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
row_num:=table1.RecordCount;
col_num:=table1.Fields.Count;
tbl := WordApplication1.ActiveDocument.Tables.Add(WordApplication1.Selection.Range,row_num+1,Col_num);
un_Var:=wdCharacter;
cnt_Var:=1;
ex_Var:=wdMove;
table1.First;
for j := 0 to Col_num-1 do //标题
begin
st:=table1.Fields.Fields[j].FieldName;
WordApplication1.Selection.TypeText(st);
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end;
for i := 0 to row_num-1 do // 行
begin
for j := 0 to Col_num-1 do // 列
begin
st:=table1.Fields.Fields[j].AsString;
WordApplication1.Selection.TypeText(st);
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end;
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
table1.next;
end;
WordApplication1.Selection.TypeText('制表人:阎磊');
WordApplication1.Selection.TypeParagraph;
end;


可以参考以下代码:
procedure PrintReport4(DSR, AJXZ, CFSJ, JBAQ: PChar); stdcall;
var
Word, Doc, Fields:OleVariant;
begin
Word:=CreateOleObject('Word.Application');

Word.Visible:=True;

Word.Documents.Add('C:/Customs/案件呈报表');
Doc := Word.ActiveDocument;

Fields := Doc.FormFields;
Fields.Item('DSR').Result := String(DSR);
Fields.Item('AJXZ').Result := String(AJXZ);
Fields.Item('CFSJ').Result := String(CFSJ);
Fields.Item('JBAQ').Result := String(JBAQ);
end;

其中案件呈报表就是word模版文件。


打开WORD文档的一段程序
uses ComObj ;
procedure TForm1.Button1Click(Sender: TObject);
var
vWord,vDoc,vRange : Variant ;
sText,sReplace : string ;
lReturn : Boolean ;
begin
sText := 'ABCDEFG' ; //原文字串
sReplace := 'GFEDCBA' ; //新字串
vWord := CreateOleObject('Word.Application') ;//创建Word线程
try
//打开要操作的文件
vDoc := vWord.Documents.Open('C:/My Documents/AAAc.Doc');
vDoc.Select ; //选取中整个文档
vRange := vDoc.Range ; //替换范围
lReturn := True ;
while lReturn do
begin //找到并替代成功则返回 True 共11个参数
lReturn := vDoc.Range.Find.Execute(sText,,,,,,,,,sReplace,True) ;
end ;
finally
vDoc.Close(True) ; //关闭文并保存
vWord.Quit(False) ; //退出Word
end ;
end;
 
在word里面录制宏,然后参考上面各位说的,
 
获取word接口,然后参考vba上的方法调用。
 
Word里打开vb编辑器,可以看里面的帮助文件,很全的
 
后退
顶部