vfp控制word

b22222222 2003-05-13 05:09:16
我已经通过程序新建了一个word工作表,
OleApp:=CREATEOBJECT("word.Application")
Application.Visible=.T.
OleApp.documents.Add()
成功后,工作表是空的,我怎样在工作表中插入一些文字,或者一个表格,然后再表格里插入
一些东西呢?
...全文
725 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
  • 打赏
  • 举报
回复
你可以下载个控件类
目 录 一、建立WORD应用程序对象,新建、打开、保存WORD文档 5 1、建立对象 5 2、显示WORD窗口 5 3、新建一个WORD文档 5 (1)Count属性 5 (2)Name属性 5 4、打开一个已存在的WORD文档 5 5、激活文档,使文档成为当前激动文档 6 6、保存文档 6 7、页面设置 6 (1)Orientation属性 6 (2)TopMargin属性 6 (3)BottomMargin属性 6 (4)LeftMargin属性 6 (5)RightMargin属性 7 (6)PageWidth属性 7 (7)PageHeight属性 7 8、打印文档 7 9、关闭文档 7 10、退出WORD 8 二、Font对象 8 1、Name 属性 8 2、Size属性 8 3、Bold属性 8 4、Italic属性 8 5、Color属性 8 6、Underline属性 8 7、UnderlineColor属性 8 8、Subscript属性 9 9、Superscript属性 9 10、Spacing属性 9 11、Scaling属性 9 三、Paragraph和Paragraphs集合对象 9 1、添加段落 9 (1) Add方法 10 (2)InsertParagraph方法 10 (3)InsertParagraphAfter 方法 10 (4)InsertParagraphBefore 方法 11 (5)TypeParagraph 方法 11 2、设置段落格式 11 (1)FirstLineIndent属性 11 (2)LeftIndent属性 11 (3)RightIndent属性 11 (4)Alignment属性 11 (5)LineSpacing属性 12 (6)HangingPunctuation属性 12 四、Range 和 Selection 对象 12 (1)Range对象 12 (2)Selection 对象 13 (3)常用属性 13 ①Start属性 13 ②End属性 14 ③Text属性 14 ④Type属性 14 ⑤Information 属性 15 (4)常用方法 18 ①Copy 方法 18 ②Cut 方法 18 ③Paste 方法 18 ④Collapse 方法 18 ⑤InsertBefore 方法 19 ⑥InsertAfter方法 19 ⑦TypeText方法 19 ⑧Delete 方法 20 ⑨移动插入点的方法 20 ㈠Move 方法 20 ㈡MoveDown 方法 20 ㈢MoveEnd 方法 21 ㈣MoveEndUntil 方法 21 ㈤MoveLeft 方法 22 ㈥MoveRight方法 22 ㈦MoveStart 方法 23 ㈧MoveStartUntil 方法 23 ㈨MoveUp 方法 24 五、表格处理 25 1、Table 对象和Tables 集合对象 25 2、添加表格 25 (1)PreferredWidthType 属性 25 (2)AllowAutoFit属性 26 (3)AllowPageBreaks属性 26 3、Border 对象和Borders 集合对象 26 (1)LineStyle属性 26 (2)LineWidth属性 27 (3)Color属性 27 (4)Enable 属性 27 (5)DefaultBorderLineWidth 属性 27 (6)DefaultBorderLineStyle 属性 27 (7)DefaultBorderColor 属性 27 4、Cell 对象和Cells 集合对象 28 5、Row 对象和Rows 集合对象 28 (1)Height 属性 28 (2)Delete 方法 28 (3)SetHeight 方法 28 6、Column 对象和Columns 集合对象 29 (1)Width 属性 29 (2)Delete 方法 29 (3)SetWidth 方法 29 7、合并与拆分单元格 30 (1)合并单元格 30 (2)拆分单元格 30 六、Shape 对象和Shapes 集合对象 32 1、常用方法 32 (1)AddLine 方法 32 (2)AddTextbox 方法 33 (3)AddTextEffect 方法 33 (4)AddPicture 方法 34

2,748

社区成员

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

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