TheSwamp
Code Red => VB(A) => Topic started by: Amsterdammed on July 05, 2005, 07:55:23 AM
-
Hello
in Vlisp i can set a variable on the PC with setenv.
Does VBA know something like this, too?
Thanks in advance,
Bernd
-
Something like :- ?
ThisDrawing.SetVariable "MAXSORT", 100
-
Bernd,
In the Preferences objects, there are many properties that refer to environment variables maintained by AutoCAD.
If you mean system environment variables, I can only think of API calls such as SetEnvironmentVariable. It's not a simple task and I'd recommend a search on the net (including the word "VBA" to sift out any references for C etc.)
-
To explain what I meant:
In a lisp program I wrote I could set with
(Setenv "myvar" " myvalue")
what ever I needed for future use.
I could for example check if a user is using a version of one of my programs for the first time, and launch a help file to read first.
Something like this I search for in VBA.
Thanks, Bernd
-
SMadsen,
Thank you for the right advice,
Option Explicit
Private Declare Function SHSetValue Lib "SHLWAPI.DLL" Alias "SHSetValueA" (ByVal hKey As Long, ByVal pszSubKey As String, ByVal pszValue As String, ByVal dwType As Long, pvData As String, ByVal cbData As Long) As Long
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
'Purpose : Updates the specified environment variable.
'Inputs : sSettingName The name of the environment variable.
' sSettingValue The new value of the environment variable.
' [bSystemEnvironment] If true sets a system environment variable,
' else sets local user environment variable.
'Outputs : N/A
'Author : Andrew Baker
'Date : 9/Jul/2001
'Notes :
Public Sub EnvironmentSet(sSettingName As String, sSettingValue As String, Optional bSystemEnvironment As Boolean = False)
Dim lRet As Long
Const REG_EXPAND_SZ = 2, HWND_BROADCAST = &HFFFF&, WM_WININICHANGE = &H1A
Const HKEY_CURRENT_USER = &H80000001, REG_SZ = 1
Const SHREGSET_FORCE_HKCU = &H1
Const SMTO_ABORTIFHUNG = &H2
Const HKEY_LOCAL_MACHINE = &H80000002
'Set the environment variable for the current process
SetEnvironmentVariable sSettingName, sSettingValue
If bSystemEnvironment = False Then
'Set the local environment variable for all other processes (via registry)
lRet = SHSetValue(HKEY_CURRENT_USER, "Environment", sSettingName, REG_EXPAND_SZ, ByVal CStr(sSettingValue), CLng(LenB(StrConv(sSettingValue, vbFromUnicode)) + 1))
Else
'Set the system environment variable for all other processes (via registry)
lRet = SHSetValue(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Control\Session Manager\Environment", sSettingName, REG_EXPAND_SZ, ByVal CStr(sSettingValue), CLng(LenB(StrConv(sSettingValue, vbFromUnicode)) + 1))
End If
'Send the environment update message (with a 5 sec timeout)
Call SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0, "Environment", SMTO_ABORTIFHUNG, 5000, lRet)
End Sub
'Purpose : Replace function for the VB Environ() function. Returns the CURRENT value of
' an environment variable.
'Inputs : sSettingName The name of the environment setting to return the value of.
' [sDefaultValue] The default value to return.
'Outputs : Returns the value of the environment variable, else returns the default value
' if the value was not found.
'Author : Andrew Baker
'Date : 9/Jul/2001
'Notes :
Function EnvironmentGet(sSettingName As String, Optional sDefaultValue As String = "") As String
Dim lEndPos As Long
Dim sBuffer As String * 512
lEndPos = GetEnvironmentVariable(sSettingName, sBuffer, Len(sBuffer))
If lEndPos > 0 Then
'Setting found, return it's value
EnvironmentGet = Left$(sBuffer, lEndPos)
Else
'Return the default value
EnvironmentGet = sDefaultValue
End If
End Function
'Demonstration routine
'NOTE VB does not reload the Environment variables if you use the
'"Environ" function to return their values. If you change any values, you
'must use the EnvironmentGet function to return the updated values
Sub test()
Call EnvironmentSet("Andrew", "Is Ace (" & Now & ")")
MsgBox "The value for the environment variable Andrew : " & EnvironmentGet("Andrew")
End Sub
I found on the web.
Thanks,
:D Bernd
-
Of course, you could always use SendCommand and still use SETENV :)
-
Of course, you could always use SendCommand and still use SETENV :)
but that is just so ... you know .. UGLY
-
You could also use the VBA functions GetSetting & SaveSetting
-
Not for environment settings, but for program settings you can ...