Delete Power Query queries' custom XML data using VBA

末鹿安然 提交于 2019-12-13 02:01:42

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/28626183/delete-power-query-queries-custom-xml-data-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!