How to print out variables in VBA

selant

Board Regular
Joined
Mar 26, 2009
Messages
109
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.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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.
 
Upvote 0
can i create a text file into a temporary folder with a temporary file name ? any examples would greatly be appreciated.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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. ?
 
Upvote 0
I have no idea what you mean by that. You have to run the code - how you trigger it is up to you.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top