marsy00
09-27-2012, 01:54 PM
Hello Again,
So, I'm still trying to figure out exactly how the range object behaves. I've run into another question that I hope someone can help me with.
Below I paste an example requirement from my requirements document. I've added the styles of each portion in {} since I cant paste the actual word content into here.
SYS:0089 {Style = Requirement}
HW, FW {Style = Affects}
Example Requirement. This requirement is based on: {Style = Normal}
• IEC 60601-1, Subclause 6.3.1 {Style = Standards Char}
• IEC 45502-1, Subclause 15.6 {Style = Standards Char}
[Trace Tag] {Style = Trace}
§
Now, what I want to do is generate a report in Excel that lists each requirement in the document and parses the content into columns based on style. I have it working using the Find object, but it only finds the first item in the requirement with the given style. For example, the above requirement would only return the first bullet for the Standards style.
In my code below, the commented out block (near the bottom) is the functional code that only returns the one bullet. The code just below that is what I used to try to return both bullets. But, the problem is that when I use the do loop, the .find is executing on the entire document (i.e. its parsing the entire document full of requirements, not just the one requirement that I'm trying to work with). I thought that my r.MoveEndUntil cset:="§" limited the range to just the one requirement.
Any help is greatly appreciated! Thanks!
Private Function getReqData(req As String, toGet As String) As String
Dim r As Range
Dim i
Set r = ActiveDocument.Range
i = 0
' Find the requirement "req" in the document.
With r.find
.ClearFormatting
.Style = ActiveDocument.Styles("Requirement")
.Text = req
.Replacement.Text = ""
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindStop
If .Execute = True Then
'Once found, exit the with block and find the "toGet" style.
Else
'If the requirement wasnt found, then exit the function (error condition).
getReqData = "ERROR:NOT FOUND"
Exit Function
End If
End With
' Expand the range to include the entire requirement.
r.MoveEndUntil cset:="§"
' Find the item in the requirement with the style = toGet.
With r.find
.ClearFormatting
.Style = ActiveDocument.Styles(toGet)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindStop
' If .Execute = True Then
' getReqData = r.Text
' End If
Do While .Execute = True
If i = 0 Then
getReqData = r.Text
Else
getReqData = getReqData & vbCr & r.Text
End If
i = i + 1
Loop
End With
End Function
So, I'm still trying to figure out exactly how the range object behaves. I've run into another question that I hope someone can help me with.
Below I paste an example requirement from my requirements document. I've added the styles of each portion in {} since I cant paste the actual word content into here.
SYS:0089 {Style = Requirement}
HW, FW {Style = Affects}
Example Requirement. This requirement is based on: {Style = Normal}
• IEC 60601-1, Subclause 6.3.1 {Style = Standards Char}
• IEC 45502-1, Subclause 15.6 {Style = Standards Char}
[Trace Tag] {Style = Trace}
§
Now, what I want to do is generate a report in Excel that lists each requirement in the document and parses the content into columns based on style. I have it working using the Find object, but it only finds the first item in the requirement with the given style. For example, the above requirement would only return the first bullet for the Standards style.
In my code below, the commented out block (near the bottom) is the functional code that only returns the one bullet. The code just below that is what I used to try to return both bullets. But, the problem is that when I use the do loop, the .find is executing on the entire document (i.e. its parsing the entire document full of requirements, not just the one requirement that I'm trying to work with). I thought that my r.MoveEndUntil cset:="§" limited the range to just the one requirement.
Any help is greatly appreciated! Thanks!
Private Function getReqData(req As String, toGet As String) As String
Dim r As Range
Dim i
Set r = ActiveDocument.Range
i = 0
' Find the requirement "req" in the document.
With r.find
.ClearFormatting
.Style = ActiveDocument.Styles("Requirement")
.Text = req
.Replacement.Text = ""
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindStop
If .Execute = True Then
'Once found, exit the with block and find the "toGet" style.
Else
'If the requirement wasnt found, then exit the function (error condition).
getReqData = "ERROR:NOT FOUND"
Exit Function
End If
End With
' Expand the range to include the entire requirement.
r.MoveEndUntil cset:="§"
' Find the item in the requirement with the style = toGet.
With r.find
.ClearFormatting
.Style = ActiveDocument.Styles(toGet)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindStop
' If .Execute = True Then
' getReqData = r.Text
' End If
Do While .Execute = True
If i = 0 Then
getReqData = r.Text
Else
getReqData = getReqData & vbCr & r.Text
End If
i = i + 1
Loop
End With
End Function