How can i define the file path when opening workbook? - excel

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.

Related

SSIS won't execute foreach loop for dynamic xlsx filename [duplicate]

This question already has answers here:
SSIS - How to loop through files in folder and get path+file names and finally execute stored Procedure with parameter as Path + Filename
(2 answers)
Closed 3 years ago.
I have a xlsx file that will be dropped into a folder on a monthly basis. The filename will change every month (filename_8292019) based on the date, to which I cannot change.
I want to build a foreach loop to pick up the xlsx file and manipulate it (load into SQL server table, the move the file to an archive folder). I cannot figure out how to do this with a dynamic filename (where the date changes.
I was able to successfully run the package when converting the xlsx to CSV, and also when pointing directly to the xlsx filename.
[Flat File Destination [219]] Error: Cannot open the datafile "filename"
OR errors relating to file not found
The Files: entry on the Collection tab of the Foreach Loop container will accept wildcard characters.
The general pattern here is to create a variable, say, FileName. Set your Files: to something like:
Files:
BaseFileName*
or, if you want to be sure to only pick up spreadsheets, maybe:
Files:
BaseFileName*.xlsx
Select either Name and extension or Fully qualified, which will include the full file path. I usually just use Name and extension and put the file path into another variable so when Ops tells me they're moving my drop location, I can change a parameter instead of editing the package. This step tells the container to remember the name of the file it just found so you can use it later for a variable mapping.
On the Variable Mappings tab, select your variable name and assign it to Index 0.
Then, for each spreadsheet, the container will loop, pick up the name of the first file it finds that matches your pattern, and assign the full name, with the date extension (and path, if you go that way), to your variable. Pass the variable as in input parameter to the tasks inside the loop and use that to process the file, including moving it to the archive, or you'll get yourself into an infinite loop, processing the same file(s) over and over. <--Does that sound like the voice of experience? Yeah. Been there, done that.
Edit:
Here, the FullFilePath variable is just the folder name, without a file reference. (Red variable to red entry in the Folder box).
The FileBaseName variable drives what shows up in the Files box. (Blue to blue).
Another variable picks up the actual file name, with the date extension. Later, say in a File System Task, if I need the folder & file name together, I concatenate the variables.
As far as the Excel Connection Manager error you're getting, unfortunately I'm no help. I don't use it. We have SentryOne's Task Factory for SSIS which includes a much more resilient Excel connector.

How to open Locked for Editing file as Read Only?

I have a macro that opens multiple files. If it comes to a file "Locked for Editing" it will give me an error saying
FileName is currently in use. Try again later.
How can I make it open said file as read only? I tried:
Workbooks.Open FileName:=Selected_EOS_Report_File, ReadOnly:=True
and
Workbooks.Open FileName:=Selected_EOS_Report_File, ReadOnly:=True, IgnoreReadOnlyRecommended:=True
Update: The first method does work. My code runs on multiple files that pass through the "Selected_EOS_Report_File" variable. At some point a file passed through that was an Excel temp file (begins the filename with "~$"). I created an if/then statement to skip over any such files.
As far as I know, you need Notify:= True
MSDN link
Notify
If the file cannot be opened in read/write mode, this argument is True
to add the file to the file notification list. Microsoft Excel will
open the file as read-only, poll the file notification list, and then
notify the user when the file becomes available. If this argument is
False or omitted, no notification is requested, and any attempts to
open an unavailable file will fail.
The code below worked for a similar Problem I had. This will set the ReadOnly and IgnoreReadOnlyRecommended parameters.
I tested this for Excel 365.
ReadOnly: True to open the workbook in read-only mode.
IgnoreReadOnlyRecommended: True to have Microsoft Excel not display the read-only recommended message (if the workbook was saved with the Read-Only Recommended option).
dim wbReadOnly as Workbook
Set wbReadOnly = Workbooks.Open(strXLSFileName, , True, , , , True)
link to VBA Documentation
Try this?
Dim wb As Workbook
Set wb = GetObject(Selected_EOS_Report_File)
wb.Open 'ReadOnly:=True (removed the readonly part)
Derived from this post: Opening .xlsx with VBA, File in Use error. Read-only not working
edit
A post here indicates a similar issue for older versions, and that if you undate to xlsx then it goes away:
https://social.technet.microsoft.com/Forums/en-US/5c9f7444-a2c7-4598-beca-21a6d5575d94/excel-file-currently-in-use

Get variables from .xlam to a .xlsb

Recently I discovered about .xlam files to reuse functions that I use in several files. I'm not a programmer so this is maybe something easy but I've been hours into this and I can't find the way to solve it.
I've just created a project with some of the functions that I used most and I saved the file in a Sharepoint site. After that I created a Reference and everything seems to be working well. I can Call functions just with a
call FunctionName()
And the functions are running well. But I haven't found the way to return global variables from the .xlam file to my .xlsb file
I'm defining a Global variable just like this in a Module in the .xlam
Global CRegion As String
In the same module I have this easy macro:
Function GetRegion()
CRegion = Range("B2").Value
End Function
And then in my .xlsb file Im testing something like this after calling the GetRegion Function.
Msgbox CRegion
The result is an empty Message Box. I've tried to define a global variable with the same name in my .xlsb file and it still does not work.
¿Any clue to solve this?
Have not tested it, but try:
To make a public variable available to other workbooks, from a new workbook select the workbook containing the public variable in the Available References box of the References dialog box (from a module sheet, click References on the Tools menu).
Source: MSDN
http://support.microsoft.com/kb/141693

Relative paths links in 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

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