VBA to open, modify and save XML file

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

From a workbook I wish to open an XML file, modify it then save it with a change of name. I've got the following code so far, but it's failing on the SaveAsXMLData line in red with error message "Argument not optional" From what I can tell, I'm missing a schema map or haven't specified one.

My understanding of XML file types isn't great (if someone can suggest a good link, I'd appreciate that) but for now at least, please can someone suggest how to correct my code so far:
Rich (BB code):
Sub ImportXMLFile()
     
Dim ThisWbk As Workbook:       Set ThisWbk = ActiveWorkbook

Dim FilePath As String:            FilePath = ThisWbk.Sheets(1).Range("FilePath")
Dim FileName As String:           FileName = "TimeTestingReport.xml"
Dim TradeTimeCol As String:     TradeTimeCol = "AC"

Dim BST_Time As String, j As String
Dim i As Long
Dim FileOpen As Date
     
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With
    
Workbooks.OpenXML FileName:=FilePath & FileName, LoadOption:=xlXmlLoadOpenXml
FileOpen = Time
With ThisWbk.Sheets(1).Range("FileOpen")
    .Value = FileOpen
    .Offset(, 2) = FileName
End With

i = Range(TradeTimeCol & Rows.Count).End(xlUp).Row

Do
    BST_Time = CStr(Val(Left(Range(TradeTimeCol & i), 2)) + 1)
    If Len(BST_Time) < 2 Then BST_Time = "0" & BST_Time
    Range(TradeTimeCol & i) = BST_Time & Right(Range(TradeTimeCol & i), Len(Range(TradeTimeCol & i)) - 2)
    i = i - 1    
Loop Until i = 1

FileName = Left(FileName, Len(FileName) - 4) & " BST Modified.xml"
FileOpen = Time
ActiveWorkbook.SaveAsXMLData FileName:=FileName

With ThisWbk.Sheets(1).Range("FileSave")
    .Value = FileOpen
    .Offset(, 2) = FileName
End With

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

Set ThisWbk = Nothing

End Sub
I'd also like code included that closes the modified XML file as well please.

Thank you in advance,
Jack
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi Segrio, apologies for not replying, this got pushed down in priorities and now need to review again. Thank you for suggesting the website, reading it now.
Best,
Jack
 
Upvote 0

Forum statistics

Threads
1,207,257
Messages
6,077,337
Members
446,278
Latest member
hoangquan2310

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