Allocate the "User" variable in the path of the folder where Excel is located

Leandro Amaral 0 Reputation points
2023-03-28T11:20:36.64+00:00

Guys, good afternoon!

I just migrated to Office 365 with the entire server in the Cloud. The code below worked normally, as the main worksheet was on a physical server and the path of this file in Excel was fixed for any user who was logged on the network and on any machine connected to the network, but with the migration to the cloud, the path of the same became dynamic by One Drive through the username active on the computer.

I created the following subroutine to capture the active user on the machine and allocated it to a variable that worked when I printed a test message on the screen, as below:

Sub GetActiveUserNameAndSavePDF()

Sub GetActiveUserName()

Dim UserName As String

UserName = Environ("UserName")

Dim User As String

User = UserName

End Sub

The following code is what searches for all active information on the screen and saves it as a PDF in the folder that used to be on a physical server. I tried to allocate the variable in the path (highlighted in " & User & "), but it doesn't work, the path is not found:

Private Sub CommandButton1_Click()

ActiveWindow.SmallScroll Down:=-9

ActiveSheet.Range("A1", "K30").Select

ActiveWindow.SmallScroll Down:=-42

ChDir "C:\Users" & User & "\Unimed FDRJ\OCA - Documentos\trab\pac\Cardiologia\Prints" '<<<<<< ALTERAR O CAMINHO DE SALVAMENTO'

ThisWorkbook.Worksheets(Array("Autorização")).Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _

Filename:="C:\Users" & User & "\Unimed FDRJ\OCA - Documentos\trab\pac\Cardiologia\Prints - " & Range("B9") & " - " & Range("B11") & " - Senha " & Range("J7") & " (" & Range("C13").Value & ")"

ActiveWindow.SmallScroll Down:=-3

End Sub

Can you help me? thanks!

Office
Office
A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.
1,599 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,843 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,867 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.