Replace text in XML file if another string is found?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
303
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have the following code courtesy of @JEC (many thanks)
VBA Code:
Sub jec()
Dim it

With CreateObject("scripting.filesystemobject")

' specify source folder, file extension and what text needs to be replaced
For Each it In .GetFolder("W:\COYA\interfaces\MIPLoader\failed\").Files
      If .GetExtensionName(it) = "XML" Then .CreateTextFile(it).Write Replace(.OpenTextFile(it).ReadAll, "<TaxCode>VIZERO</TaxCode>", "<TaxCode>VISTD</TaxCode>")
Next
End With

End Sub

Which works perfectly and does a simple find/replace.

Can this be enhanced so that if these strings (not necessarily adjacent in the file)
Code:
<DocumentMasterCode>PINVCAPX</DocumentMasterCode>
<InputTemplateMasterCode>PINVMAT</InputTemplateMasterCode>

are found then the second string needs to say
Code:
<InputTemplateMasterCode>PINVCAPX</InputTemplateMasterCode>

Thanks in advance as always.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi again!

For now the quick adjustment:

VBA Code:
Sub jec()
 Dim it, x, y
 With CreateObject("scripting.filesystemobject")
   For Each it In .GetFolder("W:\COYA\interfaces\MIPLoader\failed\").Files
      If .GetExtensionName(it) = "XML" Then
        x = "<DocumentMasterCode>PINVCAPX</DocumentMasterCode>" & vbLf & "<InputTemplateMasterCode>PINVMAT</InputTemplateMasterCode>"
        y = Replace(x, "PINVMAT", "PINVCAPX")
        .CreateTextFile(it).Write Replace(Replace(.OpenTextFile(it).ReadAll, "<TaxCode>VIZERO</TaxCode>", "<TaxCode>VISTD</TaxCode>"), x, y)
      End If
   Next
 End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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