Modify Registry Keys with Code

I need to create PDF's out of VB, using Crystal Reports.  I purchased a PDF writer which gives me the ability to call the pdf writer with code and also bypass the Save As dialog.  This is much better and easier for my users.  

I can changed the printer name, the printer port, and the printer driver names in code. because I ran .PrinterSelect with the Crystal OCX to see what they are!

But there are other registry settings that require modification to properly direct the ouput and to tell it to bypass the Save As dialog.

They gave me this sample code:which will set the ouput file name, but more importantly, override the Save As dialog:

Private Sub Print_PDF()
     Dim lRetVal As Long
     Dim hKey As Long
     Dim sValue As String
     lRetVal = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Custom PDF Printer",  _
                    0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
                    0&, hKey, lRetVal)
     sValue = "C:\Sample.pdf"
     RegSetValueExString hKey, "OutputFile", 0&, REG_SZ, sValue, Len(sValue)
     sValue = "1"
     RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len(sValue)  
      sValue = "0"
     RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len(sValue)          
     RegCloseKey (hKey)
End Sub

When I paste this code into my APP and try to run it, the FIRST thing it doesn't like is HKEY_CURRENT_USER.

I'm out of my depth here, so I need some pretty specific code to get this implemented.  Thanks for any help.

Who is Participating?
sirbountyConnect With a Mentor Commented:
Sorry - had to step out for a bit.
Yes, looks like you got the function...module is fine. :)
Place this at the top of your code...
Const HKEY_CURRENT_USER = &H80000001
Skip_LaughlinAuthor Commented:
OK, that worked.  Thanks.
Then it stumbled on KEY_ALL_ACCESS and  REG_OPTION_NON_VOLATILE.  I added the same constructs for them.  Is that right?

But with those constructs at the top, now it doesn't like the REG_SZ parameter on this line:
RegSetValueExString hKey, "OutputFile", 0&, REG_SZ, sValue, Len(sValue)

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Looks like these should be added...

Skip_LaughlinAuthor Commented:
OK.  Added those.  So far, so good.  But , it still doesn't like REG_SZ


Skip_LaughlinAuthor Commented:
I didn't tell you that I found this in another solution and put it at the top, too.  It seems to be OK.

Private Declare Function RegCreateKeyEx _
                Lib "advapi32.dll" _
                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, _
                 ByVal lpSecurityAttributes As Long, _
                 ByRef phkResult As Long, _
                 ByRef lpdwDisposition As Long) As Long
That const (reg_sz) should be

Const REG_SZ = 1
Skip_LaughlinAuthor Commented:
Sorry to be so dense, but this is way over my head.  Now it doesn't like

I looked for a function, but didn't see one.

I think there's only one more possible bump after that and it is RegCloseKey (hKey)

Skip_LaughlinAuthor Commented:
Well, I found these two but I don't know how to properly add them.

Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Skip_LaughlinAuthor Commented:
I put them in my code Module and it seems to be OK.   Do you agree, sirbounty?

Now my problem is getting the Crystal.OCX to cooperate.  'nother problem.

BTW, if you installed the VSTools when you installed VB, open the API Text Viewer.  Off of File choose Load Text File and select WIN32API.TXT.  You can now choose, under API Type, Declares (where you can find the def for the function you found) and Constants (which is where you can find the constant values).  Obviously your question is already answered so please don't take any points from sir..
Skip_LaughlinAuthor Commented:
Thanks very much.  Sir, I will gladly confirm your rank in the group!  I appreciate the help.
My rank in the group?
Well, glad it worked for you - thanx! :^)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.