Solved

Print Code by specific user

Posted on 2013-01-09
8
176 Views
Last Modified: 2013-01-09
I'm using the following code to automatically print my file via vba. But now I need to run different code depending on the user. How can I make this code switch to a different code when a different user opens the file? The code called "PrintBCJobs" should be run when user "jdoe" opens the file. The code called "PrintBCJobsLS" should be run when user "lsalvucci" opens the file. How can I specify this in VBA?

Sub PrintBCJobs()
Dim ActivePrint As String
    ActivePrint = Application.ActivePrinter
    ActiveSheet.PageSetup.PrintArea = ""
    Application.ActivePrinter = ActivePrint
    ExecuteExcel4Macro _
        "PRINT(1,,,1,,,,,,,,2,""ActivePrint"",,TRUE,,FALSE)"
    ActiveWindow.SelectedSheets.PrintPreview
    ActiveSheet.PageSetup.PrintArea = ""
    ActiveWindow.SelectedSheets.PrintPreview
    ActiveWorkbook.Save
End Sub


Sub PrintBCJobsLS()
    ActivePrint = Application.ActivePrinter
    ActiveSheet.PageSetup.PrintArea = ""
    Application.ActivePrinter = "Dell 2150cdn Color Printer on Ne06:"
    ExecuteExcel4Macro _
        "PRINT(1,,,12,,,,,,,,2,""Dell 2150cdn Color Printer on Ne06:"",,TRUE,,FALSE)"
    ActiveWindow.SelectedSheets.PrintPreview
    ActiveSheet.PageSetup.PrintArea = ""
    ActiveWindow.SelectedSheets.PrintPreview
    ActiveWorkbook.Save
End Sub

Open in new window

0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:armchair_scouse
ID: 38758821
You can use something like the following (if on Windows) to read the user name from the environment settings:

UserID = Environ("USERNAME")
0
 
LVL 9

Accepted Solution

by:
armchair_scouse earned 500 total points
ID: 38758831
Ooops... forgot to add...  once you've worked out who the user is, your code could be modified to include a Case statement, e.g.

Select Case UserID
   Case "Fred"
      ... run some code
   Case "Bert"
     ...  run some different code
End Select
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 38758864
So is this how the code would look? Because it doesn't seem to run at all. Am I missing something?

Sub PrintBCJobsReport()

UserId = Environ("USERNAME")

Select Case UserId
   Case "lsalvucci"
        PrintBCJobsLS
   Case "sconnell"
        PrintBCJobs
End Select

End Sub

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 48

Expert Comment

by:Martin Liss
ID: 38759200
Have you dim-ed UserId somewhere?

Are those UserIds correct? You can check your own by clicking in the left-hand margin of line 5 and when the code gets there (if it does), see what's in UserId. If you need more help debugging you can read this article I wrote. It's aimed at VB6 but most of it applies to VBA as well.
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 38759286
No I did not dim the UserId. What should I DIM it as?
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38759295
Dim UserID As String
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38759307
Also if someone else is running the code you can do this and have them tell you what their userid says.

Sub PrintBCJobsReport()
Dim UserID As String

UserId = Environ("USERNAME")
MsgBox "My UserID is " & UserId
Select Case UserId
   Case "lsalvucci"
        PrintBCJobsLS
   Case "sconnell"
        PrintBCJobs
End Select

End Sub
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 38759308
Working now. Thank you very much!!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question