Looping through file , temp file interfering - excel

My script would require accessing all excel file in some other folder.
Excel create a temporary file which the script would try to access and return an error.
I dont even know what to look for .
Set System_exp = CreateObject("Scripting.FileSystemObject")
Set folder = System_exp.Getfolder("C:\Sumthing\data") 'Working directory'
Set file_list = folder.Files
For Each file In file_list
Set Workbook = Workbooks.Open(file)
[Some loop/function here]
Next
End Sub
Was't expecting the temporary file .
Run-time error '1004' : Excel cannot open the file '~$Data 1.xlsx' because the file format or file extension not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Apparently it some sort of backup file in case excel crash . What can I do?

You can test the filename:
For Each file In file_list
If Not file.Name Like "~*" Then
Set Workbook = Workbooks.Open(file)
[Some loop/function here]
End If
Next

Related

Looping through a .zip file to open an Excel file, can't get file extension?

I'm trying to loop through a directory with .zip files and pull out the file which is an Excel type file having specific string in the name. I can see the files within .zip having .xlsx extension, but why I try to get this extension name (file_ext) using GetExtensionName, it's empty. Here is a slice of my code:
'Create instance of the shell application
Set obj_app = CreateObject("Shell.Application")
'Get the .zip file
Set obj_file = obj_app.Namespace((str_zip_file)).Items
'Loop through each file within the .zip file
For Each obj_stream In obj_file
str_file = obj_stream.Name
If obj_stream.Type Like "Microsoft Excel Worksheet" And InStr(str_file, "ESG") Then
Set obj_target = obj_app.Namespace(obj_subfolder.Path)
'If it's an Excel file, set the file name to `str_excel_file` and exit the loop
str_excel_file = str_file
obj_target.CopyHere obj_stream
file_ext = LCase(obj_fso.GetExtensionName(obj_subfolder.Path & "\" & str_file))
Exit For
End If
Next obj_stream
Also, when pulled out of .zip into the directory when actual .zip is placed (done by obj_target.CopyHere obj_stream) it appears as being without '.xlsx' extension. What am I doing wrong?

Reference workbook by path and name error: Subscript out of range

I get the "Subscript out of range" error from the line below:
Set WBset = Workbooks("FilePath\PK18 - Unformatted.xls").Worksheets("PK18 - Unformatted")
I checked the sheet name and the filename and they are correct. The sheet exists.
Presuming WBset is a Workbook Object, you need to remove the file path from the command so it looks like the following. The file needs to be open as Workbooks is a collection of the Workbook objects open.
Set WBset = Workbooks("PK18 - Unformatted.xls").Worksheets("PK18 - Unformatted")
wrong workbook name, should be the name not the path

Open a workbook without knowing the exact name

so i searched but didnt find something good for my use , i have a folder where i will import an excel file , this excel file will have a different name everytime how i can open it with vba , thank you
You can get the file name using the Dir function and multiple character (*) wildcard.
Const Path As String = "C:\Test"
Dim filename As String
filename = Dir(Path & "\*.xlsx")
If Len(filename) > 0 Then
' Do your work
' Remember 'filename' only holds the file name
' you will need to attach the rest of the path to get the full directory.
End If
Note: If there's only one file in the folder you will not have any issues, however if the folder contains multiple files (matching the above pattern), you will need to either loop or provide additional file name characters to the function.
An example:
File name: daily_report_20190404.xlsx
filename = Dir(Path & "\daily_report_*.xlsx")
Hope this helps.

How to know numerically the last saved file name

I am creating and saving .ini files in Excel. The files need to follow a specific naming convention that increments by 1 each time a file is created. Not all the files will be created in Excel, some will be done in a separate program. Is there a way to read the saved files in their folder to know which number is next?
For example there are files named exampleFile1.ini through exampleFile63.ini. From Excel using VBA or other means can I see that exampleFile63.ini was the last file and name the next exampleFile64.ini?
Thank you. I'm very new if thats not obvious.
This function will return the next available .INI file name:
Private Function GetNextFile() As String
Dim i As Long
Dim fileName As String
For i = 1 To 1000
' Update Path and File Name prefix below to where your .INI files will be stored.
fileName = "d:\path\exampleFile" & i & ".ini"
If Dir(fileName) = "" Then
GetNextFile = fileName
Exit Function
End If
Next
End Function
Call it like this:
Dim NextIniFile As String
NextIniFile = GetNextFile()

Openfiledialog method alternative in lotus script

Is it possible to get all files (ex : jpg images) from a particular folder with out using openfiledialog method in lotus script? In lotus script, we can give the file path as hard coded.
You can use Dir to get all files from a folder:
Dim pathName As String
Dim fileName As String
pathName = "C:\yourFolder\"
fileName = Dir(pathName + "*.jpg", 0)
Do While fileName <> ""
Print pathName + fileName
fileName = Dir()
Loop
This sample code prints all your jpg files from yourFolder like
C:\yourFolder\a.jpg
C:\yourFolder\b.jpg
C:\yourFolder\c.jpg
From there you can use the list to attach the files to a document or whatever you want to do with the files.
Here you can find the description.

Resources