-
I'm stuggling with the syntax to run the Word search from Excel. Here's some code that I've got so far. The Word code contained within the asterisks works within Word to return the values you're looking for. The code requires a reference to Microsoft Word.
[vba]
Sub OpenAndReadWordDoc()
' assumes that the previous procedure has been executed
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim tString As String, tRange As Word.Range
Set wrdApp = CreateObject("Word.Application")
'wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\MyNewWordDoc.doc")
' example word operations
With wrdDoc
'**************************
'This section comes from Word and needs adapting
Dim MyPos As Range, Paras As Long, Chk As Long, Pg As Long
With Selection.Find
Do
.ClearFormatting
.Text = "stop"
.Execute
Set MyPos = ActiveDocument.Paragraphs(1).Range
MyPos.SetRange Start:=MyPos.Start, _
End:=Selection.Range.End
Paras = MyPos.Paragraphs.Count
Pg = Selection.Information(wdActiveEndPageNumber)
If Paras = Chk Then Exit Do
Chk = Paras
MsgBox "page " & Pg & " - " & Paras
Loop
End With
'**************************
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
ActiveWorkbook.Saved = True
End Sub
[/vba]
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules