Results 1 to 8 of 8

How to print out variables in VBA

This is a discussion on How to print out variables in VBA within the Excel Questions forums, part of the Question Forums category; Hi, I want to print out some text from my form but it doesnt seem to work. Here is the ...

  1. #1
    Board Regular selant's Avatar
    Join Date
    Mar 2009
    Posts
    104

    Unhappy How to print out variables in VBA

    Hi,

    I want to print out some text from my form but it doesnt seem to work.
    Here is the code :

    Code:
     
    Private Sub CommandButton2_Click()
     
        Dim rapor As String
        rapor = "Hello World"
        rapor.PrintOut Copies:=1, Collate:=True
        MsgBox (rapor)
     
    End Sub
    I dont know if this is the correct way to print out string variables from VBA.

  2. #2
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    East Sussex
    Posts
    22,828

    Default Re: How to print out variables in VBA

    If you actually want to send them to a printer, you will need to either output them into a worksheet and print that, or output them to a text file and print that. You cannot simply send a variable to the printer.

  3. #3
    Board Regular selant's Avatar
    Join Date
    Mar 2009
    Posts
    104

    Default Re: How to print out variables in VBA

    can i create a text file into a temporary folder with a temporary file name ? any examples would greatly be appreciated.

  4. #4
    Board Regular selant's Avatar
    Join Date
    Mar 2009
    Posts
    104

    Default Re: How to print out variables in VBA

    I hope somebody can help me coping with my printing problem My vba form will create a text to a variable and i want to print it out to the default printer. If its not possible to print directly from VBA, how can i create a temporary file to print the text out and delete the temporary file afterwards ? Any helps please

  5. #5
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    East Sussex
    Posts
    22,828

    Default Re: How to print out variables in VBA

    Something like this:
    Code:
    Option Explicit
    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
    Const SW_HIDE As Long = 0&
    ' change path as required.
    Private Const mc_strLOGFILEPATH As String = "C:\my_variable_log.txt"
    Sub LogVariables()
       Dim MyIndex, FileNumber
       Dim variable1, variable2
       
       variable1 = "This is variable1"
       variable2 = "This is variable2"
       ' Get unused file
       FileNumber = FreeFile
       ' Create log file
       Open mc_strLOGFILEPATH For Output As #FileNumber
       
       ' Output variables.
       Write #FileNumber, "Variable1 is: " & variable1
       Write #FileNumber, "Variable2 is: " & variable2
       
       ' Close file.
       Close #FileNumber
       ' Print file
       PrintFile mc_strLOGFILEPATH
    End Sub
    Sub PrintFile(strFilePath As String)
       ShellExecute Application.hwnd, "Print", strFilePath, 0&, 0&, SW_HIDE
    End Sub

  6. #6
    Board Regular selant's Avatar
    Join Date
    Mar 2009
    Posts
    104

    Default Re: How to print out variables in VBA

    this is a cool solution for me! Thank you very much! It worked.. The only point i want to ask is that, is there any possibility to create the file automatically instead of creating the file manually. ?

  7. #7
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    East Sussex
    Posts
    22,828

    Default Re: How to print out variables in VBA

    I have no idea what you mean by that. You have to run the code - how you trigger it is up to you.

  8. #8
    Board Regular selant's Avatar
    Join Date
    Mar 2009
    Posts
    104

    Default Re: How to print out variables in VBA

    ok thanks a lot.

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com