Search find / Report

B

Broads

Hi I am, looking to find a solution to a problem.

I am looking to scan through XML code for a keyword and then reading the
text following this to the next ">" character.. in theory my code should
work. But I do not know word vba. I think the key point I am looking for is
the character position where the String is found... Check the following code
and please make suggestions,, evan if my approach is completely wrong......

Code:
Function openSearch(myfile)
Dim searchString As String
Dim curentchar As String
searchString = frmIndexGen.txtSearchString

Dim i As Integer
Dim endfound As Boolean
Dim theError As String


Documents.Open FileName:=myfile


With ActiveDocument.Content.Find
Do While .Execute(FindText:=searchString, Forward:=True,
Format:=True) = True
i = 1 ' Should be the char after the string I am looking for.
For i = 1 To ActiveDocument.Characters.Count - 1
If ActiveDocument.Characters(i) = ">" Then
endfound = True: Exit For
Else
theError = theError + ActiveDocument.Characters(i)
End If
Next i

With Documents(2).Content
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.InsertAfter myfile
.Select
.InsertParagraphAfter
.InsertAfter theError
theError = ""
End With
Loop

End With


ActiveDocument.Close (False)


End Function
 
M

mbaird

Are you trying to get the value of a particular node(element) in the XML file?

<Root>
<Node>Value</Node>
</Root>

Mark Baird
 
B

Broads

Kind of, I am scanning through a directory for all *.xml files opening them
and then searching for the node <ERROR val=

and then writing an error report for the contents upto the closing brace.

eg...
<ERROR val="Invalid shipment (9140002577):Invalid shipment detail
(20):Invalid item.:Item "24901" with company "" does not exist.">


I have got a crude report working, but I believe it can be speeded up.

Code:
Function openSearch(myfile)
Dim searchString As String
Dim curentchar As String

Dim i As Integer
Dim endfound As Boolean
Dim theError As String
Dim last11chars As String
Dim filenameprinted As Boolean
filenameprinted = False
Documents.Open FileName:=myfile

'With ActiveDocument.Content.Find
For i = 1 To ActiveDocument.Characters.Count - 1
If i >= 12 Then last11chars = ActiveDocument.Range(i - 12, i - 1)

If i > 11 And last11chars = "ERROR val=""" Then
If filenameprinted = False Then
Documents(2).Content.Collapse Direction:=wdCollapseEnd
Documents(2).Content.InsertParagraphAfter
Documents(2).Content.InsertParagraphAfter
Documents(2).Content.Font.Bold = wdToggle
Documents(2).Content.InsertAfter myfile
Documents(2).Content.Font.Bold = wdToggle
Documents(2).Content.InsertParagraphAfter
filenameprinted = True
End If

Do Until endfound
If ActiveDocument.Characters(i) = ">" Then
endfound = True
With Documents(2).Content
.InsertAfter theError
.InsertParagraphAfter
.InsertParagraphAfter
theError = ""
last11chars = ""
endfound = False
Exit Do
End With
Else
theError = theError + ActiveDocument.Characters(i)
End If
i = i + 1
Loop
End If
Next i

'End With


ActiveDocument.Close (False)


End Function


mbaird said:
Are you trying to get the value of a particular node(element) in the XML file?

<Root>
<Node>Value</Node>
</Root>

Mark Baird

Broads said:
Hi I am, looking to find a solution to a problem.

I am looking to scan through XML code for a keyword and then reading the
text following this to the next ">" character.. in theory my code should
work. But I do not know word vba. I think the key point I am looking for is
the character position where the String is found... Check the following code
and please make suggestions,, evan if my approach is completely wrong......

Code:
Function openSearch(myfile)
Dim searchString As String
Dim curentchar As String
searchString = frmIndexGen.txtSearchString

Dim i As Integer
Dim endfound As Boolean
Dim theError As String


Documents.Open FileName:=myfile


With ActiveDocument.Content.Find
Do While .Execute(FindText:=searchString, Forward:=True,
Format:=True) = True
i = 1 ' Should be the char after the string I am looking for.
For i = 1 To ActiveDocument.Characters.Count - 1
If ActiveDocument.Characters(i) = ">" Then
endfound = True: Exit For
Else
theError = theError + ActiveDocument.Characters(i)
End If
Next i

With Documents(2).Content
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.InsertAfter myfile
.Select
.InsertParagraphAfter
.InsertAfter theError
theError = ""
End With
Loop

End With


ActiveDocument.Close (False)


End Function
 
M

mbaird

The following is an example of using a wildcard search to find what you are
looking for.

Dim vError As Variant
Dim sError As String

' This sets up a wild card match.
With Selection.Find
.ClearFormatting
.Text = "\<ERROR val=*\>"
.Replacement.Text = ""
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Do While Selection.Find.Execute = True
' Split the string between the '='
vError = Split(Selection.Text, "=")
' Get the second portion of the string.
sError = vError(1)
' Remove the trailing angle bracket.
sError = Left(sError, Len(sError) - 1)
Debug.Print sError
Loop


The other route is to use the Microsoft XML parser which is a considerable
learning curve but will be worth the effort to learn. If you are willing to
look at using the XML parser following is some sample code. This of course
requires a properly formed XML file. You have to set a reference to
"Microsoft XML, v2.6" or you can download "Microsoft XML v4.0" along with the
SDK.

This would require an understanding of XPath and the XML parser. You can
download the Microsoft XML SDK at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmscxmloverview.asp

Dim oErrorFile As New DOMDocument
Dim oErrors As IXMLDOMNodeList
Dim i As Integer

' Open the XML file
oErrorFile.Load "C:\test.xml"

If oErrorFile.parseError = 0 Then

' Get all of the nodes named 'ERROR'
Set oErrors = oErrorFile.SelectNodes("//ERROR")

For i = 0 To oErrors.Length - 1
' Move through each node and get the value of the attibute named
'val'
Debug.Print oErrors(i).Attributes.getNamedItem("val").Text
Next i
Else
' File could not be open
End If


Mark Baird


Broads said:
Kind of, I am scanning through a directory for all *.xml files opening them
and then searching for the node <ERROR val=

and then writing an error report for the contents upto the closing brace.

eg...
<ERROR val="Invalid shipment (9140002577):Invalid shipment detail
(20):Invalid item.:Item "24901" with company "" does not exist.">


I have got a crude report working, but I believe it can be speeded up.

Code:
Function openSearch(myfile)
Dim searchString As String
Dim curentchar As String

Dim i As Integer
Dim endfound As Boolean
Dim theError As String
Dim last11chars As String
Dim filenameprinted As Boolean
filenameprinted = False
Documents.Open FileName:=myfile

'With ActiveDocument.Content.Find
For i = 1 To ActiveDocument.Characters.Count - 1
If i >= 12 Then last11chars = ActiveDocument.Range(i - 12, i - 1)

If i > 11 And last11chars = "ERROR val=""" Then
If filenameprinted = False Then
Documents(2).Content.Collapse Direction:=wdCollapseEnd
Documents(2).Content.InsertParagraphAfter
Documents(2).Content.InsertParagraphAfter
Documents(2).Content.Font.Bold = wdToggle
Documents(2).Content.InsertAfter myfile
Documents(2).Content.Font.Bold = wdToggle
Documents(2).Content.InsertParagraphAfter
filenameprinted = True
End If

Do Until endfound
If ActiveDocument.Characters(i) = ">" Then
endfound = True
With Documents(2).Content
.InsertAfter theError
.InsertParagraphAfter
.InsertParagraphAfter
theError = ""
last11chars = ""
endfound = False
Exit Do
End With
Else
theError = theError + ActiveDocument.Characters(i)
End If
i = i + 1
Loop
End If
Next i

'End With


ActiveDocument.Close (False)


End Function


mbaird said:
Are you trying to get the value of a particular node(element) in the XML file?

<Root>
<Node>Value</Node>
</Root>

Mark Baird

Broads said:
Hi I am, looking to find a solution to a problem.

I am looking to scan through XML code for a keyword and then reading the
text following this to the next ">" character.. in theory my code should
work. But I do not know word vba. I think the key point I am looking for is
the character position where the String is found... Check the following code
and please make suggestions,, evan if my approach is completely wrong......

Code:
Function openSearch(myfile)
Dim searchString As String
Dim curentchar As String
searchString = frmIndexGen.txtSearchString

Dim i As Integer
Dim endfound As Boolean
Dim theError As String


Documents.Open FileName:=myfile


With ActiveDocument.Content.Find
Do While .Execute(FindText:=searchString, Forward:=True,
Format:=True) = True
i = 1 ' Should be the char after the string I am looking for.
For i = 1 To ActiveDocument.Characters.Count - 1
If ActiveDocument.Characters(i) = ">" Then
endfound = True: Exit For
Else
theError = theError + ActiveDocument.Characters(i)
End If
Next i

With Documents(2).Content
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.InsertAfter myfile
.Select
.InsertParagraphAfter
.InsertAfter theError
theError = ""
End With
Loop

End With


ActiveDocument.Close (False)


End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top