Solved

Modify Registry Keys with Code

Posted on 2007-04-06
13
532 Views
Last Modified: 2008-01-09
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.

0
Comment
Question by:Skip_Laughlin
  • 7
  • 5
13 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18867765
Place this at the top of your code...
Const HKEY_CURRENT_USER = &H80000001
0
 

Author Comment

by:Skip_Laughlin
ID: 18867905
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)

???
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18867915
Looks like these should be added...

Const REG_OPTION_NON_VOLATILE = 0
Const KEY_ALL_ACCESS = &H3F
0
 

Author Comment

by:Skip_Laughlin
ID: 18867977
OK.  Added those.  So far, so good.  But , it still doesn't like REG_SZ

??

0
 

Author Comment

by:Skip_Laughlin
ID: 18867983
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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18867997
That const (reg_sz) should be

Const REG_SZ = 1
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Skip_Laughlin
ID: 18868021
Sorry to be so dense, but this is way over my head.  Now it doesn't like

RegSetValueExString
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)

??
0
 

Author Comment

by:Skip_Laughlin
ID: 18868031
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
0
 

Author Comment

by:Skip_Laughlin
ID: 18868053
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.

0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18868359
Sorry - had to step out for a bit.
Yes, looks like you got the function...module is fine. :)
0
 
LVL 4

Expert Comment

by:quiklearner
ID: 18868370
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..
0
 

Author Comment

by:Skip_Laughlin
ID: 18869228
Thanks very much.  Sir, I will gladly confirm your rank in the group!  I appreciate the help.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18869510
My rank in the group?
Well, glad it worked for you - thanx! :^)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now