Consulting

Results 1 to 4 of 4

Thread: Print Text From vbYes/No Message Box Using Default Printer

  1. #1

    Print Text From vbYes/No Message Box Using Default Printer

    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!

    [VBA]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[/VBA]


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

    [VBA]
    '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[/VBA]

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Default printer would be the one set by the user as default you could always use the line[vba]Shell("Notepad.exe /p c:\tempmsg.txt", vbHide)[/vba] or simply use[vba]Printer.Print MessageText[/vba]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    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!

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Or just bring the contents of the text file in to excel and then print it
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •