我使用Microsoft评估和规划工具包(MAP Toolkit)执行扫描。 尽管MAP工具包生成的通用报告很有用,但Office报告却非常混乱。 在尝试了几种不同的方法来处理实际报告之后,我发现我必须直接从数据库中获取数据。 不幸的是,我不仅是VBA的初学者,我对SQL语句还没有真正的线索。 下面的代码是Excel的宏录制功能的结果,很多人在这里阅读@stackoverflow,我的朋友谷歌,大量的反复试验和几个小时的尝试来解决它。
我必须承认,当我能够将查询成功导入我的工作簿时,我为自己感到骄傲。 不幸的是,我没有通过“WHERE Name IN”部分限制结果的结果。
因此,我很乐意在三点上请求你的帮助:
Sub Office_Version()
Dim sourcedb As String
Dim xConnect As Object
Dim strFormulaTemplate_HW As String
Dim strFormula_HW As String
Dim strFormulaTemplate_SW As String
Dim strFormula_SW As String
'this part seems to not work at all...
For Each xConnect In ActiveWorkbook.Connections
If xConnect.Name <> "ThisWorkbookDataModel" Then xConnect.Delete
Next xConnect
'this part is working fine
sourcedb = InputBox("Name source database")
strFormulaTemplate_HW = "let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""(LocalDB)\MAPToolKit"", ""@@DBName@@""), " & Chr(13) & "" & Chr(10) & " AllDevices_Assessment_HardwareInventoryView = Source{[Schema=""AllDevices_Assessment"",Item=""HardwareInventoryView""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " AllDevices_Assessment_HardwareInventoryView"
strFormulaTemplate_SW = "let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""(LocalDB)\MAPToolKit"", ""@@DBName@@""), " & Chr(13) & "" & Chr(10) & " Office_Reporting_OfficeProductsView = Source{[Schema=""Office_Reporting"",Item=""OfficeProductsView""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Office_Reporting_OfficeProductsView"
strFormula_HW = Replace(strFormulaTemplate_HW, "@@DBName@@", sourcedb)
strFormula_SW = Replace(strFormulaTemplate_SW, "@@DBName@@", sourcedb)
ActiveWorkbook.Queries.Add Name:="HardwareInventoryView", Formula:=strFormula_HW
ActiveWorkbook.Queries.Add Name:="OfficeProductsView", Formula:=strFormula_SW
ActiveWorkbook.Queries.Add Name:="Office_Report", _
Formula:="let" & Chr(13) & "" & Chr(10) & " Source = Table.NestedJoin(#""OfficeProductsView"",{""DeviceNumber""},#""HardwareInventoryView"",{""DeviceNumber""},""HardwareInventoryView"",JoinKind.LeftOuter)," & Chr(13) & "" & Chr(10) & " #""Expanded HardwareInventoryView"" = Table.ExpandTableColumn(Source, ""HardwareInvento" & _
"ryView"", {""ComputerName""}, {""HardwareInventoryView.ComputerName""})," & Chr(13) & "" & Chr(10) & " #""Removed Columns"" = Table.RemoveColumns(#""Expanded HardwareInventoryView"",{""DeviceNumber"", ""OfficeVersion"", ""IdentifyingNumber"", ""IsSuite"", ""VersionNumber""})," & Chr(13) & "" & Chr(10) & " #""Reordered Columns"" = Table.ReorderColumns(#""Removed Columns""," & _
"{""HardwareInventoryView.ComputerName"", ""Name""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Reordered Columns"""
'this part is only working w/o the (..) Where Name in ('..') part
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Office_Report;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT HardwareInventoryView.ComputerName,Name " & _
"FROM [Office_Report] WHERE Name IN ('Microsoft Office Professional Plus 2010', 'Microsoft Office Professional Plus 2013', 'Microsoft Office Professional Plus 2016')")
.PreserveFormatting = True
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Office_Report"
End With
Range("A1").Select
End Sub