Author Topic: setenv in VBA?  (Read 3403 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
setenv in VBA?
« 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
Bernd

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
setenv in VBA?
« Reply #1 on: July 05, 2005, 08:40:37 AM »
Something like :- ?
ThisDrawing.SetVariable "MAXSORT", 100
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

SMadsen

  • Guest
setenv in VBA?
« Reply #2 on: July 05, 2005, 11:10:24 AM »
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.)

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
setenv in VBA?
« Reply #3 on: July 05, 2005, 02:26:02 PM »
To explain what I meant:

In a lisp program I wrote I could set with

Code: [Select]
(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
Bernd

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
setenv in VBA?
« Reply #4 on: July 05, 2005, 02:44:24 PM »
SMadsen,

Thank you for the right advice,

Code: [Select]

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
Bernd

SMadsen

  • Guest
setenv in VBA?
« Reply #5 on: July 05, 2005, 04:36:52 PM »
Of course, you could always use SendCommand and still use SETENV :)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
setenv in VBA?
« Reply #6 on: July 05, 2005, 05:07:14 PM »
Quote from: SMadsen
Of course, you could always use SendCommand and still use SETENV :)


but that is just so ... you know .. UGLY
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Jeff_M

  • King Gator
  • Posts: 3946
  • C3D user & customizer
setenv in VBA?
« Reply #7 on: July 05, 2005, 06:14:19 PM »
You could also use the VBA functions GetSetting & SaveSetting

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
setenv in VBA?
« Reply #8 on: July 05, 2005, 06:25:48 PM »
Not for environment settings, but for program settings you can ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal