Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Modify Registry Keys with Code

Posted on 2007-04-06
13
Medium Priority
?
573 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
Technology Partners: 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

604 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