I have multiple documents that contain list of Equipment and Procedures for different Methods. I want a code that can do the following:
1st: Locate a given Method Number in Word Doc
2nd: Look right after Method number to determine which comes first, "Equipment -" or "Procedure and Evaluation -". "Equipment -", if present will always come before "Procedure and Evaluation -", but if "Procedure and Evaluation -" is first, then "Equipment -" will not be present.
3rd: Copy the range of text between "Equipment -" and "Procedure and Evaluation -" (if "Equipment -" is present) and paste to Excel
4th: Copy the range of text between "Procedure and Evaluation -" and "Design", and paste to Excel. ("Design" is the word that indicates the end of the Method Number)
Unfortunately, I am not good at going between Excel and Word, and I know the code below has numerous issues. The use of "rng.Find" does not seem to be allowed the way I am using it, along with multiple other things I'm sure. Any help to point me in the direction of being able to locate which word comes first in a document, and being able to transfer a range of text, based on specific words, to Excel would be greatly appreciated.
Sub Find_and_Copy()
Dim oWord As Word.Application
Dim oWdoc As Word.Document
Dim LastRow As Integer
Dim i As Integer
Dim rng As Range
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim Text1 As String
Dim Text2 As String
Set oWord = Word.Application
Set oWdoc = oWord.Documents.Open("C:\Test.docx")
Set rng = oWdoc.Range
LastRow = Sheets("Temp").Cells.SpecialCells(xlCellTypeLastCell).Row
'Loop through rows searching for the Method Number, Equipment, Procedure, & Design
'Start on Row 4
For i = 4 To LastRow
'Check to make sure cell is not blank, if it is, then go to next iteration
If Sheets("Temp").Cells(i, 6).Value = "" Then
GoTo NextIteration
End If
'Set the Method Number to find
strFnd = Sheets("Temp").Cells(i, 6).Value & "."
'Locate the Method Number and transfer the text between (but not including) "Equipment -" & "Procedure and Evaluation -" and "Procedure and Evaluation -" & "Design" if they both appear.
'I don't know how to check if Equipment - comes before "Procedure and Evaluation"
If rng.Find.Execute(FindText:=strFnd) Then
Set rng1 = oWdoc.Range(rng.End, oWdoc.Range.End)
If rng1.Find.Execute(FindText:="Equipment -") Then
Set rng2 = oWdoc.Range(rng1.End, oWdoc.Range.End)
If rng2.Find.Execute(FindText:="Procedure and Evaluation -") Then
Text1 = oWdoc.Range(rng1.End, rng2.Start).text
Set rng3 = oWdoc.Range(rng2.End, oWdoc.Range.End)
If rng3.Find.Execute(FindText:="Design") Then
Text2 = oWdoc.Range(rng2.End, rng3.Start).text
Else
Text2 = ""
End If
ElseIf rng2.Find.Execute(FindText:="Desing") Then
Text1 = oWdoc.Range(rng1.End, rng2.Start).text
End If
ElseIf rng1.Find.Execute(FindText:="Procedure and Evaluation -") Then
Set rng2 = oWdoc.Range(rng1.End, oWdoc.Range.End)
If rng2.Find.Execute(FindText:="Design") Then
Text2 = oWdoc.Range(rng1.End, rng2.Start).text
Else
Text2 = ""
End If
Esle
Text1 = ""
Text2 = ""
GoTo NextIteration
End If
End If
Sheets("Temp").Cells(i, 6).Value = Text1
Sheets("Temp").Cells(i, 7).Value = Text2
Next
Cleanup:
oWdoc.Close
Set oWdoc = Nothing
oWord.Quit
Set oWord = Nothing
End Sub
Update to Question: Here is an example of the Word Doc
Method Number: 11111.1
A. Procedure and Evaluation - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- Alkjaasdlkajghlja
- Jlasjdfkjasd;lfjlakdjs
Design
-----Page Break----
Method Number: 22222.2
A. Equipment - bbbbbbbbbbbbbbbbbbbbbbbbbbb
- asdfasdf
- asdfasf
- asdfasdf
- asdfadf
- asdfasdf
B. Procedure and Evaluation - cccccccccccccccccccccccccccccccccccc.
- Asdfasdfasdfasdf
Design
----Page Break-----
If I am looking for Method Number: 11111.1, then I want the code to be able to take information from "Procedure and Evaluation -" and place that in Column 7. For Method Number: 22222.2, I want the code to be able to take the text in "Equipment -" and place it in Column 6, and place the "Procedure and Evaluation -" text in Column 7 again.
Notes about Document:
-The Method Number is located in a textbox, the rest of the text is normal
-There is a page break between Methods
Range
objects. You need to distinguish between them when declaring variables, e.g.Dim rng As Word.Range
– Timothy Rylatt