你挑
VBA字代碼(這個地方的模塊中)
Option Explicit
Sub VBASample()
Dim para As Paragraph
Dim strParaText As String
Dim searchString As String
searchString = "Sample"
For Each para In ActiveDocument.Paragraphs
If para.Style = "Heading 2" Then
strParaText = para.Range.Text
If InStr(1, strParaText, searchString, vbTextCompare) Then
'~~> You code here if the text is found
MsgBox "Found"
End If
End If
Next para
End Sub
VB6 CODE
Option Explicit
'~~> Using Late Binding so no reference to Word object is required.
Sub VB6Sample()
Dim oWordApp As Object, oWordDoc As Object, oWordPara As Object
Dim FlName As String, strParaText As String, searchString As String
'~> File Name where you want to check the paragraphs
FlName = "C:\Documents and Settings\Siddharth Rout\Desktop\Sample.doc"
'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = True
Set oWordDoc = oWordApp.Documents.Open(FlName)
'~~> String that you want to search
searchString = "Sample"
For Each oWordPara In oWordDoc.Paragraphs
If oWordPara.Style = "Heading 2" Then
strParaText = oWordPara.Range.Text
If InStr(1, strParaText, searchString, vbTextCompare) Then
'~~> You code here if the text is found
MsgBox "Found"
End If
End If
Next oWordPara
End Sub