@xld
Thanks for your help..thanks to your input I have this working.
I have of course come up against another issue. The documents that I am parsing are 20,000+ words, so on a document this size, my parser is quite slow. If I select the last chapter heading in the document on my checklist it takes between 30-40 seconds to hit, which of course is far far too long..
Can you or anyone have a quick check over my code and see if there might be a more efficient way to carry out this task that would speed up my program??
I hasten to add that I am a third year college student on placement and my first introduction to VB was the week before last. So any help would be really really appreciated....
HTML Code:
'================================================================
' POPULATING LIST BOX WITH DATA IN
' CONFIG WORKSHEET
'================================================================
Private Sub ListBox1_Click()
ListBox1.ListFillRange = "Sheet2!A1:A45"
End Sub
'================================================================
' PROCESSING LISTBOX SELECTION
'================================================================
Private Sub Parse_Click()
Dim i As Long
Dim msg As String
Dim Check As String
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
msg = msg & .List(i)
End If
Next i
End With
If msg = vbNullString Then
MsgBox "Nothing Selected!!"
Else
Check = MsgBox("You Selected:" & vbNewLine & msg & vbNewLine & _
"Is This Correct?", _
vbYesNo + vbInformation, "Please Confirm")
If Check = vbYes Then
ParseDoc msg
Else
'User wants to try again, so clear listbox selections and
'return user to the userform
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = False
Next
End If
End If
End Sub
Public Sub ParseDoc(ByVal msg As String)
'======================================================================
' DECLARING VARIABLES
'======================================================================
Dim ExcelBook As Object
Dim i As Long
Dim oRow As Row
Dim NextRow As Long
Dim oRng As Range
Dim oPara As Paragraph
Dim fDialog As FileDialog
Dim objWord As Word.Application
Dim objExcel As Excel.Application
'Setting Location of Excel Spread for Parsed Details
Const WorkBookName As String = "C:\Users\edoogar\Documents\ParseProject\ParseDetails.xls"
Set objWord = New Word.Application
Set objExcel = New Excel.Application
objExcel.Visible = True
objWord.Visible = True
'=======================================================================
' SETTING UP THE DIALOG BOX
' FOR FILE SELECTION
'=======================================================================
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select Folder to Process and Click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Operation Cancelled", , _
"List Folder Contents"
'Unload Form1
End If
strPath = fDialog.SelectedItems(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(srtPath) - 2)
End If
On Error Resume Next
Set ExcelBook = objExcel.Workbooks.Open(Filename:=WorkBookName)
'========================================================================
' SETTING STRFILENAME TO THE FOLDER + .DOC NAME
' EG.."C:\PARSE\REPORT1.DOC..C:\PARSE\REPORT2.DOC ETC...
'========================================================================
strFilename = Dir$(strPath & "*.doc")
'=======================================================================
' OPENS EACH WORD DOCUMENT WITHIN THE SELECTED FILE, PARSES
' THROUGH AND SELECTS THE DATA IN EACH CELL IN THE REFERNCED
' TABLES AND COPIES IT OVER TO THE RELEVANT WORKSHEET IN EXCEL
'=======================================================================
While Len(strFilename) <> 0
WordBasic.DisableAutoMacros 1
Set oDoc = objWord.Documents.Open(Filename:=strPath & strFilename, AddToRecentFiles:=False)
'========================================================================
' CHECKING EACH LINE OF DOC FOR LIST BOX ITEM
' DISPLAYING MESSAGE BOX
' (Testing Purposes)
'=========================================================================
For Each oPara In oDoc.Paragraphs
If InStr(1, oPara.Range, msg) > 0 Then
If InStr(1, oPara.Style, "H2") > 0 Then
oPara.Range.Select
MsgBox "You have found the string!"
GoTo CloseDoc
End If
End If
Next oPara
CloseDoc:
oDoc.Close wdDoNotSaveChanges
strFilename = Dir$()
Wend
WordBasic.DisableAutoMacros 0
'ExcelBook.Close
'objExcel.Quit
objWord.Quit
'========================================================================
' TIDYING UP PROGRAM - SETTING OBJECTS TO NULL
'========================================================================
Set fDialog = Nothing
Set ExcelBook = Nothing
Set oDoc = Nothing
Set oRng = Nothing
End Sub