word macro ends with error53 kernel32 not found

von Fintel, Jörg 0 Reputation points
2026-10-15T09:30:03.1333333+00:00

running a word macro using

Private Declare Function SetEnv Lib "kernel32" _

    Alias "SetEnvironmentVariableA" (ByVal chName As String, _

       ByVal chValue As String) _

    As Long

and

ret = SetEnv("PATH", chPath$)

ends with error 53: file not found: kernel64

Microsoft 365 and Office | Word | For business | Windows

5 answers

Sort by: Newest
  1. Jay Freedman 208.4K Reputation points Volunteer Moderator
    2026-10-16T12:16:19.2033333+00:00

    This is just a coding error:

    #If VBA7 And Win64 Then Private Declare PtrSafe Function SetEnv Lib "kernel64" _ <====== I don't know why

    Instead it should be

    #If VBA7 And Win64 Then
    Private Declare PtrSafe Function SetEnv Lib "kernel32" _   
    

    Windows will take care of extracting the correct (64-bit) code from the kernel32.dll file.

    Was this answer helpful?

    0 comments No comments

  2. von Fintel, Jörg 0 Reputation points
    2026-10-16T10:44:06.7966667+00:00

    Microsoft® Word für Microsoft 365 MSO (Version 2608 Build 16.0.20326.20072) 64 Bit

    coding

    Option Explicit

    #If VBA7 And Win64 Then

    Private Declare PtrSafe Function SetEnv Lib "kernel64" _

        Alias "SetEnvironmentVariableA" (ByVal chName As String, _
    
           ByVal chValue As String) _
    
        As Long
    

    #Else

    Private Declare Function SetEnv Lib "kernel32" _

        Alias "SetEnvironmentVariableA" (ByVal chName As String, _
    
           ByVal chValue As String) _
    
        As Long
    

    #End If

    Public Sub SetPath()

    Dim chPath$

    Dim chWWIPath$

    Dim ret

    ' read PATH variable

    chPath$ = Environ("PATH")

    ' get DLL path from registry

    ' WA051201: use different registry setting

    chWWIPath$ = System.PrivateProfileString("", "HKEY_CLASSES_ROOT\WWIDOCUMENT\InstallDir", "")

    ' chWWIPath$ = WordBasic.[GetPrivateProfileString$] _

    ' ("HKEY_CLASSES_ROOT\WWIDOCUMENT\InstallDir", "", "")

    ' if registry entry is not there use path of wwi.dot (but without historic subdirectory of central installation)

    If chWWIPath$ = "" Then

    chWWIPath$ = ActiveDocument.AttachedTemplate.path
    
    If Right(chWWIPath$, 5) = "Wwi97" Then
    
      chWWIPath = Left(chWWIPath$, Len(chWWIPath$) - 6)
    
    End If
    

    End If

    ' append DLL path if not already there

    If chWWIPath$ <> "" _

     And InStr(chPath$, chWWIPath$ + ";") = 0 _
    
     And Right(chPath$, Len(chWWIPath$)) <> chWWIPath$ Then
    
    chPath$ = chPath$ + ";" + chWWIPath$
    
    ' set PATH variable
    
    ret = SetEnv("PATH", chPath$)                   **<===== Abbruch**
    

    End If

    End Sub

    Was this answer helpful?

    0 comments No comments

  3. von Fintel, Jörg 0 Reputation points
    2026-10-16T10:42:56.0066667+00:00

    Version of Word is

    Microsoft® Word für Microsoft 365 MSO (Version 2608 Build 16.0.20326.20072) 64 Bit

    see the coding of the error:

    Option Explicit

    #If VBA7 And Win64 Then

    Private Declare PtrSafe Function SetEnv Lib "kernel64" _ <====== I don't know why

        Alias "SetEnvironmentVariableA" (ByVal chName As String, _
    
           ByVal chValue As String) _
    
        As Long
    

    #Else

    Private Declare Function SetEnv Lib "kernel32" _

        **Alias "SetEnvironmentVariableA" (ByVal chName As String, _**
    
           **ByVal chValue As String) _**
    
        **As Long**
    

    #End If

    Public Sub SetPath()

    Dim chPath$

    Dim chWWIPath$

    Dim ret

    ' read PATH variable

    chPath$ = Environ("PATH")

    ' get DLL path from registry

    ' WA051201: use different registry setting

    chWWIPath$ = System.PrivateProfileString("", "HKEY_CLASSES_ROOT\WWIDOCUMENT\InstallDir", "")

    ' chWWIPath$ = WordBasic.[GetPrivateProfileString$] _

    ' ("HKEY_CLASSES_ROOT\WWIDOCUMENT\InstallDir", "", "")

    ' if registry entry is not there use path of wwi.dot (but without historic subdirectory of central installation)

    If chWWIPath$ = "" Then

    chWWIPath$ = ActiveDocument.AttachedTemplate.path
    
    If Right(chWWIPath$, 5) = "Wwi97" Then
    
      chWWIPath = Left(chWWIPath$, Len(chWWIPath$) - 6)
    
    End If
    

    End If

    ' append DLL path if not already there

    If chWWIPath$ <> "" _

     And InStr(chPath$, chWWIPath$ + ";") = 0 _
    
     And Right(chPath$, Len(chWWIPath$)) <> chWWIPath$ Then
    
    chPath$ = chPath$ + ";" + chWWIPath$
    
    ' set PATH variable
    
    ret = SetEnv("PATH", chPath$)                    **<===== Abbruch**
    

    End If

    End Sub

    Was this answer helpful?

    0 comments No comments

  4. Senthil kumar 2,665 Reputation points
    2026-10-15T10:41:08.3833333+00:00

    Hi @von Fintel, Jörg

    As per your error message. root cause for compatibility issue. so, first check your word as 64 bit version.

    how to check your version File -> account -> about word.

    suppose your version is not an issue then try below code in vba.

    Dim ret As Long
    ret = SetEnvironmentVariable("PATH", chPath)
    If ret = 0 Then
        MsgBox "Failed to set PATH"
    End If
    
    
    

    Thanks.

    Was this answer helpful?

    0 comments No comments

  5. Liora 2,595 Reputation points Independent Advisor
    2026-10-15T09:53:15.1033333+00:00

    Dear @von Fintel, Jörg,

    I hope you’re having a good day 

    One detail stands out immediately: the declaration you posted references kernel32, but the error message reports:

    Error 53: File not found: kernel64

    Under normal circumstances, Windows API functions such as SetEnvironmentVariableA are provided by kernel32.dll. Windows does not normally use a file named kernel64.dll, which makes the error message particularly interesting.

    Before assuming the issue is with the specific declaration shown, I would recommend checking whether kernel64 appears anywhere else in the VBA project. For example, another module, shared code library, or conditional compilation block may be referencing a different DLL name.

    It would also be helpful to know:

    • Which version of Word you are using.
    • Whether Word is installed as 32-bit or 64-bit.
    • Whether the error occurs exactly when the SetEnv function is called.
    • Whether a search of the VBA project finds any reference to kernel64.

    If you are running 64-bit Office, I would also review the API declaration itself. Legacy VBA code often requires the PtrSafe keyword when used in modern 64-bit Office environments.

    Please understand that our initial response does not always resolve the issue immediately. However, with your help and more detailed information, we can work together to find a solution.   

    Looking forward to hearing back from you with any updates or additional details.  

    Warm regards,  


    If the answer is helpful, please select "Yes, accept answer". If you have any questions or new updates, please feel free to comment below. 

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.