1

I'm decided to try my way around VBA, got a little script running to export a XML map I have in a worksheet to XML, works fine so far, but when that XML file is already generated, it returns a error instead of replace it... I tried many ways I've seen in other topics of this forum and around web but because I don't know VBA protocols it's taking me (a lot) of time, can anyone help?

What I have so far:

    Sub Export_Main_XML()

Dim JobNumber As String
JobNumber = Sheet12.Range("A4").Text
XMLName = ThisWorkbook.Path & "\" & JobNumber & "_Main_Export.xml"
    ActiveWorkbook.XmlMaps("Main_XML_Map").Export URL:=XMLName
End Sub

Ideally what I'm looking for is:

Export

-if not file existent, just export (script above)

-if file on same path and name exists > ask to replace "yes or no"

-if yes, replace

-if no, exit

Help please, Thanks!

1 Answer 1

1

Look for an existing file, ask to overwrite if it exists - if that's the choice pass true to .Export to indicate your intention to replace it:

JobNumber = Sheet12.Range("A4").Text
XMLName = ThisWorkbook.Path & "\" & JobNumber & "_Main_Export.xml"

'' does it exist already?
If Dir$(XMLName) <> "" Then
    '' exists
    If MsgBox("Overwrite " & XMLName & " ?", vbYesNo, "Confirm Overwrite") = vbNo Then Exit Sub
End If

'' pass true to overwrite
ActiveWorkbook.XmlMaps("Main_XML_Map").Export XMLName, True
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks Alex, I've been trying it for the last 5 hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.