J
Justin
Hello Everyone
I've been working on some code for a very long time. What I'm trying to do is..
1) Select a wordperfect file from the open dialog box
2) Change the default printer to Adobe Writer
3) Print the selected wordperfect file in Adobe without naming it (it is named in VB so it is always saved to the same place)
I cannot figure out why my code doesn't work. I think the problem is that I do not name wordperfect as the hwnd variable under the ShellExecute function
Thank you Alex Breitkreutz for your help so far
Here is the code..
Option Compare Databas
'*************************************
'Windows API/Global Declarations for :C
' eate PDF from MS Access Repor
'*************************************
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Lon
Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Lon
Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Lon
Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Lon
Declare Function RegCreateKeyEx Lib "advapi32" 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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Lon
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation
As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd
As Long) As Lon
Global Const SW_SHOWNORMAL =
#If Win32 The
Public Const HKEY_CLASSES_ROOT = &H8000000
Public Const HKEY_CURRENT_USER = &H8000000
Public Const HKEY_LOCAL_MACHINE = &H8000000
Public Const HKEY_USERS = &H8000000
Public Const KEY_ALL_ACCESS = &H3
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const REG_CREATED_NEW_KEY = &H
Public Const REG_OPENED_EXISTING_KEY = &H
Public Const ERROR_SUCCESS = 0
Public Const REG_SZ = (1
#End I
Type SECURITY_ATTRIBUTE
nLength As Lon
lpSecurityDescriptor As Lon
bInheritHandle As Boolea
End Typ
'*************************************
' Name: Create PDF from MS Access Repor
' Description:On a machine where the Adobe PDFWriter is installed, the current printer is swapped out wit
' the PDFWriter and the PDF file is created. The original printer is then restored
' Inputs:rptName = Microsoft Access report name you want to create pdf from. sPDFPath = the directory pat
' where you want to create the pdf file (ex. - "c:\data\"). sPDFName = the name of the pdf file you ar
' wanting to create (ex. - "file001.pdf")
' Assumes:This code is easily modified to be used in other program
' Side Effectslease use the most recent installs of Adobe Exchange or PDFWriter to ensure proper functionality
Public Function bGetRegValue(ByVal hKey As Long, ByVal sKey As String, ByVal sSubKey As String) As Strin
Dim lResult As Lon
Dim phkResult As Lon
Dim dWReserved As Lon
Dim szBuffer As Strin
Dim lBuffSize As Lon
Dim szBuffer2 As Strin
Dim lBuffSize2 As Lon
Dim lIndex As Lon
Dim lType As Lon
Dim sCompKey As Strin
Dim bFound As Boolea
lIndex =
lResult = RegOpenKeyEx(hKey, sKey, 0, 1, phkResult
Do While lResult = ERROR_SUCCESS And Not (bFound
szBuffer = Space(255
lBuffSize = Len(szBuffer
szBuffer2 = Space(255
lBuffSize2 = Len(szBuffer2
lResult = RegEnumValue(phkResult, lIndex, szBuffer, lBuffSize, dWReserved, lType, szBuffer2, lBuffSize2
If (lResult = ERROR_SUCCESS) The
sCompKey = Left(szBuffer, lBuffSize
If (sCompKey = sSubKey) Then
bGetRegValue = Left(szBuffer2, lBuffSize2 - 1)
RegCloseKey phkResult
Exit Function
End If
End If
lIndex = lIndex + 1
Loop
RegCloseKey phkResult
End Function
Public Function bSetRegValue(ByVal hKey As Long, ByVal lpszSubKey As String, ByVal sSetValue As String, ByVal sValue As String) As Boolean
On Error Resume Next
Dim phkResult As Long
Dim lResult As Long
Dim SA As SECURITY_ATTRIBUTES
Dim lCreate As Long
RegCreateKeyEx hKey, lpszSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, SA, phkResult, lCreate
lResult = RegSetValueEx(phkResult, sSetValue, 0, REG_SZ, sValue, CLng(Len(sValue) + 1))
RegCloseKey phkResult
bSetRegValue = (lResult = ERROR_SUCCESS)
End Function
Public Function RunReportAsPDF(rptName As String, sPDFPath As String, sPDFName As String, x As Integer) As String
'----------------------------------------------------------------------------------------
'rptName = Microsoft Access report name you want to create pdf from
'sPDFPath = the directory path where you want to create the pdf file (ex. - "c:\data\")
'sPDFName = the name of the pdf file you are wanting to create (ex. - "file001.pdf")
'----------------------------------------------------------------------------------------
Dim sMyDefPrinter As String
Dim strFileName As String
Dim strLocation As String
Dim strFileName2 As String
On Error GoTo Err_RunReport
'Save current default printer
sMyDefPrinter = bGetRegValue(HKEY_CURRENT_USER, "Software\Microsoft\WIndows NT\CurrentVersion\Windows", "Device")
'Set default printer to PDF Writer
bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "Acrobat PDFWriter"
'Setting value for PDFFileName in the registry stops file dialog box from appearing
bSetRegValue HKEY_CURRENT_USER, "Software\Adobe\Acrobat PDFWriter", "PDFFileName", sPDFPath + sPDFName
'Run the report
strFileName = Right(rptName, x - 1)
strLocation = Left(rptName, Len(rptName) - x + 1)
strFileName2 = "C:\" & Left(strFileName, Len(strFileName) - 4) & ".pdf"
RunReportAsPDF = ShellExecute(hwnd2, "open", strFileName, "", strLocation, SW_SHOWNORMAL)
Exit_RunReport:
'Restore default printer
bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\WIndows NT\CurrentVersion\Windows", "Device", sMyDefPrinter
Exit Function
Err_RunReport:
MsgBox err.DESCRIPTION
Resume Exit_RunReport
End Function
Public Sub TestThisShit()
Dim strFilter As String
Dim strInputFileName As String
Dim i As Integer
Dim str As String
Dim strChar As String
strFilter = ahtAddFilterItem(strFilter, "WP Documents (*.WPD)", "*.WPD")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)
i = 0
Do Until strChar = "\"
strChar = Mid(strInputFileName, Len(strInputFileName) - i, 1)
i = i + 1
Loop
str = RunReportAsPDF(strInputFileName, "C:\", "ustin.pdf", i)
End Sub
I've been working on some code for a very long time. What I'm trying to do is..
1) Select a wordperfect file from the open dialog box
2) Change the default printer to Adobe Writer
3) Print the selected wordperfect file in Adobe without naming it (it is named in VB so it is always saved to the same place)
I cannot figure out why my code doesn't work. I think the problem is that I do not name wordperfect as the hwnd variable under the ShellExecute function
Thank you Alex Breitkreutz for your help so far
Here is the code..
Option Compare Databas
'*************************************
'Windows API/Global Declarations for :C
' eate PDF from MS Access Repor
'*************************************
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Lon
Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Lon
Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Lon
Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Lon
Declare Function RegCreateKeyEx Lib "advapi32" 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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Lon
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation
As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd
As Long) As Lon
Global Const SW_SHOWNORMAL =
#If Win32 The
Public Const HKEY_CLASSES_ROOT = &H8000000
Public Const HKEY_CURRENT_USER = &H8000000
Public Const HKEY_LOCAL_MACHINE = &H8000000
Public Const HKEY_USERS = &H8000000
Public Const KEY_ALL_ACCESS = &H3
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const REG_CREATED_NEW_KEY = &H
Public Const REG_OPENED_EXISTING_KEY = &H
Public Const ERROR_SUCCESS = 0
Public Const REG_SZ = (1
#End I
Type SECURITY_ATTRIBUTE
nLength As Lon
lpSecurityDescriptor As Lon
bInheritHandle As Boolea
End Typ
'*************************************
' Name: Create PDF from MS Access Repor
' Description:On a machine where the Adobe PDFWriter is installed, the current printer is swapped out wit
' the PDFWriter and the PDF file is created. The original printer is then restored
' Inputs:rptName = Microsoft Access report name you want to create pdf from. sPDFPath = the directory pat
' where you want to create the pdf file (ex. - "c:\data\"). sPDFName = the name of the pdf file you ar
' wanting to create (ex. - "file001.pdf")
' Assumes:This code is easily modified to be used in other program
' Side Effectslease use the most recent installs of Adobe Exchange or PDFWriter to ensure proper functionality
Public Function bGetRegValue(ByVal hKey As Long, ByVal sKey As String, ByVal sSubKey As String) As Strin
Dim lResult As Lon
Dim phkResult As Lon
Dim dWReserved As Lon
Dim szBuffer As Strin
Dim lBuffSize As Lon
Dim szBuffer2 As Strin
Dim lBuffSize2 As Lon
Dim lIndex As Lon
Dim lType As Lon
Dim sCompKey As Strin
Dim bFound As Boolea
lIndex =
lResult = RegOpenKeyEx(hKey, sKey, 0, 1, phkResult
Do While lResult = ERROR_SUCCESS And Not (bFound
szBuffer = Space(255
lBuffSize = Len(szBuffer
szBuffer2 = Space(255
lBuffSize2 = Len(szBuffer2
lResult = RegEnumValue(phkResult, lIndex, szBuffer, lBuffSize, dWReserved, lType, szBuffer2, lBuffSize2
If (lResult = ERROR_SUCCESS) The
sCompKey = Left(szBuffer, lBuffSize
If (sCompKey = sSubKey) Then
bGetRegValue = Left(szBuffer2, lBuffSize2 - 1)
RegCloseKey phkResult
Exit Function
End If
End If
lIndex = lIndex + 1
Loop
RegCloseKey phkResult
End Function
Public Function bSetRegValue(ByVal hKey As Long, ByVal lpszSubKey As String, ByVal sSetValue As String, ByVal sValue As String) As Boolean
On Error Resume Next
Dim phkResult As Long
Dim lResult As Long
Dim SA As SECURITY_ATTRIBUTES
Dim lCreate As Long
RegCreateKeyEx hKey, lpszSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, SA, phkResult, lCreate
lResult = RegSetValueEx(phkResult, sSetValue, 0, REG_SZ, sValue, CLng(Len(sValue) + 1))
RegCloseKey phkResult
bSetRegValue = (lResult = ERROR_SUCCESS)
End Function
Public Function RunReportAsPDF(rptName As String, sPDFPath As String, sPDFName As String, x As Integer) As String
'----------------------------------------------------------------------------------------
'rptName = Microsoft Access report name you want to create pdf from
'sPDFPath = the directory path where you want to create the pdf file (ex. - "c:\data\")
'sPDFName = the name of the pdf file you are wanting to create (ex. - "file001.pdf")
'----------------------------------------------------------------------------------------
Dim sMyDefPrinter As String
Dim strFileName As String
Dim strLocation As String
Dim strFileName2 As String
On Error GoTo Err_RunReport
'Save current default printer
sMyDefPrinter = bGetRegValue(HKEY_CURRENT_USER, "Software\Microsoft\WIndows NT\CurrentVersion\Windows", "Device")
'Set default printer to PDF Writer
bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "Acrobat PDFWriter"
'Setting value for PDFFileName in the registry stops file dialog box from appearing
bSetRegValue HKEY_CURRENT_USER, "Software\Adobe\Acrobat PDFWriter", "PDFFileName", sPDFPath + sPDFName
'Run the report
strFileName = Right(rptName, x - 1)
strLocation = Left(rptName, Len(rptName) - x + 1)
strFileName2 = "C:\" & Left(strFileName, Len(strFileName) - 4) & ".pdf"
RunReportAsPDF = ShellExecute(hwnd2, "open", strFileName, "", strLocation, SW_SHOWNORMAL)
Exit_RunReport:
'Restore default printer
bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\WIndows NT\CurrentVersion\Windows", "Device", sMyDefPrinter
Exit Function
Err_RunReport:
MsgBox err.DESCRIPTION
Resume Exit_RunReport
End Function
Public Sub TestThisShit()
Dim strFilter As String
Dim strInputFileName As String
Dim i As Integer
Dim str As String
Dim strChar As String
strFilter = ahtAddFilterItem(strFilter, "WP Documents (*.WPD)", "*.WPD")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)
i = 0
Do Until strChar = "\"
strChar = Mid(strInputFileName, Len(strInputFileName) - i, 1)
i = i + 1
Loop
str = RunReportAsPDF(strInputFileName, "C:\", "ustin.pdf", i)
End Sub