OberonPlace.com Forums

OberonPlace.com Forums (http://forum.oberonplace.com/index.php)
-   CorelDRAW/Corel DESIGNER VBA (http://forum.oberonplace.com/forumdisplay.php?f=16)
-   -   exporting registry entries to a *.reg file (http://forum.oberonplace.com/showthread.php?t=7653)

gorgo 28-04-2011 22:14

exporting registry entries to a *.reg file
 
I found this code:

Code:

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

arrRegPaths = Array( _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset1\", _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset2\", _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset3\", _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset4\" _
      )

Const intForReading = 1
Const intUnicode = -1

strFileName = objShell.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\RegKeys.reg"

For Each strRegPath In arrRegPaths
      strCommand = "cmd /c REG EXPORT " & strRegPath & " " & Replace(strRegPath, "\", "_") & ".reg"
      objShell.Run strCommand, 0, True
      Set objRegFile = objFSO.CreateTextFile(strFileName, True, True)
      objRegFile.WriteLine "Windows Registry Editor Version 5.00"
      If objFSO.FileExists(Replace(strRegPath, "\", "_") & ".reg") = True Then
            'WScript.Sleep 1000 ' Wait one second to give the file time to close
            Set objInputFile = objFSO.OpenTextFile(Replace(strRegPath, "\", "_") & ".reg", intForReading, False, intUnicode)
            If Not objInputFile.AtEndOfStream Then
                  objInputFile.SkipLine
                  objRegFile.Write objInputFile.ReadAll
            End If
            objInputFile.Close
            Set objInputFile = Nothing
            objFSO.DeleteFile Replace(strRegPath, "\", "_") & ".reg", True
      End If
      objRegFile.Close
      Set objRegFile = Nothing
Next

MsgBox "Finished."

But if I use this in a button in a form and run it, I get a Compile Error, Variable not defined at line ObjShell. How do I define it?

gorgo 28-04-2011 22:38

*UPDATE:

running this will give the error/stop Runtime Error 91, Object Variable or With block Variable not set.

Code:

Private Sub CommandButton1_Click()
Dim objShell As Object
Dim objFSO As Object
Dim arrRegPaths As Object
Dim strFileName As Object
Dim objRegFile As Object
Dim strRegPath As Object
Dim strCommand As Object
Dim objInputFile As Object
'Dim objShell As Object
Dim objInputFire As Object

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

arrRegPaths = Array( _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset1", _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset2", _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset3", _
      "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset4" _
      )

Const intForReading = 1
Const intUnicode = -1

strFileName = objShell.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\PageNumberer_settings.reg"

Set objRegFile = objFSO.CreateTextFile(strFileName, True, True)
objRegFile.WriteLine "Windows Registry Editor Version 5.00"

For Each strRegPath In arrRegPaths
      strCommand = "cmd /c REG EXPORT " & strRegPath & " " & Replace(strRegPath, "\", "_") & ".reg"
      objShell.Run strCommand, 0, True
      If objFSO.FileExists(Replace(strRegPath, "\", "_") & ".reg") = True Then
            'WScript.Sleep 1000 ' Wait one second to give the file time to close
            Set objInputFile = objFSO.OpenTextFile(Replace(strRegPath, "\", "_") & ".reg", intForReading, False, intUnicode)
            If Not objInputFile.AtEndOfStream Then
                  objInputFile.SkipLine
                  objRegFile.Write objInputFile.ReadAll
            End If
            objInputFile.Close
            Set objInputFile = Nothing
            objFSO.DeleteFile Replace(strRegPath, "\", "_") & ".reg", True
      End If
Next

objRegFile.Close
Set objRegFile = Nothing

MsgBox "Finished."
End Sub


shelbym 28-04-2011 23:27

Code
 
REM all the following lines:
Code:

'Dim arrRegPaths As Object
'Dim strFileName As Object
'Dim objRegFile As Object
'Dim strRegPath As Object
'Dim strCommand As Object
'Dim objInputFile As Object
'Dim objShell As Object
'Dim objInputFire As Object

and it should work, many of these variables should be be redefined to the proper types, but that is the quick answer that should work for you.

-Shelby

gorgo 28-04-2011 23:31

1 Attachment(s)
Quote:

Originally Posted by shelbym (Post 18841)
REM all the following lines:
Code:

'Dim arrRegPaths As Object
'Dim strFileName As Object
'Dim objRegFile As Object
'Dim strRegPath As Object
'Dim strCommand As Object
'Dim objInputFile As Object
'Dim objShell As Object
'Dim objInputFire As Object

and it should work, many of these variables should be be redefined to the proper types, but that is the quick answer that should work for you.

-Shelby

okay, did this. now error.

gorgo 28-04-2011 23:34

1 Attachment(s)
Quote:

Originally Posted by gorgo (Post 18842)
okay, did this. now error.

ah ha! got it to work now with alternate changes. verifying.

gorgo 28-04-2011 23:39

new problem. I got this far, now the *.reg file is empty (other than "Windows Registry Editor Version 5.00
"). What to do to tell this code to properly input the registry values as noted in the lines starting with HKEY_CURRENT_USER.

shelbym 28-04-2011 23:57

Reg
 
It means you have no values under the keys it pulled.

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\PNPreset1

-Shelby

Alex 02-05-2011 13:36

Do you really need to do this in the code? Why not going to registry editor, right-clicking the key and selecting "Export"?

gorgo 02-05-2011 18:39

Quote:

Originally Posted by Alex (Post 18853)
Do you really need to do this in the code? Why not going to registry editor, right-clicking the key and selecting "Export"?

Yes, the purpose for having this possible in a macro is to distribute saved settings for macros like PageNumbering and Thumbnailer as well as other possible ideas. Settings like text size, spacing, margins, colors, files paths, presets can all be exported into a *.reg file that can be shared with others in the workgroup instead of showing them the settings. They just get the reg file, open, then allow it to add the data to the registry and there you go.

runflacruiser 02-05-2011 20:32

Quote:

Originally Posted by gorgo (Post 18854)
can all be exported into a *.reg file that can be shared with others in the workgroup instead of showing them the settings.

Hi.
They can still see the settings by opening the reg file in Wordpad.

~John


All times are GMT -5. The time now is 15:06.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com