在vb中
(1)
引用Microsoft Word *.0 Library (*.0表示word版本,小心不兼容)
(2)代码示例(查一个word文档tt.doc中文字aaaaaa是否黑体(可操作宏)
dim word as object
set word=createobject("word.application")
word.documents.open "d:\tt.doc"
with word.selection.find
.text="aaaaaa"
.forward=true
.warp=wdfindcontine
..matchbyte=true
end with
word.selection find.execute
if word.selection.style="黑体"
......
end if
我做了一个比较完整的例子
Dim MyWd As Object '用于存放 Microsoft Word 引用的变量。
Dim MyDoc As Object
Dim WordWasNotRunning As Boolean
Dim DocWasNotOpened As Boolean
Private Sub Get_Word()
On Error GoTo Open_word '不带第一个参数调用 Getobject 函数将
'返回对该应用程序的实例的引用,如果该应用程序不在运行,则会产生错误。
Set MyWd = GetObject(, "Word.Application")
GoTo Set_Visible
Open_word:
Set MyWd = CreateObject("Word.Application")
'如果发生错误则要创建一个Word.Application对象
WordWasNotRunning = True '表明Word是由程序启动的,最后应该关闭
Err.Clear
Set_Visible:
MyWd.Visible = True
MyWd.Activate
End Sub
Private Sub Get_Doc()
Dim MyFile As String
Dim i As Integer
'检测 Microsoft Excel。如果 Microsoft Excel 在运行,
'则将其加入运行对象表。
'DetectExcel
'将对象变量设为对要看的文件的引用。
MyFile = "f:\shj.doc" '改为你的文件所在的绝对路径
If Dir(MyFile) = "" Then Exit Sub
With MyWd
For i = 1 To .Documents.Count
If StrConv(.Documents(i).fullname, vbUpperCase) _
= StrConv(MyFile, vbUpperCase) Then
Set MyDoc = .Documents(i)
GoTo Set_Active
End If
Next
End With
Set MyDoc = GetObject("f:\shj.doc", "Word.Document")
DocWasNotOpened = True
Set_Active:
MyDoc.Activate
'在此处对文件进行操作。
MyDoc.Select
With MyWd.Selection.Find
.ClearFormatting
While .Execute("写作单位") '被替换的内容
MyWd.Selection.TypeText Text:="中科院数学所" '替换后的内容
Wend
End With
'如果不想要自动退出就吧下边几行注释掉
'''''''''''''''''''''''''''''''''''''''''''''
If WordWasNotRunning Then
MyWd.Quit
Else
If DocWasNotOpened Then MyDoc.Close
End If
''''''''''''''''''''''''''''''''''''''''''''
Set MyWd = Nothing '释放对象
Set MyDoc = Nothing
End Sub
Private Sub Command1_Click()
Get_Word
Get_Doc
WordWasNotRunning = False
DocWasNotOpened = False
End Sub
Private Sub Command1_Click()
Dim word As Object
Dim o
Dim selection
Dim doc
Set word = CreateObject("word.application")
Set doc = word.documents.open("c:\d.doc")
o = doc.bookmarks("ssss")
doc.bookmarks("ssss").Select
Set selection = word.selection
selection.Text = "llll"
word.Visible = True
End Sub