vfp控制word

b22222222 2003-05-13 05:09:16
我已经通过程序新建了一个word工作表,
OleApp:=CREATEOBJECT("word.Application")
Application.Visible=.T.
OleApp.documents.Add()
成功后,工作表是空的,我怎样在工作表中插入一些文字,或者一个表格,然后再表格里插入
一些东西呢?
...全文
660 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rida 2003-05-15
  • 打赏
  • 举报
回复
如果做成模板可能比较好。
我这里有delphi操作word模板的代码
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls, word2000;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
Procedure ReplaceIt(FindStr,ReplaceStr: String);

private
{ Private declarations }
TextRange:variant;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var MSWord: Variant;
begin
MSWord := CreateOLEObject('Word.Application');//连接Word
MSWord.Documents.Open(FileName:='e:\test.doc', ReadOnly:=false);//打开外部Word文档
MSWord.Visible := 1;//是否显示文件编辑
MSWord.ActiveDocument.Range(Start:=0, End:=0);//开始改变的启止位置
MSWord.ActiveDocument.Range.InsertAfter(Text:='Title');//在Word中增加字符'Title'
MSWord.ActiveDocument.Range.InsertParagraphAfter;
MSWord.ActiveDocument.Range.Font.Name := 'Arial';//字体名称
MSWord.ActiveDocument.Range.Font.Size := 24;//字体大小
MSWord.ActiveDocument.Save;
MSWord.Application.Quit(0);
//MSWord.Documents.SaveAs('另存为的文件全路径名');

end;
procedure TForm1.Button2Click(Sender: TObject);
var
word,t:Variant;
activerow: integer;
begin
Word:=CreateOLeObject('Word.application');
word.documents.add(Template:='e:\test.dot');
word.visible := 1;
word.selection.endkey;
word.activedocument.Paragraphs.add;
activerow:=word.activedocument.Paragraphs.count;
TextRange := word.activedocument.range;
word.activedocument.paragraphs.item(activerow).range.text:='哈哈,加进去了。';//这里加入
replaceit('<#hello>','aaahello');
replaceit('<#title>','aaatitle');
t:= word.activedocument.tables.item(1);
t.cell(1,1).range.text :='a';
t.cell(1,2).range.text :='b';
t.cell(1,3).range.text :='c';

word.activedocument.saveas('e:\title.doc');

end;


procedure TForm1.ReplaceIt(FindStr,ReplaceStr: String);
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace,
matchkashida,matchdiacritics,matchalefhamza,matchcontrol: olevariant;
begin
findtext := FindStr;
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdFindContinue;
format := false;
replacewith := ReplaceStr;
Replace:= true;
while TextRange.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms,
forward,wrap, format, replacewith, replace,matchkashida,
matchdiacritics,matchalefhamza,matchcontrol )
do begin
// showmessage(IntToStr(TextRange.Start));
end;
end;



end.
rida 2003-05-15
  • 打赏
  • 举报
回复
这是vfp中一个实例中的代码。
#DEFINE CRLF CHR(13)+CHR(10)
#DEFINE C_MESS1_LOC "Is this a snowy day?"
#DEFINE C_MESS2_LOC "Is this a rainy day?"
#DEFINE C_MESS3_LOC "Is this a sunny day?"

lWord97 = .T.
nMouseRow = MROW()
nMouseCol = MCOL()
oForm = THISFORM

oForm.addobject('oWordDoc','olecontrol','word.document')

oForm.oWordDoc.Height = THISFORM.txtFrame.height
oForm.oWordDoc.Width = THISFORM.txtFrame.width
oForm.oWordDoc.Top = THISFORM.txtFrame.top
oForm.oWordDoc.Left = THISFORM.txtFrame.left
oForm.oWordDoc.Visible = .t.
oForm.Show
oForm.oWordDoc.DoVerb(0)

IF TYPE("oForm.oWordDoc.object.name") # "C"
lWord97 = .F.
oWordRef = GetObject('','word.basic')
ENDIF

MOUSE CLICK AT 1,1
MOUSE AT m.nMouseRow,m.nMouseCol

IF m.lWord97
oForm.oWordDoc.object.content = C_MESS1_LOC+CRLF
oForm.oWordDoc.object.content.insertafter(C_MESS2_LOC+CRLF)
oForm.oWordDoc.object.content.insertafter(C_MESS3_LOC)
oForm.oWordDoc.object.content.Font.Bold = .T.
oForm.oWordDoc.object.content.Font.Size = 18
oForm.oWordDoc.object.content.Font.Name = "Arial"
oForm.oWordDoc.object.content.Font.ColorIndex = 5
ELSE
oWordRef.Insert(C_MESS1_LOC+CRLF)
oWordRef.editselectall
oWordRef.Font("Arial",18)
oWordRef.Bold
oWordRef.EditGoTo("\EndofDoc")
oWordRef.WordLeft(4)
oWordRef.SelectCurWord
oWordRef.CharColor(2)

oWordRef.EditGoTo("\EndofDoc")
oWordRef.Insert(C_MESS2_LOC)
oWordRef.WordLeft(3)
oWordRef.SelectCurWord
oWordRef.CharColor(4)
oWordRef.Bold(0)

oWordRef.EditGoTo("\StartofDoc")
oWordRef.Insert(C_MESS3_LOC+CRLF)
oWordRef.WordLeft(4)
oWordRef.SelectCurWord
oWordRef.CharColor(6)
oWordRef.Bold(0)
oWordRef.Italic

oWordRef.editselectall
oWordRef.shadingpattern(2)
oWordRef.borderoutside()
oWordRef.borderlinestyle(7)
ENDIF

THIS.ENABLED = .F.
tangmins 2003-05-14
  • 打赏
  • 举报
回复
你有沒有使用VFP7,它有個工具類察看器,可使用它來看下WORD的方法。就應可以搞定了吧
daniel2008 2003-05-14
  • 打赏
  • 举报
回复
关注
jiaxingxing 2003-05-13
  • 打赏
  • 举报
回复
你可以下载个控件类

2,726

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧