TheSwamp
Code Red => VB(A) => Topic started by: ML on May 17, 2007, 01:40:16 PM
-
I have been creating a VBA Module that will simply set all of our Search paths, file names and file locations (Support Paths).
Just about everything I needed is in The Preferences Collection, however I can not find The Method for setting the Sheet Template File Path Location. Anybody happen to know?
Preferences.Files.???? = "Path"
Thank you
Mark
-
debug.print that path and you will find yadayada;yadayada; ie one long string separated by ;
If you add to that it works fine.
Set Preferences = ThisDrawing.Application.Preferences
SupportPath = Preferences.Files.SupportPath
debug.print SupportPath
-
I think he's looking for this (see image below) in which case, what you posted doesn't show that particular path.
And from what I've seen, there's no way (via straigh VBA) to set that path. I'm sure it's buried somewhere in the registry and that you could modify it to point to your SS template file. Just thinking out loud.
-
Hi Matt,
Yes, thank you, that is exactly what I was talking about and I could not find it anywhere.
AutoCAD seems to be really good these days at adding great new features but failing to make them available in VBA. Do you have any other suggestions about how I could possibly get that path added programmatically?
Thank you
Mark
-
If you know how to change the registry via LSP or VBA, you can modify it that way.
I found the setting at HKCU\Software\Autodesk\AutoCAD\R17.0\ACAD-5106:409\Profiles\ [YOUR PROFILE NAME] \Preferences\AecProject50\Sheet Set Template
That's for ABS 2007.1.
-
No, I did know how to do that Matt :-(
May be I can put a new post up requesting some help with that?
Mark
-
FWIW,
This how I do it via lisp:
(vla-put-pagesetupoverridestemplatefile
(vla-get-files
(vla-get-preferences
(vlax-get-acad-object)
)
)
"c:\\"
)
Or this:
(setenv "AlternativePageSetUpsTemplate" "C:\\")
Ron
-
Ron,
Did you say Page Setup Overide?
Is that referring to The Sheet Template File Location?
If so, I did see that in VBA
Mark
-
Ron that is in VBA however that is for
Default Template for Sheet Creation and Pagesetup Overrides
I need the method for the one above it
Sheet Set Template File Location
Thanks again
Mark
-
Put this in a module (I think it came from Randall Rath)
Option Explicit
'@~~~~~~~~ API Constants for Win32 Reg. ~~~~~~~~~~~@
Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const REG_CREATED_NEW_KEY = &H1
Public Const REG_OPENED_EXISTING_KEY = &H2
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_CREATE_LINK = &H20
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_NO_MORE_ITEMS = 259&
Public Const ERROR_BADKEY = 1010&
Public Const ERROR_CANTOPEN = 1011&
Public Const ERROR_CANTREAD = 1012&
Public Const ERROR_REGISTRY_CORRUPT = 1015&
'@~~~~~~~~~~~~~~~~~ API Types ~~~~~~~~~~~~~~~~@
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'@~~~~~~~~~~~~~~~~~~ The Declares ~~~~~~~~~~~~~~~~~~~~~@
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal lpReserved As Long, lpType As Long, lpData As Any, _
dwSize As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32" _
Alias "RegCreateKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, ByVal Reserved As Long, _
ByVal lpClass As String, ByVal dwOptions As Long, _
ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, _
phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32.dll" _
Alias "RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal dwReserved As Long, _
ByVal dwType As Long, lpValue As Any, ByVal dwSize As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" _
Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" _
Alias "RegDeleteValueA" (ByVal hKey As Long, _
ByVal lpValueName As String) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Public Declare Function RegConnectRegistry Lib "advapi32.dll" _
Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal _
hKey As Long, phkResult As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias _
"RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Public Declare Function RegEnumKey Lib "advapi32.dll" Alias _
"RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _
lpName As String, ByVal cbName As Long) As Long
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias _
"RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _
lpValueName As String, lpcbValueName As Long, lpReserved As Long, _
lpType As Long, lpData As Byte, lpcbData As Long) As Long
Public Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias _
"RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _
lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal _
lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Public Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Public Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" _
(ByVal hKey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter _
As Long, ByVal hEvent As Long, ByVal fAsynchronus As Long) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function OSRegQueryValue Lib "advapi32.dll" _
Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As _
String, ByVal lpValue As String, lpcbValue As Long) As Long
Public Declare Function RegReplaceKey Lib "advapi32.dll" Alias _
"RegReplaceKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal lpNewFile As String, ByVal lpOldFile As String) As Long
Public Declare Function RegRestoreKey Lib "advapi32.dll" Alias _
"RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, _
ByVal dwFlags As Long) As Long
Public Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias _
"RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, _
lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, _
lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, _
lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor _
As Long, lpftLastWriteTime As FILETIME) As Long
'@~~~~~~~~~~~~~~ DeleteRegKey ~~~~~~~~~~~~~~~~~~~~@
' BEWARE! WE CAN'T HELP YOU IF YOU DELETE THE WRONG KEY!
' Always back up your registry before you use any of these
' Methods. In fact, if you don't know EXACTLY what you are
' Doing, stay safe and use the built in VB Registry methods.
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function DeleteRegKey(lngKey As Long, SubKey As String) As Long
Dim lngRet As Long
lngRet = RegDeleteKey(lngKey, SubKey)
DeleteRegKey = lngRet
End Function
'@~~~~~~~~~~~~~~ DeleteRegValue ~~~~~~~~~~~~~~~~~~@
' Delete the value of a key, please use caution.
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function DeleteRegValue(lngKey As Long, SubKey As String, valueName As String) As Long
Dim lngRet As Long
Dim lngKeyRet As Long
lngRet = RegOpenKeyEx(lngKey, SubKey, 0, KEY_WRITE, lngKeyRet)
If lngRet <> ERROR_SUCCESS Then Exit Function
lngRet = RegDeleteValue(lngKeyRet, valueName)
DeleteRegValue = lngRet
RegCloseKey lngKeyRet
End Function
'@~~~~~~~~~~~~~~~~ WriteRegLong ~~~~~~~~~~~~~~~~~~@
' Write a long Data type to a key
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function WriteRegLong(lngKey As Long, SubKey As String, _
DataName As String, dataValue As Long) As Long
Dim SEC As SECURITY_ATTRIBUTES
Dim lngKeyRet As Long
Dim lngDis As Long
Dim lngRet As Long
lngRet = RegCreateKeyEx(lngKey, SubKey, 0, "", REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, SEC, lngKeyRet, lngDis)
If (lngRet = ERROR_SUCCESS) Or (lngRet = REG_CREATED_NEW_KEY) Or _
(lngRet = REG_OPENED_EXISTING_KEY) Then
lngRet = RegSetValueEx(lngKeyRet, DataName, 0&, REG_DWORD, dataValue, 4)
RegCloseKey lngKeyRet
End If
WriteRegLong = lngRet
End Function
Public Function WriteStringValue(lngKey As Long, SubKey As String, _
DataName As String, dataValue As String) As Long
Dim SEC As SECURITY_ATTRIBUTES
Dim lngKeyRet As Long
Dim lngDis As Long
Dim lngRet As Long
lngRet = RegCreateKeyEx(lngKey, _
SubKey, 0, vbNullString, _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
SEC, lngKeyRet, lngDis)
'Trust me on this next line...
If dataValue <= "" Then dataValue = ""
If (lngRet = ERROR_SUCCESS) Or (lngRet = REG_CREATED_NEW_KEY) Or _
(lngRet = REG_OPENED_EXISTING_KEY) Then
lngRet = RegSetValueEx(lngKeyRet, DataName, 0&, _
REG_SZ, ByVal dataValue, Len(dataValue))
RegCloseKey lngKeyRet
End If
WriteStringValue = lngRet
End Function
'@~~~~~~~~~~~~~~~~ ReadRegVal ~~~~~~~~~~~~~~~~~~~~~~~@
' Read a value! see example AppInfo
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ReadRegVal(lngKey As Long, SubKey As String, _
DataName As String, DefaultData As Variant) As Variant
Dim lngKeyRet As Long
Dim lngData As Long
Dim strData As String
Dim DataType As Long
Dim DataSize As Long
Dim lngRet As Long
ReadRegVal = DefaultData
lngRet = RegOpenKeyEx(lngKey, SubKey, 0, KEY_QUERY_VALUE, lngKeyRet)
If lngRet <> ERROR_SUCCESS Then Exit Function
'If you declare a lpData as a string (that's DataName in this function) you
'must pass it ByVal as shown here
lngRet = RegQueryValueEx(lngKeyRet, DataName, 0&, DataType, ByVal 0, DataSize)
If lngRet <> ERROR_SUCCESS Then
RegCloseKey lngKeyRet
Exit Function
End If
Select Case DataType
Case REG_SZ
strData = Space(DataSize + 1)
lngRet = RegQueryValueEx(lngKeyRet, DataName, 0&, DataType, ByVal strData, DataSize)
If lngRet = ERROR_SUCCESS Then
ReadRegVal = CVar(StripNulls(RTrim$(strData)))
End If
Case REG_DWORD
lngRet = RegQueryValueEx(lngKeyRet, DataName, 0&, DataType, lngData, 4)
If lngRet = ERROR_SUCCESS Then
ReadRegVal = CVar(lngData)
End If
End Select
RegCloseKey lngKeyRet
End Function
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
' Reads all of the subkeys under strKey. NOTE VB
' users, you can change this function to return
' a string array!
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function GetSubKeys(strKey As String, SubKey As String, ByRef SubKeyCnt As Long) As String
Dim strValues() As String
Dim strTemp As String
Dim lngSub As Long
Dim intCnt As Integer
Dim lngRet As Long
Dim intKeyCnt As Integer
Dim Ft As FILETIME
lngRet = RegOpenKeyEx(strKey, SubKey, 0, KEY_ENUMERATE_SUB_KEYS, lngSub)
If lngRet <> ERROR_SUCCESS Then
SubKeyCnt = 0
Exit Function
End If
lngRet = RegQueryInfoKey(lngSub, vbNullString, 0, 0, SubKeyCnt, _
65, 0, 0, 0, 0, 0, Ft)
If (lngRet <> ERROR_SUCCESS) Or (SubKeyCnt <= 0) Then
SubKeyCnt = 0
End If
ReDim strValues(SubKeyCnt - 1)
For intCnt = 0 To SubKeyCnt - 1
strValues(intCnt) = String$(65, 0)
RegEnumKeyEx lngSub, intCnt, strValues(intCnt), 65, 0, vbNullString, 0, Ft
strValues(intCnt) = StripNulls(strValues(intCnt))
Next intCnt
RegCloseKey lngSub
For intKeyCnt = LBound(strValues) To UBound(strValues)
strTemp = strTemp & strValues(intKeyCnt) & ","
Next intKeyCnt
GetSubKeys = strTemp
End Function
'@~~~~~~~~~~~~~~StripNulls~~~~~~~~~~~~~~~~~~~~@
' Many API functions have null terminated strings
' this handy function removes the null values
' From the MS KB
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Function StripNulls(ByVal s As String) As String
Dim i As Integer
i = InStr(s, Chr$(0))
If i > 0 Then
StripNulls = Left$(s, i - 1)
Else
StripNulls = s
End If
End Function
'@~~~~~~~~~~~~~~ParseString~~~~~~~~~~~~~~~~~~@
' Use is simple: provide the delimited string, the
' integer that represents the location in the string
' you want to return, and the character that delimits
' the string. Used in this module for VBA Users who
' Need to read the string elements returned by
' "GetSubKeys"
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ParseString(strIn As String, intLoc As Integer, strDelimiter As String) As String
Dim intPos As Integer
Dim intStrt As Integer
Dim intStop As Integer
Dim intCnt As Integer
intCnt = intLoc
Do While intCnt > 0
intStop = intPos
intStrt = InStr(intPos + 1, strIn, Left$(strDelimiter, 1))
If intStrt > 0 Then
intPos = intStrt
intCnt = intCnt - 1
Else
intPos = Len(strIn) + 1
Exit Do
End If
Loop
ParseString = Mid$(strIn, intStop + 1, intPos - intStop - 1)
End Function
'@~~~~~~~~~~~~ Sample of ReadRegVal ~~~~~~~~~~~~@
Public Sub AppInfo()
Dim strVal As String
MsgBox ReadRegVal(HKEY_LOCAL_MACHINE, "SOFTWARE\Autodesk\AutoCAD\R15.0\ACAD-1:409", "ProductName", strVal)
End Sub
This changed the key for me but I don't use sheetsets to test it.
Sub SetSheetSet()
Dim CadStart As String
Dim KeyName As String
Dim valueName As String
Dim DefaultData As String
Dim dataValue As String
'This is my key "HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R17.0\ACAD-5005:409\Profiles\Lex2007\General"
KeyName = "Software\Autodesk\AutoCAD\R17.0\ACAD-5005:409\Profiles\Lex2007\General"
valueName = "SheetSetTemplatePath"
dataValue = "YourTemplatePath"
WriteStringValue HKEY_CURRENT_USER, KeyName, valueName, dataValue
Debug.Print ReadRegVal(HKEY_CURRENT_USER, KeyName, valueName, DefaultData)
End Sub
-
Hey Bryon
I appreciate it
I am looking particurlarly at the last Sub
I tried it but I am getting a Sub or Function not Defined erro on line
WriteStringValue HKEY_CURRENT_USER, KeyName, valueName, dataValue
Is there a function or another Sub routine that is missing?
Thank you
Mark
-
Ml that is included in the part you put in a module (Icall the module "Registry" in my dvb)
You need that and the constants to make it work.
-
PageSetupOverridesTemplateFile property is what you are looking for. Put the code below in a sub and run it. It should print the location of your Sheet Set Template Files in the Immediate window.
Sub Example_PageSetupOverridesTemplateFile()
Dim MyFiles As IAcadPreferencesFiles2
Set MyFiles = AcadApplication.Preferences.Files
Debug.Print MyFiles.PageSetupOverridesTemplateFile
End Sub
-
Ron that is in VBA however that is for
Default Template for Sheet Creation and Pagesetup Overrides
I need the method for the one above it
Sheet Set Template File Location
Thanks again
Mark
DOH!
I need to read posts better :oops:
-
How about this?
(vla-put-templatedwgpath
(vla-get-files
(vla-get-preferences
(vlax-get-acad-object)
)
)
"c:\\"
)
(setenv "TemplatePath" "C:\\")
-
Hey Ron, I didn't try it yet but I was hoping to keep it all within THe VBA Module.
I think Bryon is on the right track, I can try writing the path directly to the registry.
I did fine the exact location of the path in the registry, now I just need to get it there :|
Mark
-
Hey Bryon
I'm sorry, may be it's me but I do not se a sub routine called Registry and I believe the only constant I need is
Public Const HKEY_CURRENT_USER = &H80000001 for this application. Please correct me if I am wrong.
I'm not even sure that needs to be declared as a constant as I am only using it once in this app.
I can probably just declare is it as a String data type.
I appreciate your help
Mark
-
Mark, if the sub is in the same dvb (project)as the code I said to put in a standard module, when you hit the line
WriteStringValue HKEY_CURRENT_USER, KeyName, valueName, dataValue
it will look for the sub WriteStringValue and find it in the module, that in turn will look for the Const HKEY_CURRENT_USER, and find it in the same module.
You can drag out the applicable code to make it smaller but having the whole module is great for doing other work requiring registry writing reading etc.
Api's work a little different than regular code, often you pass them an empty variable, long or whatever and they fill that variable with what you need. But they have a structure that you don't want to mess with. If you copy all the code into one module it will work fine
-
Hey Bry
That sounds great and I want to "definetely" take a closer look at The whole project you posted, it is very interesting. However, I don't feel comfortable pasting all of that code into a module and running it when I am not sure of the reprocussions.
That is why I just wanted what I need to get this path into the registry for now.
I am not sure if you can supply just that, but if you can I would very much appreciate it
Thank you
Mark
-
Hi Bryon
From what I understand, you can not write to the registry via VBA. Although the code may work successfully and update the registry, AutoCAD has to be open in order to run the module and must once closed will update the registry.
Therefore once you close ACAD, it is going to overwrite your registry entry with the existing path.
Mark