Solved

Modify Registry Keys with Code

Posted on 2007-04-06
13
543 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

785 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