看word本身的文档,上面有的选择帮助,上面有VBA声明word的接口,我想你一定可以找到你要的东西的!
解决方案 »
- 请教关于DLL的两个问题:能否建立一个有Form的DLL,代码直接显示
- 求助
- 如何在数据库中写入十六进制数?
- 现在有那些关于delphi opengl方面的书呢?推荐一下吧.
- 在线请教:怎么在程序里面实现对word的打印设置(如打印页码范围,按纸型缩放的选择等)?
- 我有很多ICO图标,我想用它在TOOLButton 可是图标很小?该怎么把它放大一些呢!客户的需求是大图标!!!分不够,另外开帖!
- 如何获取shellexcute的输出结果?
- image移動時會閃爍的問題??
- 局域网与广域网,数据同步问题
- 本人初学Dephi,简单问题,请指教!
- 在delphi中,我想要查询在姓名中含有“英”的所有的姓名,如何做?
- 小问题
上面有在Delphi中操作word的例子
Word:Variant;
begin
Word:=CreateOLeObject('Word.application');
//用VBa操作word
//如:Word.documents.add;
Word.quit;//退出
end;
其中有一个books,但我在d6上他提示找不到word_tlb.dcu,该如何?我不能运行此程序,不知结果是否和我想要的一样?
我说两种思路罢:
1、在程序中打开WORD,并将WORD的VBA代码转换成Delphi代码执行,就象在WORD中操作一样;这种方法速度稍慢,可用于处理数据量小,而且出现的频率高的报表。
2、在程序中打开WORD,并运行WORD的中宏(这个宏已事先被建立),运行时所需的数据可以从临时文件中读取。下面是这个读文件的宏的一个简单例子:Sub Macro1()
'
'这个宏用来读取文件
'并将文件内容经处理后写入文档
'
'文件中每行三个字符串,中间用TAB键分开,结尾用换行符 Dim fs As Object
Dim f As Object
Dim i, j As Integer
Dim ss, s1, s2, s3 As String
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=5, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("d:\test.txt", 1, TristateFalse)
With ActiveDocument.Books
.DefaultSorting = wdSortByLocation
.ShowHidden = False
End With
ss = " "
Selection.Find.ClearFormatting
With Selection.Find
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With Do
ss = f.ReadLine
i = Pos(ss, Chr(9), 1)
j = Pos(ss, Chr(9), i)
If i <> 0 And j <> 0 Then
s1 = Left(ss, i - 1)
s2 = Mid(ss, i + 1, j - i - 1)
s3 = Right(ss, Len(ss) - j)
Else
s1 = ""
s2 = ""
s3 = ""
End If
Selection.Find.Text = "姓名"
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText (s1)
Selection.Find.Text = "性别"
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText (s2)
Selection.Find.Text = "年龄"
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText (s3)
Selection.TypeParagraph
Selection.Paste
Selection.MoveUp Unit:=wdLine, Count:=3
Selection.HomeKey
If f.AtEndofStream = True Then Exit Do
Loop
Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=3, Extend:=wdExtend
Selection.Delete
f.Close
End SubPublic Function Pos(ByVal source As String, ByVal substr As String, ByVal start As Integer) As Integer
Dim i As IntegerPos = 0
For i = start + 1 To Len(source)
If Mid(source, i, Len(substr)) = substr Then
Pos = i
Exit Function
End If
Next iEnd Function
我先在多一个办公平台的公文成文就是这样做的。
给你段大概的代码:Word替换标记字符串要使用WordDocument.Range.Find.Execute
下面的例子中用“Delphi”替换了“VB”:
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace: OleVariant;
begin
FindText := 'VB';
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := 'Delphi';
Replace := True; WordDocument.Range.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace ); 说明:你只要定义好FindText 在Word模板中就可以了!有问题给我发短信息
合同的头和尾可以用find找到和替换,中间的表格中的数据又如何,且数据纪录条数每次不一样
合同的头和尾可以用find找到和替换,中间的表格中的数据又如何,且数据纪录条数每次不一样
可以在文档中对你的表格定位
WordDocument.Tables.items[].Cell[][]看到了吗?可以单个格子操作!
如果你不知道VBA怎么写,告诉你个方法
你启动Word宏录制然后用手工的方法去增加行或列
之后查看录制的宏,你就可以看到对应的代码了!