Hello,
I have written VBA code for Excel and Word that accesses the Windows update history via the WUAPI. This code works fine in all Windows version prior to Windows 10. In W-10, when I access the ResultCode property of the IUpdateHistoryEntry object, I get the following error:
"Run-time error '-2145120257 (80240fff)': Method 'ResultCode' of object 'IUpdateHistoryEntry2' failed"
Curiously, it does actually return the correct value (2 = Succeeded) in spite of the error condition. Does that error actually mean anything? If so, what is it telling me and how do I fix whatever is causing it, without just suppressing and ignoring it?
Here's a demonstration version of my code. Just run the Demo_IUpdateHistoryEntry_Bug routine:
Option Explicit 'NOTE: For early binding, requires that the "WUAPI X.Y Type Library" reference be set. Private Type UpdateHistData ClientApplicationID As String DateTime_UTC As Date 'UTC = "Coordinated Universal Time" (formerly GMT). To convert to local time, 'see the B_GenUtils module's function DateTimeUtcToLocal or Chip Pearson's 'GMTTimeToLocalTimeSerial function. Description As String ResultCode As Integer SupportURL As String Title As String UpdateID As String UpdateRevisionNumber As Long End Type Private Enum OperationResultCode orcNotStarted = 0 orcInProgress = 1 orcSucceeded = 2 orcSucceededWithErrors = 3 orcFailed = 4 orcAborted = 5 End Enum Public Sub Demo_IUpdateHistoryEntry_Bug() ' 'Prints the date and title of the latest updates to the Immediate window. ' 'To demonstrate the Windows-10 bug, comment out the "On Error Resume Next" line in the GetUpdateInfo function, 'below (marked with "***") and then run this routine under Windows 10. ' '************************************************************************************************************* Dim i As Long Dim LatestUpdateDT As Date Dim UpdateList() As UpdateHistData UpdateList = GetUpdateInfo() 'Deterimine the latest update date For i = LBound(UpdateList) To UBound(UpdateList) If UpdateList(i).ResultCode >= orcSucceeded Then 'Is a currently installed update, so check its date If UpdateList(i).DateTime_UTC > LatestUpdateDT Then LatestUpdateDT = UpdateList(i).DateTime_UTC End If Next i 'Print all updates that are current For i = LBound(UpdateList) To UBound(UpdateList) If UpdateList(i).DateTime_UTC >= LatestUpdateDT Then Debug.Print UpdateList(i).DateTime_UTC & ": " & UpdateList(i).Title & _
", Result = " & UpdateList(i).ResultCode
End If Next i End Sub Private Function GetUpdateInfo() As UpdateHistData() ' 'Returns an array of update-history elements that are likely to be useful, as defined by the UpdateHistData 'data-type (defined above), for all update-history records. ' '************************************************************************************************************* ' 'Late binding: ' Dim EntryObj As Object ' Dim HistoryColl As Variant ' Dim SessionObj As Object ' Dim SearcherObj As Object 'Early binding requires that the "WUAPI X.Y Type Library" reference be set: Dim EntryObj As IUpdateHistoryEntry Dim HistoryColl As IUpdateHistoryEntryCollection Dim SessionObj As IUpdateSession Dim SearcherObj As IUpdateSearcher Dim HistoryCount As Integer Dim i As Long Set SessionObj = CreateObject("Microsoft.Update.Session") Set SearcherObj = SessionObj.CreateUpdateSearcher HistoryCount = SearcherObj.GetTotalHistoryCount Set HistoryColl = SearcherObj.QueryHistory(1, HistoryCount) ReDim TempArray(1 To HistoryColl.Count) As UpdateHistData For Each EntryObj In HistoryColl i = i + 1 TempArray(i).ClientApplicationID = EntryObj.ClientApplicationID TempArray(i).DateTime_UTC = EntryObj.Date 'UTC = "Coordinated Universal Time" (formerly GMT). To 'convert to local time, see the B_GenUtils module's function 'DateTimeUtcToLocal or Chip Pearson's GMTTimeToLocalTimeSerial 'function. TempArray(i).Description = EntryObj.Description 'WINDOWS-10 BUG: Windows 10 has an apparent bug in which the IUpdateHistoryEntry object can return a valid 'ResultCode value of "Succeeded" (2) but it also throws the error ' ' "Run-time error '-2145120257 (80240fff)': Method 'ResultCode' of object 'IUpdateHistoryEntry2' failed" ' 'for no apparent reason. Furthermore, note that it doesn't return result code "SucceededWithErrors" (3) in 'such cases. So ignore all such errors: ' On Error Resume Next '*** To demonstrate the bug, comment-out this line and run under Windows 10 TempArray(i).ResultCode = EntryObj.ResultCode On Error GoTo 0 TempArray(i).SupportURL = EntryObj.SupportURL TempArray(i).Title = EntryObj.Title TempArray(i).UpdateID = EntryObj.UpdateIdentity.UpdateID TempArray(i).UpdateRevisionNumber = EntryObj.UpdateIdentity.RevisionNumber Next GetUpdateInfo = TempArray() End Function
Thanks in advance for your help