Identify which computer

Haree

Board Regular
Joined
Sep 22, 2019
Messages
146
Office Version
  1. 2016
Hi all, I have a code which works fine from other computers. But when i use it from the main computer it does not work so can you please help me in identifying which computer the code is running so that i can use that printer


VBA Code:
Private Sub CommandButton1_Click()
Set mynetwork = CreateObject("WScript.network")
mynetwork.setdefaultprinter "\\Desktop-165t6df\THERMAL Receipt #1"
ActiveSheet.Range("A3:B52").Select
Selection.PrintOut Copies:=1
mynetwork.setdefaultprinter "\\Desktop-165t6df\HP LaserJet 1020"
End Sub

if the code is run from "Desktop-165t6df" this computer i just want the printer's name

Thanks in Advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try environ("computername") - i.e.

VBA Code:
mynetwork.setdefaultprinter "\\" & environ("computername") & "\THERMAL Receipt #1"
 
Upvote 0
Solution
Thank you so much RoryA. I was able to get the computer name using the environ function. I have changed the code using the environ function can you please check it . My if statement isnt working.

Thank you so much for your time and patience.

VBA Code:
Private Sub CommandButton1_Click()
Dim Computername As String
Set mynetwork = CreateObject("WScript.network")
Computername = Environ$("computername")
If Computername <> "Desktop-165t6df" Then
    mynetwork.setdefaultprinter "\\Desktop-165t6df\THERMAL Receipt #1"
    ActiveSheet.Range("A3:B52").Select
    Selection.PrintOut Copies:=1
    mynetwork.setdefaultprinter "\\Desktop-165t6df\HP LaserJet 1020"
Else
    mynetwork.setdefaultprinter "THERMAL Receipt #1"
    ActiveSheet.Range("A3:B52").Select
    Selection.PrintOut Copies:=1
    mynetwork.setdefaultprinter "HP LaserJet 1020"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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