I am debugging some VBA code I've written in Excel 2016, and this sub is crashing Excel 2016 on windows Server with no errors.
It is crashing on the Set RegObj = GetObject...
Sub TestPrinter()
On Error GoTo e
Dim RegObj As Object
'This next line is where the crash occurs...
Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Exit Sub
e:
MsgBox "Error number " & Err & " in TestPrinter" & vbCrLf & "Error: " & Error$(Err)
End Sub
My end goal is to enumerate the printers connected on the machine, and then set Application.ActivePrinter
based on the string I pull out of the registry. This code is working fine on every other machine I've tried it on - but fails on this one server.
How can I go about debugging this? The error handler is never hit.
SWbemSecurity
orwinmgmts
to accessimpersonationLevel
? I have only seen the prior, so grain of salt. I can only imagine you're interfacing with the service host and causing a necessary process to die. – InwardSWbemSecurity.ImpersonationLevel property
. You essentially use the code designated as "You can also specify impersonation levels as part of a moniker. The following example sets the authentication level and the impersonation level, and retrieves an instance of Win32_Service", which may explain the issue as you're not just reading. Again, I have only seen this done, not written the code myself, whereas the object being modified is a security object. I apologize that I cannot add more. – Inward