Create a general path for shared file - excel

I'm trying to use file dialogue to pick a file and save it to a folder but I can't figure out how to code a folder path and keep getting "Path not found error"
I'm not at all a coder, so please explain in plain English:)
Below is the piece of code I tried to use but didn't work.
ArchiveFolderPath = Environ("UserDomain") & "\" & Environ("username") & "\Desktop"
Full sub code from comment below:
Sub CreateCopyFile(FilePathToCopy As String)
Dim fso As Scripting.FileSystemObject
Dim FileToCopy As Scripting.File
Dim ArchiveFolderPath As String
'Create a new folder path
Set fso = New Scripting.FileSystemObject
ArchiveFolderPath = Environ("UserProfile") & "\Desktop\Archive"
'Create a new folder
If Not fso.FolderExists(ArchiveFolderPath) Then
fso.CreateFolder ArchiveFolderPath
End If
Set FileToCopy = fso.GetFile(FilePathToCopy)
FileToCopy.Copy ArchiveFolderPath & "\" & FileToCopy.name
Set fso = Nothing
End Sub

Try adding another backslash at the end of the word, "Desktop", and add two backslashes at the very beginning of the path. Like so:
ArchiveFolderPath = "\\" & Environ("UserDomain") & "\" & Environ("username") & "\Desktop\"

You need to put another \ at the end of Desktop and then make sure that you put the file name after it when you are saving.
So will look like ArchiveFolderPath = Environ("UserDomain") & "\" & Environ("username") & "\Desktop\"

Some of what you are looking for is going to depend on the operating system. In Windows 7, there is a Public Desktop that populates all user desktops and has fairly open file permissions. It is located at:
c:\Users\Public\Desktop
If that is where you want to put things then your code would be one of these,
ArchiveFolderPath = "c:\Users\Public\Desktop"
ArchiveFolderPath = Environ("PUBLIC") & "\Desktop"
If you wanted to point to the current users desktop then that would be one of these,
ArchiveFolderPath = "c:\Users\" & Environ("USERNAME") & "\Desktop"
ArchiveFolderPath = Environ("USERPROFILE") & "\Desktop"
The environment variable that you are using (`Environ("USERDOMAIN")) typically points to the computer name in a workgroup environment although it would be different in a structured Active Directory network.
Remember to use environment variables whenever possible to avoid variations in hard-coded paths between system operating versions. An XP machine's user profiles are not in the same place as a Windows 7 or Windows 8 machine.
A quick and easy way to get a list of the environment variables in the current session is to open a command prompt window and type SET then hit Enter.

Related

What is the equivalent of this code for MacOS excel?

Can someone advise the equivalent code to use in Excel for Mac that would create the same result as the below does in Windows?
Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
ActiveWorkbook.SaveAs Path & "CAD DATA.xlsx"
Use something like the following function
Function GetDesktopPath() As String
#If Mac Then
GetDesktopPath = Mid(MacScript("tell application ""Finder""" & vbLf & "return desktop as alias" & vbLf & "end tell"), 7)
#Else
GetDesktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
#End If
End Function
in your code to make it work on both Mac and Windows
Path = GetDesktopPath & Application.PathSeparator
ActiveWorkbook.SaveAs Path & "CAD DATA.xlsx"
Make sure ActiveWorkbook is actually what you want to use. You probably meant to use ThisWorkbook:
ActiveWorkbook is the workbook that has the focus (is on top) while this code runs. This can easily change by a simple mouse click or any other interference.
ThisWorkbook is the workbook this code runs at. This is much more reliable because it never changes by any user interaction.
① Source of the MacScript: http://www.vbaexpress.com/forum/showthread.php?54852-Returning-the-Desktop-Path

Excel VBA: How to point to relative directory (name of the file changes a bit everyday)?

The current code is:
Set Workbook = Workbooks.Open("Z:\test\bankinfo20180815.xls")
The file in the folder would change. For example: it was bankinfo20180814.xls yesterday, bankinfo20180815.xls today and bankinfo20180816.xls tomorrow. So I am wondering how I can specify in VBA code to point to the file that starts with "bankinfo"?
Try this:
MyFile = "Z:\test\bankinfo"
Set Workbook = Workbooks.Open(MyFile & "*.xls")
Hope this helps!
You can use a wildcard like *, but if there are multiple files, it can open a wrong file so a better method is to make sure you are opening the exact file.
Sub OpenMyWB()
sdir = "Z:\test\"
sFile = Dir(sdir & "bankinfo" & Format(Date, "yyyymmdd") & ".xls")
Set wb = Application.Workbooks.Open(sdir & sFile)
End Sub

Issue with looping through excel files while one is opened by another user on a shared drive

I created a package that loop through excel files and copy them over to a different folder. However, if one of the is open the package will fail. Any suggestions?
You could add a script task first, something that goes through and closes all files in a particular folder, or a specific file. You could use something like below, it's vbscript but you could choose whatever scripting language you want.
strComputer = "your-server"
strFolder = "Z:\fileshare"
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
Set objConnection = GetObject("WinNT://" & strComputer & "/LanmanServer")
Set colResources = objConnection.Resources
For Each objResource in colResources
strParent = objFSO.GetParentFolderName(objResource.Path)
If LCase(strParent) = LCase(strFolder) Then
objShell.Run "openfiles /disconnect /s " & strComputer & " /id " &_
objResource.Name, 0, False
End If
Next

VBA to search for folder or create it

good afternoon all,
i am using the following code on my spreadsheet to save the file in a specific folder with a specific format:
Const csPath As String = "C:\Stationery Orders\"
MyName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=csPath & Sheets("Stationery").Cells(1, 1) & Format(CStr(Now), "ddmmyyyy_hhmm") & " " & MyName & ".xlsm", FileFormat:=52
my problem is that i can't find a way to create this folder C:\Stationery Orders\ if the folder doesn't exist and also paste a shortcut on the user's desktop. Is that even possible? any ideas?
kind regards
Put a check before doing SaveAs. Something like,
If Dir(csPath, vbDirectory) = "" Then MkDir csPath
Then do the SaveAs
Try this. It will check if folder exists and create it if it doesn't exist.
Sub MyCuteSub()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists("C:\temp\temptemptemp") Then
FSO.CreateFolder ("C:\temp\temptemptemp")
End If
End Sub

How to set a Relative path in Rollup Macro

I am using a Macro to rollup a bunch of excel files in a folder, the path is set like this:
MyPath = "C:\Users\Tim\Desktop\XXX\Compiled"
I need to set a relative path as the folder XXX keeps changing names.
I have found that
Workbooks.Open Filename:=ThisWorkbook.Path & "\Compiled"
will set a relative path, but can not figure out how to apply it to my situation, I have tried
MyPath = Workbooks.Open Filename:=ThisWorkbook.Path & "\Compiled"
but does not work
Thanks
Edit
I figured it out
MyPath = ActiveWorkbook.Path & "\Compiled"
While you have resolved this in terms of the path of the host workbook, it may be useful to note that you can return a relative directory to the Desktop regardless of the OS by using SpecialFolders:
Dim wsShell As Object
Set wsShell = CreateObject("wscript.shell")
strDir = wsShell.SpecialFolders("Desktop") & "\XXX\Compiled"

Resources