compile error User-defined type not defined at "oShell As WshShell"
Asked Answered
B

1

0

I am currently using MS Access 2016 database. I get this compile error below

"compile error User-defined type not defined" at the code where it is Dim oShell As WshShell

Below is the program. I dont understand where the problem is. Can anyone help me.

Sub SetWebControlAsIE9()
    Dim sKey As String
    Dim oShell As WshShell
    Dim vKey As Variant
    Dim nErr As Long
    Dim bNewStart As Boolean
    
    Set oShell = New WshShell
    
    sKey = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\msaccess.exe"
    On Error Resume Next
    vKey = oShell.RegRead(sKey)
    nErr = Err.Number
    On Error GoTo 0
    If nErr = -2147024894 Then
        oShell.RegWrite sKey, 9999, "REG_DWORD"
        Debug.Print "IE9 Mode set"
        bNewStart = True
    Else
        If vKey <> 9999 Then
            oShell.RegWrite sKey, 9999, "REG_DWORD"
            Debug.Print "IE9 Mode set"
            bNewStart = True
        Else
            Debug.Print "IE9 Mode is already set"
        End If
    End If
    Set oShell = Nothing
    
    If bNewStart Then
        RestartAccess
    End If
    
End Sub
Sub RestartAccess()
    If MsgBox("Die Datenbank und Access müssen neu gestartet werden," & vbCrLf & _
        "weil die Einstellungen für das WebControl geändert wurden." & vbCrLf & _
        "Jetzt neu starten?", vbQuestion Or vbYesNo, "Bestätigen:") = vbYes Then
        
        Dim sExec As String
        Dim oShell As Object
        
        sExec = Chr$(34) & SysCmd(acSysCmdAccessDir) & "msaccess.exe" & Chr$(34) & _
                " " & Chr$(34) & CurrentDb.Name & Chr$(34)
        oShell.Exec sExec
        Application.Quit acQuitSaveNone
    End If
End Sub
Blouin answered 26/6, 2020 at 12:6 Comment(0)
B
0

If you want to use late binding instead (not adding the reference) then you can change this

Dim oShell As WshShell

to this

Dim oShell As Object

and then change this

Set oShell = New WshShell

to this

Set oShell = CreateObject("WScript.Shell")
Bergh answered 26/6, 2020 at 12:11 Comment(4)
Hi, Thanks a lot. I think it works but I get the error now on a different part of the program. I just added the Sub RestartAccess part of program to the above my question. Do I have to make the same changes to this part of the script? on "oShell.Exec sExec" line the debugger highlights.Blouin
In your second routine, you have the first part that Dims the object, but you dont have the second line that Sets the object. You have to have both.Bergh
I didn't get that exactly, sorry I am a beginner to VBA.Blouin
What didnt you get? Your second routine needs this line too - Set oShell = CreateObject("WScript.Shell") - Put it after the Dim line.Bergh

© 2022 - 2024 — McMap. All rights reserved.