Relative paths links in excel - excel

I have an excel file with some email links. I would like to have a clickable link to download those files.
Having the two files (the excel and the email) in the same folder, the link file://test.eml or only test.eml does not open it, I need to use an absolute path. There would be a way to do this?
Thanks
EDIT
The absolute path is a known value, but anyway, the file is not opened.
=HYPERLINK("C:\Users\blanca.hernandez\Desktop\2012-05-24T09-41-11-0.00007.eml", "email")
It is funny, because something like
file://C:\Users\blanca.hernandez\Desktop\2012-05-24T09-41-11-0.00007.eml
gives me a clickabe link to open the file, but if I want to do the same with other directory, as
file://C:\repository\project\mail\archive\2012\5\24\11\2012-05-24T09-41-11-0.00007.eml
it is not possible to open. Are there some reasons for that?

You can use the Hyperlink(LinkLocation, FriendlyName") function in conjunction with Cell("filename")
Cell("filename") returns the full file name of the file that contains the reference as
C:\Full\Path\[FileName.xlsm]SheetName
To get just the path use
LEFT(CELL("filename"),FIND("*", SUBSTITUTE(CELL("filename"), "\", "*", LEN(CELL("filename")) - LEN(SUBSTITUTE(CELL("filename"),"\","")))))
Hyperlink(LinkLocation, FriendlyName") creates a hyperlink
To link to the file test.eml the full formula is
=HYPERLINK(LEFT(CELL("filename"),FIND("*",SUBSTITUTE(CELL("filename"),"\","*",LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"\","")))))&"test.eml","Link to File")

MyPath = ActiveWorkbook.Path
that will give you the path to the workbook

Related

'Save As' of object '_workbook' failed

I am trying to save a workbook as xlsm, this is the code I use:
ThisWorkbook.SaveAs Filename:="path\workbook_name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
I have done some testing, and It does in fact save in both a private folder and a public or shared folder
however when I save it in a shared folder I also get this:
Run-time error '1004' Method 'Save As' of object '_workbook' failed
why do I get error message when it is saved in a public/shared folder, or onedrive?
In your string containing path there is variable "path" i suppose. Then you should write it like this:
Dim path as String: path = "C:\Users\xxx\Desktop"
ThisWorkbook.SaveAs Filename:=path & "\workbook_name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Or you can hardcode full path (but i recommend using variables - first example).
ThisWorkbook.SaveAs Filename:="C:\Users\xxx\Desktop\file.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Then when you can save it manually and you have permissions then i have no idea since i normally save files to shared folders and network drives... Maybe your specific path is wrong. It is best to save using servername and folders...
path = "\\servername\folder1\folder2\workbook_name.xlsm"
Because sometimes when you refefer to mapped drive for example X:\folder2 then you must ensure that all users have it mapped in same way (someone uses X someone uses R etc.). And if path is correct then only other way i can think of it is permission issue, but if you can save it manually then i have sadly no idea... :)

How can i define the file path when opening workbook?

I have a macro that opens a workbook (each time a different one) and imports data from it. But when I activate it, the standard path is to my documents. How can i define a file path to a specific folder?
If you're looking for a way to change the default directory for choosing a file you would use:
ChDir (ThisWorkbook.Path)
However, if you're looking for some way to open a specific filepath, you would have to use a FilePickerDialog, assign a String to that user input, then use something like:
[StringVarName] = Application.GetOpenFilename
If you post your code with your attempt at fixing the issue, we would be able to better assist.

Excel VBA Dir() default path?

I am working through an exercise that uses Dir() to find out whether a file exists in the current directory (i.e. the same directory as the workbook I'm using). The code given - and apparently working in the video example - is like this:
IsThere = (Dir("SomeFile.xlsx") <> "")
When I run this code, IsThere returns False. I can work around it by using:
IsThere = (Dir(ActiveWorkbook.Path & "\SomeFile.xlsx") <> "")
but I want to know why Dir isn't looking by default in the current directory as expected.
I'm struggling to find any relevant advice on this. Most of the examples I've found of how to use Dir() are with the file path specified so they don't really shed any light on my problem. The closest I've found is this (obsolete) MSDN reference which says that:
To run correctly, the Dir function requires the Read and PathDiscovery flags of FileIOPermission to be granted to the executing code.
Trouble is, I don't really understand the linked advice in there on how to set PathDiscovery to 1.
As for StackOverflow, this is probably the closest to my problem - although this uses a specified path, and I am not referencing a network location. I note that the answer to this question seems to presume that Dir() should work in the way expected i.e. with a simple filename and not a fully specified path.
This has nothing to do with whatever the host application thinks it's directory is. You can always find out what directory Dir will default to by calling the CurDir function:
Debug.Print CurDir$
If you need to change it, call ChDir:
Debug.Print CurDir$
Debug.Print Dir$("*.*")
ChDir "C:\"
Debug.Print CurDir$
Debug.Print Dir$("*.*")
Think of it like sitting at a command prompt and typing dir or cd - it does exactly the same thing. Note that the current directory holds it's state between macro executions, so you can't rely on it being in some default location.
If you need a path relative to an open Workbook, use Workbook.Path if you need a path relative to the default path, use Application.Path. If you need to test for the existence of a file, don't use Dir at all - use the Scripting.FileSystemObject instead. You'll do yourself a huge favor if start thinking of the legacy file functions as deprecated.
Check the Excel Options --> Save --> Default File location to see what your default is. It won't by default use where the file is located.

How to find a .zip file in the current directory using Groovy?

I have a .zip file in the current directory, I want to get its file name using Groovy. e.g. if the file is myfile.zip, I want to get the "myfile" part. Can anyone give me a code snip? Thanks.
Something like this should work:
filename=new File("directory").listFiles().find{it.name.endsWith(".zip")}
If you don't want the .zip on the end, subtract it:
filename=new File("directory").listFiles().find{it.name.endsWith(".zip")}.name - ".zip"
(By the way, the first one ends up with a file object--you can do whatever you want with it. The second ends up with the string that is the name without the .zip)

VBA - How to get the last modified file or folder in a directory in Excel 2010

What I want to do is more complex than selecting a file from a list of files. I will start in a directory, then I want to change to the most recently modified directory. I then want to repeat that process in a sub-directory, and then, inside of that, I want to select the most recently modified excel file and open it.
What is the best approach to do this?
What objects / methods should I be looking into?
The simplest function is
FileDateTime(pathname)
where pathname can be a directory for folder.
Alternatively, you can use the FileSystemObject object, DateLastModified property:
Dim fileModDate As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(<filenamestringhere>)
fileModDate = f.DateLastModified
All of the above can be explored in VBA help.

Resources