PDA

View Full Version : Print Text From vbYes/No Message Box Using Default Printer



oxicottin
12-22-2012, 06:44 AM
Hello, I have found a few code examples that would work for me but what I cant figure out is how to apply the "Default Printer" VB to the message box sub instead of the full path to a printer. I needed to use the default printer because it might change with each person that prints it. Thanks!

Sub PrintMessageBox()

Dim MessageText As String
Dim Answer As Variant

MessageText = "Test Message to print in default printer." & vbCrLf _
& "If you would like to print it, select Yes" & vbCrLf _
& "If not, select No"

Answer = MsgBox(MessageText, vbInformation + vbYesNo, _
"A Message From Our Sponsor")

If Answer = vbYes Then
Open "c:tempmsg.txt" For Output As #1
Print #1, MessageText
Close
Shell "print ??DEFAULT PRINTER??? c:tempmsg.txt"
Application.Wait Now + 2 / 86400 'wait two seconds for Shell completion
Kill "c:tempmsg.txt"
End If

End Sub


Code Found (written by Leith Ross) to print to default printer:


'Written: March 15, 2008
'Author: Leith Ross
'Summary: Prints a file using the default printer


'This will print a file if it supports the Print Command
Private 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 Long

Sub PrintFile(ByVal FileName As String, Optional ByVal FileDir As String)

Dim Ret As Long

If FileDir = "" Then FileDir = CurDir

Ret = ShellExecute(0&, "print", FileName, vbNullString, FileDir, vbHide)

'Did Connection Fail? Errors are from 0 to 32
If Ret <= 32 Then
Select Case Ret
Case 2 'SE_ERR_FNF
Msg = "File not found"
Case 3 'SE_ERR_PNF
Msg = "Path not found"
Case 5 'SE_ERR_ACCESSDENIED
Msg = "Access denied"
Case 8 'SE_ERR_OOM
Msg = "Out of memory"
Case 32 'SE_ERR_DLLNOTFOUND
Msg = "DLL not found"
Case 26 'SE_ERR_SHARE
Msg = "A sharing violation occurred"
Case 27 'SE_ERR_ASSOCINCOMPLETE
Msg = "Incomplete or invalid file association"
Case 28 'SE_ERR_DDETIMEOUT
Msg = "DDE Time out"
Case 29 'SE_ERR_DDEFAIL
Msg = "DDE transaction failed"
Case 30 'SE_ERR_DDEBUSY
Msg = "DDE busy"
Case 31 'SE_ERR_NOASSOC
Msg = "Default Email not configured"
Case 11 'ERROR_BAD_FORMAT
Msg = "Invalid EXE file or error in EXE image"
Case Else
Msg = " Unknown error"
End Select

Msg = "Unable to Print File " _
& vbCrLf & "Error Number " & CStr(Ret) & " - " & Msg _
& vbCrLf & FileName & vbCrLf _
& FileDir
Ret = MsgBox(Msg, vbExclamation + vbOKOnly)
Exit Sub
End If

End Sub

Simon Lloyd
12-22-2012, 08:13 AM
Default printer would be the one set by the user as default you could always use the lineShell("Notepad.exe /p c:\tempmsg.txt", vbHide) or simply usePrinter.Print MessageText

oxicottin
12-22-2012, 08:33 PM
Thanks Simon Lloyd, I couldn't get either example to work. Im puting way to much thought into this so I'm just going to save it as a .text file and tell the user if they want to print it they can.

Thanks!

Simon Lloyd
12-23-2012, 03:02 AM
Or just bring the contents of the text file in to excel and then print it :)