Solved

Modify Registry Keys with Code

Posted on 2007-04-06
13
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

728 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