Reputation: 4781
I'm a third year software design student. I'm currently in the middle of my work placement. I had been asked to create a tool to parse word documents for relevant tabular data and export it to ms excel. I had never had any dealings with VB but based on the research I carried out I decided that was the route to take. Anyway I managed to execute this successfully. Now, however, I have been asked to expand the program to parse paragraphs also. The documents are broken down into chapters. Each chapter heading is in 'h2' style, my idea is to try to search all text in 'h2' style for a unique string and if found, which should be the required heading, then process the text underneath.
Can this be done?? If so, can you please let me know how, this has been a steep learning curve, and I was delighted with what I had achieved, now, I'm stuck dead. I have the methodology, it's finding the correct way to implement it. This will also allow me to create a search form which I intend to integrate to allow the user search for the chapter they want to parse...
If anyone can help me get out of this hole I would greatly appreciate it.
Upvotes: 1
Views: 293
Reputation: 149287
Take your Pick
VBA WORD CODE (Place this in a module)
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
Upvotes: 1