0

I have code to save a file. Here the path is static.

Sub savefile()

Dim strpath As String
Dim fry As String
Dim mth As String
Dim yr As String

yr = Year(Now)
mth = MonthName(Month(Now))
fry = Application.WorksheetFunction.Weekday(Date, 11)

Filename = "D:\Users\Desktop\ docs\" & yr & " " & "Week Of" & " " & mth & " " & fry & ".jpg"

ThisWorkbook.SaveCopyAs (Filename)

End Sub

How to replace this static or hardcode with a dynamic path?

4
  • In that case, you may have to ask the user, the path to save the file. Commented Nov 23, 2018 at 7:40
  • But how to write the code behind? Commented Nov 23, 2018 at 7:45
  • would whis work for you? Application.Dialogs(xlDialogSaveAs).Show filename Commented Nov 23, 2018 at 7:56
  • 1
    Are you trying to save a workbook as a JPG? This cannot work. Please tell what your actual goal is. Commented Nov 23, 2018 at 8:06

3 Answers 3

0

Try below sub

Sub SaveFileAs()
Dim strpath As String
Dim fry As String
Dim mth As String
Dim yr As String

    yr = Year(Now)
    mth = MonthName(Month(Now))
    fry = Application.WorksheetFunction.Weekday(Date, 11)

    Filename = yr & " " & "Week Of" & " " & mth & " " & fry

Application.Dialogs(xlDialogSaveAs).Show (Filename)
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

The same way you have done with the year, you can allow your user to input data for the path into a variable that is a string and then use & to put the entire path togheter.

example:

Sub savefile()

Dim strpath As String
Dim fry As String
Dim mth As String
Dim yr As String
Dim path As String

yr = Year(Now)
mth = MonthName(Month(Now))
fry = Application.WorksheetFunction.Weekday(Date, 11)    

Filename = yr & " " & "Week Of" & " " & mth & " " & fry & ".jpg"
Path = Application.DefaultFilePath & yr & " " & "Week Of" & " " & mth & " " & fry & ".jpg"

Application.Dialogs(xlDialogSaveAs).Show (Filename)
ThisWorkbook.SaveCopyAs (Path)


End Sub

If you want your user to input the path directly you can use the application.dialogs(xlDialogSaveAs).Show (Filename)

If you want the program to save all the files in a predefined path that can altho change depending on the structure of the computer you are using you could use the Application.DefaultFilePath that would save to documents in my case, depending on which folder the user has set up as it's default savefile path it will save there.

NOTE: the default saving location can be changed and it will be where Excel saves files by default.

Comments

0

If you share the same network drive the path should be stated as an UNC Path, for example: (\\?\C:\my_dir).

To find the UNC path, use the cmd.exe (command prompt) and write net use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.