Use box to change part of VBA code?

ExcelRookie01

New Member
Joined
Mar 18, 2025
Messages
4
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi there.

I need to make a function for exporting a file as an PDF and attach it to an e-mail. I have the vba working for me, by inserting my username (U18695) in the FilePath.

However I need the function to work for multiple users, so I was wondering if it is possible to have an inputbox show up when running the macro, where the user can type in their username and then have the variable username inserted into the filepath in the code???

I have inserted the first part of the code below. Please let me know if you would need the whole code to help me out :)
Thanks!!


CODE:

Sub SendBelæg_PO()

Dim OutApp As Object

Dim OutMail As Object

Dim Filepath As String

Dim Filename As String


'Set the file path and name for the PDF file

Filepath = "C:\Users\U18695\Documents\"

Filename = ActiveWorkbook.Name & ".pdf"
 
How about capturing the username rather than typing it?
VBA Code:
Sub SendBelæg_PO()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Filepath As String
    Dim Filename As String
    Dim objNetwork As Object
    Dim uName As String
   
    'Set the file path and name for the PDF file
    Set objNetwork = CreateObject("WScript.Network")
    uName = objNetwork.UserName
    Filepath = "C:\Users\" & uName & "\Documents\"
    Filename = ActiveWorkbook.Name & ".pdf"

    Set objNetwork = Nothing
End Sub
 
Last edited:
Upvote 0
Why do you need an input box? can you not just use the code below?

VBA Code:
Filepath = "C:\Users\" & Application.UserName & "\Documents\"

or

VBA Code:
Filepath = "C:\Users\" & Environ("USERNAME") & "\Documents\"

depending on what username you want
 
Upvote 1
Solution
Why do you need an input box? can you not just use the code below?

VBA Code:
Filepath = "C:\Users\" & Application.UserName & "\Documents\"

or

VBA Code:
Filepath = "C:\Users\" & Environ("USERNAME") & "\Documents\"

depending on what username you want

Just tried this out.

The first one didn't work for some reason

The second one worked for me, but not when my co-worker tried it out


I have attached some more of the code, as it was in the line beginning with "ActiveSheet" the macro reported a bug



Sub SendBelæg_PO()

Dim OutApp As Object

Dim OutMail As Object

Dim Filepath As String

Dim Filename As String


'Set the file path and name for the PDF file

Filepath = "C:\Users\" & Environ("USERNAME") & "\Documents\"

Filename = ActiveWorkbook.Name & ".pdf"


'Export the active sheet as a PDF file

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Filepath & Filename, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False


'Create a new Outlook email

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)
 
Upvote 0
That is the code that only worked from my user, but didn't work for my co-worker with a different username
 
Upvote 0
I have attached some more of the code, as it was in the line beginning with "ActiveSheet" the macro reported a bug
What does the error state?

Also does the filepath look correct in the Immediate Window if you run the code below?

Rich (BB code):
Dim Filename As String


'Set the file path and name for the PDF file

Filepath = "C:\Users\" & Environ("USERNAME") & "\Documents\"

Filename = ActiveWorkbook.Name & ".pdf"

Debug.Print Filename
 
Upvote 0
Just testet it out with another co-worker and now it works! :)

Thanks a lot for the much simpler solution, than I thought I needed!
 
Upvote 0

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