Removing/replacing XML in multiple files

shawnm

New Member
Joined
Mar 15, 2012
Messages
3
I have a subroutine that looks in multiple XML files and tries to remove an XML attribute:

Code:
Sub ReplaceStringInFile()
    Dim d As MSXML2.DOMDocument60
    Dim i As MSXML2.IXMLDOMNodeList
    Dim n As MSXML2.IXMLDOMNode
    Set d = New MSXML2.DOMDocument60
    Const sSearchString As String = "C:\Users\Shawn\Desktop\GD test\*.xml"
    Dim sBuf As String
    Dim sTemp As String
    Dim iFileNum As Integer
    Dim sFileName As String
    Dim sFilePath As String
    sFileName = Dir(sSearchString)
    Do While sFileName <> ""
        sFilePath = "C:\Users\Shawn\Desktop\GD test\" & sFileName  'Get full path to file
        iFileNum = FreeFile
        sTemp = ""  'Clear sTemp
        Open sFilePath For Input As iFileNum
        d.Load sFilePath
        Set i = d.SelectNodes("//@pattern")
            For Each n In i
            Do Until EOF(iFileNum)
                Line Input #iFileNum, sBuf
                sTemp = sTemp & sBuf & vbCrLf
            Loop
        Debug.Print n.Text
        Next n
        Close iFileNum
        sTemp = Replace(sTemp, n.Text, "")
        iFileNum = FreeFile
        Open sFilePath For Output As iFileNum
        Print #iFileNum, sTemp
        Close iFileNum
        sFileName = Dir() 'Get the next file
    Loop
End Sub
I am getting the following error in the following line:
Code:
sTemp = Replace(sTemp, n.Text, "")
Runtime error ‘91’:
Object variable or With block variable not set.
I’m unsure exactly what I am doing wrong.
Here’s a sample of the XML file:

HTML:
<?xml version="1.0"?>
<report useStyleVersion="1" interactivePageBreakByFrame="true" ignoreFilterContext="false" expressionLocale="fr-fr" xmlns="http://developer.cognos.com/schemas/report/8.0/">
<!--RSU-SPC-0093 La spécification de rapport a été mise à niveau de {originalNS} à {destNS} le 5-12-2012. 8:22:16-->
<modelPath>/content/package[@name='LG - RECEVABLES.CAT']/model[last()]</modelPath>
<listColumn>
<listColumnBody>
<contents>
<style>
<CSS value="border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;font-family:'Arial';font-weight:normal;font-size:6pt;text-align:center;vertical-align:top;white-space:nowrap"/>
<dataFormat>
<numberFormat pattern="####/##/##;-####/##/##"/>
</dataFormat>
</style>
</contents>
<listColumnRowSpan refDataItem="No de facture"/>
</listColumnBody>
</listColumn>
<contents>
<textItem>
<dataSource>
<dataItemValue refDataItem="Total (Montant de la facture (Can$)) No.1"/>
</dataSource>
<style>
<dataFormat>
<numberFormat pattern="$#,##0.00;-$#,##0.00"/>
</dataFormat>
</style>
</textItem>
</contents>
<reportName>LEG - Age des comptes à recevoir - Financier avec GL.imr</reportName>
</report>

My ultimate objective is to complete remove all references to the following pattern attribute including its values:
Example before :
Example after :

Any help in pinpointing the issue in my code would be much appreciated!
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It looks like this line: sTemp = Replace(sTemp, n.Text, "")
should be before that one: Next n
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top