Here's what I've tried:
API Call:
Shell Command:
I don't get any errors from the API call, but the Path doesn't appear to change either. Maybe in another workspace or something? Anyway, I need the real system path changed. Not one in Never Never Land.
The Shell command gives me an automation error 80020009.
So I found this on the Microsoft site:
//
// MessageId: RPC_E_SERVER_DIED_DNE
//
// MessageText:
//
// The callee (server [not server application]) is not available
// and disappeared; all connections are invalid. The call did not
// execute.
//
#define RPC_E_SERVER_DIED_DNE _HRESULT_TYPEDEF_(0x80010012L)
Since I'm computer illiterate, can someone explain that to me.
I also found this nice code snippet on the Microsoft site. It will make my life a lot easier, I think. Maybe it will help someone else.
API Call:
Code:
Public Function WriteEnvironmentVariable(strEnvVarName As String, _
strEnvVarValue As String) _
As Boolean
Dim lngAPICallRetVal As Long
On Error GoTo WriteEnvironmentVariable_Error
Dim strErrorMessage As String
Dim lngErrReturn As Long
lngAPICallRetVal = SetEnvironmentVariable(strEnvVarName, strEnvVarValue)
If lngAPICallRetVal <> 0 Then
WriteEnvironmentVariable = True
Else
Call MsgBox("There was an error while writing the value.", _
vbExclamation Or vbSystemModal, "Unable To Write Value")
End If
'On Error GoTo 0
Exit Function
WriteEnvironmentVariable_Error:
strErrorMessage = "Error " & Err.Number & _
" (" & Err.Description & _
") in procedure WriteEnvironmentVariable" & _
" of Module mdlProcs"
lngErrReturn = MsgBox(strErrorMessage, _
vbOKOnly + vbCritical, _
"Error!")
End Function
Code:
'---------------------------------------------------------------------------------------
' Procedure : WriteEnvironmentVariableII
' Author : rstkw
' Date : 03/23/14
' Purpose :
'
' ***** Requires a reference to Windows Script Host Object Model
'---------------------------------------------------------------------------------------
Function WriteEnvironmentVariableII(strEnvVarName As String, _
strEnvVarValue As String) As Boolean
Dim objShell As wshShell
Dim lngReturnCode As Long
Dim strShellCommand As String
On Error GoTo WriteEnvironmentVariableII_Error
Dim strErrorMessage As String
Dim lngErrReturn As Long
Set objShell = New wshShell
strShellCommand = " /c Set " & strEnvVarValue
'//// MsgBox "strShell Command: " & strShellCommand
lngReturnCode = objShell.Run(strShellCommand, vbNormalFocus, vbTrue)
'On Error GoTo 0
Exit Function
WriteEnvironmentVariableII_Error:
strErrorMessage = "Error " & Err.Number & _
" (" & Err.Description & _
") in procedure WriteEnvironmentVariableII" & _
" of Module mdlProcs"
lngErrReturn = MsgBox(strErrorMessage, _
vbOKOnly + vbCritical, _
"Error!")
End Function
The Shell command gives me an automation error 80020009.
So I found this on the Microsoft site:
//
// MessageId: RPC_E_SERVER_DIED_DNE
//
// MessageText:
//
// The callee (server [not server application]) is not available
// and disappeared; all connections are invalid. The call did not
// execute.
//
#define RPC_E_SERVER_DIED_DNE _HRESULT_TYPEDEF_(0x80010012L)
Since I'm computer illiterate, can someone explain that to me.
I also found this nice code snippet on the Microsoft site. It will make my life a lot easier, I think. Maybe it will help someone else.
Code:
Option Explicit
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Declare Function FormatMessage Lib "kernel32" Alias _
"FormatMessageA" ( ByVal dwFlags As Long, lpSource As Long, _
ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Any) _
As Long
Private Function MessageText(lCode As Long) As String
Dim sRtrnCode As String
Dim lRet As Long
sRtrnCode = Space$(256)
lRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0&, lCode, 0&, _
sRtrnCode, 256&, 0&)
If lRet >0 Then
MessageText = Left(sRtrnCode, lRet)
Else
MessageText = "Error not found."
End If
End Function