vba to retrieve dynamic username for file save

Mr_Ragweed2

Board Regular
Joined
Nov 11, 2022
Messages
145
Office Version
  1. 365
Platform
  1. Windows
Hello and thanks for reading! I have a spreadsheet that is saved on multiple users desktop. The issue is i don't (and won't) know who the users are. On the sheet there is a command button that will save it to a folder also on their desktop. This folder ("Finished Work") will have the same name for all users. At the end of the day, when they return to the office, they will upload that folder to Teams. As you can see in my code example below we are all on a One Drive. I know the code to retrieve my own pathway to the folder i just can't seem to find a way to retrieve an unknown username to add into the save pathway.
Any help is always appreciated and thank you in advance if you try and help me!

VBA Code:
 ChDir "C:\Users\My_Name\OneDrive - My_Company\Desktop" ' need to make this dynamic to any user - our format for "My_Name" is JSmith
 
Somehow i got it to work using the following: I'm assuming i finally got the concatenation correct as you pointed out :)

VBA Code:
Dim MyName As String
    MyName = Environ$("Username")
   
    ChDir "C:\Users\" & MyName & "\OneDrive - MyCompany Inc\Desktop\New folder" 'from excel forum
   
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\" & MyName & "\OneDrive - MyCompany Inc\Desktop\New folder" & "\" & newfile & ".xlsm" _
        , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.AutoSaveOn = False
    ActiveWorkbook.Save

I just went straight to the desktop C:\ drive instead of the https: method. For some reason i didnt think that was possible. What i notice though is when i open that folder it shows the files are still syncing - i'm assuming with OneDrive.
Thank you very much for your help in this matter! it is greatly appreciated.
Next step is to send all of the files in "new folder" to a Teams location. I have done that before from a worksheet but never from a whole folder.

Thanks again.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I'm sorry, but never used OneDrive, so I have no idea what your files should look like in this case.
Anyway, thanks for the positive feedback, glad having been of some help.
 
Upvote 0
i promised i would post results. Sorry it took so long. The following goes thru a folder, saves .xlsx files as pdfs and save them to appropriate destinations in sharepoint Teams. The only issue now is that the pdfs do not open in teams but do from the c:drive.
For this thread, the issue was acquiring an unknown user name so the path to sharepoint could be mapped correctly. As far as that part is concerned. the following is a solution.

Private Sub CommandButton1_Click()

Dim sourceFolderPath As String
Dim oldName As String
Dim newName As String
Dim wb2 As Workbook
Dim FSO As Object
Dim SourceFolder As Object
Dim File As Object

Application.ScreenUpdating = False

Dim MyName As String 'this bit here
MyName = Environ$("Username") 'and this bit here fixed the issue.

sourceFolderPath = "C:\Users\" & MyName & "\OneDrive - my companyInc\Desktop\Propane Forms"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(sourceFolderPath)

'12-26 12:30am this mostly works. files go to correct folders as pdfs and are deleted from original folder. "ZSync" is left alone.
' problem is that the pdfs wont open in Teams but will open on the C:drive....

On Error Resume Next

For Each File In SourceFolder.Files

oldName = File.Name

newName = Left(oldName, Len(oldName) - 5)

If oldName Like "*ZSync*.xlsm" Then
Exit Sub

ElseIf oldName Like "*SWO*" & ".xlsx" Then
Set wb2 = Workbooks.Open(File)
wb2.SaveAs fileName:= _
"https://abcd.sharepoint.com/teams/m...ystal, Mary, Anna/Propane Service Work Orders" _
& "/" & newName & ".pdf"
wb2.ExportAsFixedFormat Type:=xlTypePDF
ActiveWindow.Close
Kill "C:\Users\" & MyName & "\OneDrive - my company Inc\Desktop\Propane Forms\" & File.Name

ElseIf oldName Like "*TMS*" & ".xlsx" Then
Set wb2 = Workbooks.Open(File)
wb2.SaveAs fileName:= _
"https://abcd.sharepoint.com/teams/my company/Shared%20Documents/General/Crystal,%20Mary,%20Anna/Tank%20Movement%20Sheet/" _
& newName & ".pdf"
wb2.ExportAsFixedFormat Type:=xlTypePDF
ActiveWindow.Close
Kill "C:\Users\" & MyName & "\OneDrive - my company Inc\Desktop\Propane Forms\" & File.Name

End If

Next File

Set SourceFolder = Nothing
Set FSO = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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