Author Topic: VBScripting my way to The Registry  (Read 6483 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
VBScripting my way to The Registry
« on: May 22, 2007, 05:16:00 PM »

OK, I am new to VBScripting and (trying) to write to the registry so please be gentle.
I have some code written that goes like this:

Code: [Select]
  Dim WshShell
 Dim KeyName, ValueName, ValueData As String
       
 Set WshShell = WScript.CreateObject("WScript.Shell")
   

  KeyName = "HKEY_USERS\S-1-5-21-1749436377-938699083-1307212239-11542\Software\Autodesk\AutoCAD\R16.2\ACAD-4008:409\Profiles\Test\General"
  ValueName = "SheetSetTemplatePath"
  ValueData = "I:\Path\Test"       
           
   
    WshShell.RegWrite KeyName & ValueName & ValueData"

    WshShell = Nothing

I am sure there are a few things wrong in the code as it is erroring out on me.
I am a bit suprised, I am not sure why it is erroring out when creating the WScript.Shell Object but that is where it seems to be.
If someone could help steer me in the right direction, I would appreciate it.
Also, I was wondering if it is possible to access The WScript.Shell Object from within AutoCAD VBA as well?

Thank you,

Mark

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: VBScripting my way to The Registry
« Reply #1 on: May 22, 2007, 06:10:34 PM »
If this helps, this works in VBA:
Code: [Select]
Sub testme()
Dim scrpt As New WshShell
Dim DataVal As String
Dim regKey As String

regKey = "HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R17.1\ACAD-6000:409\Profiles\Civil3D\General\SheetSetTemplatePath"
DataVal = scrpt.RegRead(regKey)
scrpt.RegWrite regKey, DataVal & "\subFolder"
Debug.Print scrpt.RegRead(regKey)
scrpt.RegWrite regKey, DataVal ''return it to it's origianl state
End Sub

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #2 on: May 23, 2007, 10:06:47 AM »

Jeff

It doesn't seem to work, I am getting a user defined type not defined.
Are you sure there is an object in VBA called New WshShell?

Mark

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: VBScripting my way to The Registry
« Reply #3 on: May 23, 2007, 04:24:12 PM »
Well, you must add a reference to WindowsScriptingHost and then it will work. Sorry for not mentioning that part.....

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #4 on: May 24, 2007, 12:21:53 AM »

So are you saying that the reference I set should look like this:?

Set scrpt = CreateObject("New WshShell")


Dnereb

  • Guest
Re: VBScripting my way to The Registry
« Reply #5 on: May 24, 2007, 06:15:20 AM »
No,

in VBA you can add a reference to a type library in the VBA editor. This makes the code know how a certain object model is build. I do not know if VB script has the same references.
But if it doesn't look for code editing the registry through API calls that way you don't need a reference.

HarleyHetz

  • Guest
Re: VBScripting my way to The Registry
« Reply #6 on: May 24, 2007, 08:16:04 AM »
Also, make DARN sure BEFORE you go poking around in your registry that you make a backup of it. You can hose your computer QUICK in there!!!!

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #7 on: May 24, 2007, 09:23:24 AM »


Yes,
I did back up The registry, I do realize it is treading in muddy waters.
In some instances, VBA can access some of the same objects that VBSCript can.
For instance, I believe you can access The FileSystemObject through VBA but I don't believe that in like The WSCript.Shell Object.

As far as I see, there doesn't seem to be an easy way to write to the registry via VBA.

It kind of pisses me off  :realmad: that AutoDesk will allow most paths to be set via The VBA API but for some odd reason, a few others are not accessible.

So, howelse do you set a path programmatically without going straight to the registry.

Anyone know a "finite" way to make this happen via VBA?

Thanks

Mark

Dave R

  • Guest
Re: VBScripting my way to The Registry
« Reply #8 on: May 24, 2007, 10:19:30 AM »
Mark -

Try putting the code below in a .vbs file:

Code: [Select]
Dim WshShell
 Dim KeyName
 Dim ValueData
       
 Set WshShell = WScript.CreateObject("WScript.Shell")
   

  KeyName = "HKEY_USERS\S-1-5-21-1749436377-938699083-1307212239-11542\Software\Autodesk\AutoCAD\R16.2\ACAD-4008:409\Profiles\Test\General\SheetSetTemplatePath"
 
   ValueData = "I:\Path\Test"       
           
   
    WshShell.RegWrite KeyName, ValueData

    Set WshShell = Nothing


OK, I am new to VBScripting and (trying) to write to the registry so please be gentle.
I have some code written that goes like this:

Code: [Select]
  Dim WshShell
 Dim KeyName, ValueName, ValueData As String
       
 Set WshShell = WScript.CreateObject("WScript.Shell")
   

  KeyName = "HKEY_USERS\S-1-5-21-1749436377-938699083-1307212239-11542\Software\Autodesk\AutoCAD\R16.2\ACAD-4008:409\Profiles\Test\General"
  ValueName = "SheetSetTemplatePath"
  ValueData = "I:\Path\Test"       
           
   
    WshShell.RegWrite KeyName & ValueName & ValueData"

    WshShell = Nothing

I am sure there are a few things wrong in the code as it is erroring out on me.
I am a bit suprised, I am not sure why it is erroring out when creating the WScript.Shell Object but that is where it seems to be.
If someone could help steer me in the right direction, I would appreciate it.
Also, I was wondering if it is possible to access The WScript.Shell Object from within AutoCAD VBA as well?

Thank you,

Mark

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #9 on: May 24, 2007, 03:15:31 PM »
Hey Dave, thanks!

The below code worked! It wrote the path to the registry

Code: [Select]
Dim WshShell
Dim KeyName
Dim ValueData
       
Set WshShell = WScript.CreateObject("WScript.Shell")
   

KeyName = "HKEY_USERS\S-1-5-21-1749436377-938699083-1307212239-11542\Software\Autodesk\AutoCAD\R16.2\ACAD-4008:409\Profiles\Test\General\SheetSetTemplatePath"
 
   ValueData = "I:\Path\Test"       
           
   
    WshShell.RegWrite KeyName, ValueData

    Set WshShell = Nothing

In VBA, it worked by removing The Wscript and simply saying:

Code: [Select]
Dim WshShell
Dim KeyName as String
Dim ValueData as String
       
[b]Set WshShell = CreateObject("WScript.Shell")[/b]


KeyName = "HKEY_USERS\S-1-5-21-1749436377-938699083-1307212239-11542\Software\Autodesk\AutoCAD\R16.2\ACAD-4008:409\Profiles\Test\General\SheetSetTemplatePath"
 
ValueData = "I:\Path\Test"       
           
   
WshShell.RegWrite KeyName, ValueData

Set WshShell = Nothing

However, neither one updated the path in my ACAD Profile which is the end result I need.

Any idea why it is not being updated in CAD?

Thanks

Mark

Dave R

  • Guest
Re: VBScripting my way to The Registry
« Reply #10 on: May 24, 2007, 04:18:15 PM »
What Profile do you have set current?

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #11 on: May 24, 2007, 04:44:24 PM »

Profile "Test"


From what I understand or am learning; you can not write to the registry with ACAD open while using a vbs script.
Therefore, ACAD must be closed and not referenced in the code.

Mark

Dave R

  • Guest
Re: VBScripting my way to The Registry
« Reply #12 on: May 25, 2007, 07:58:14 AM »
Yes, I believe that is correct ACAD needs to be closed to write to a registry key, but if your original intention was to run the code from a VBScript wouldn't that be the case?


Profile "Test"


From what I understand or am learning; you can not write to the registry with ACAD open while using a vbs script.
Therefore, ACAD must be closed and not referenced in the code.

Mark


ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #13 on: May 25, 2007, 12:18:03 PM »

Yes, you are correct but I made the mistake of adding a reference to The ACAD Application Object and that screwed me up. I have been doing VBA for a little while but scripting is very new to me and I am not real use to accessing other programs from CAD or outside of the application. I must admit, it is great stuff but I need to exercise a lot more care in doing so then I would with just customizing the software from within VBA.

For some reason, I was thinking I needed to open a session of CAD first, then write the code but I was mistaken.

I think I am getting on the right path now.

Mark


Dave R

  • Guest
Re: VBScripting my way to The Registry
« Reply #14 on: May 25, 2007, 02:43:36 PM »
Mark -

Since you're new to scripting you may want to take a stroll through here. There's a lot of good info to be had.

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #15 on: May 25, 2007, 02:51:05 PM »

Thanks Dave

Microsoft has a VBScripting users guide, do you have that?

If you'd like, I can send you the link.

Mark

Dave R

  • Guest
Re: VBScripting my way to The Registry
« Reply #16 on: May 25, 2007, 03:52:26 PM »
Already have it. But thanks for the offer.


Thanks Dave

Microsoft has a VBScripting users guide, do you have that?

If you'd like, I can send you the link.

Mark

ML

  • Guest
Re: VBScripting my way to The Registry
« Reply #17 on: May 25, 2007, 07:34:06 PM »

Duh That is precisely what you sent to me (The VBScripting Users Guide)
May be next time I should look first LOL   :-D