1
votes

I have Power Query queries in an Excel 2010 file and would like to delete these queries using VBA. so far I can achieve that manually with the following steps:

  1. unload Power Query addin
  2. run Document Inspector and click to delete Custom XML data

The Macro recorder record the following code for step 2.

Sub Makro1()
'
' Makro1 Makro
'
'
ActiveWorkbook.RemoveDocumentInformation (xlRDIPrinterPath)
ActiveWorkbook.RemoveDocumentInformation (xlRDIDocumentProperties)
ActiveWorkbook.RemoveDocumentInformation (xlRDIInactiveDataConnections)
End Sub

However, using this macro I do not achieve the same result (=PQ query code gone from the workbook).

Any ideas on how to achieve the PQ query codes to be removed using VBA?

2

2 Answers

0
votes

This code will delete the custom part, but you will have Power Query connections, and possibly TableObjects, and DataModels left in your workbook. Trying to refresh will give you errors.

Sub DeletePowerQueryCustomXml()
  Set parts = ActiveWorkbook.CustomXMLParts.SelectByNamespace("http://schemas.microsoft.com/DataMashup")
  For Each part In parts
    part.Delete
  Next part
End Sub
0
votes

For Excel 2016 preview, there are new VBA objects which enable us to better handle PQ programmatically. E.g. check out Sub DeleteQuery() at: https://gallery.technet.microsoft.com/office/VBA-to-automate-Power-956a52d1

However for those of us stuck on Office 2010, this doesn't yet seem possible.