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.
Related
The problem is, I want to copy data from .csv file, but excel automatically separates it into columns by comma, I need to separate it by ";".Can I edit csv file using vba code to add 'sep=' at the beginning?
Excel/VBA ignores the separator option if the file has the .csv extension. You have to rename it to set the delimiter. Check out my VBA CSV parser project.
The solution worked for me is to use filesystem object to read csv file and copy it into temporary file with 'sep=' at the first line.
Here is the code:
Function readCsvF(delim as String, fPath as String) As String
Dim sourceFile As Object, objFSO as Object, newTempFile as Object, _
line as String, newName as String
Set objFSO = CreateObject("scripting.FileSystemObject")
Set sourceFile = objFSO.OpenTextFile(fPath)
newName = objFSO.GetParentFolderName(fPath) & "\tempCSVfile.csv"
Set newTempFile = objFSO.CreateTextFile(newName, True)
newTempFile.Writeline("sep=" & delim)
While Not sourceFile.AtEndOfStream
line = sourceFile.Readline
newTempFile.Writeline (line)
Wend
sourceFile.Close
newTempFile.Close
readCsvF = newName
End Function
So what this function does is basically creates new file in which writes first line sep=*'your specified delimiter'* and then copies data from original csv file line by line. This function takes two string parameters: delim is delimiter you want to use and fPath is a path to the csv file, - and returns a path to the new file, so you can open it as workbook and do whatever manipulation you want with it.
Hopefully this will help someone, I really struggled to find the solution, maybe there was any better way, idk.
my problem: I have many text files that I want to rename.
I have been using the ADODB.Stream object to open/read/write the files because they are encoded in UTF-8. So now, if possible, I want to rename the files without the workaround of copying their content, writing their content into a new file with the desired name and deleting the old one. The time stamp on the documents is a valuable information for me, which is why I do not want to create new files.
here is my current workaround that creates new files and deletes the old ones.
Issues with the code:
1) Copied files have new time stamps
2) New Lines don't get copied into the new lines. As they contain some kind of XML code, the generated files become hard to read. I would need to write a piece of code that sets new lines on all appropiate positions after copying.
Sub renameModules()
Dim currentTXT As Variant, newTxt As Variant
Dim currentPath As String, newPath As String
Dim currentContent As String
currentPath = "C:\Users\Me\Desktop\Test\MyCurrent.txt"
newPath = "C:\Users\Me\Desktop\Test\Target001.txt"
Set currentTXT = CreateObject("ADODB.Stream")
currentTXT.Charset = "utf-8"
currentTXT.Open
currentTXT.LoadFromFile (currentPath)
currentContent = currentTXT.ReadText()
Set newTxt = CreateObject("ADODB.Stream")
newTxt.Charset = "utf-8"
newTxt.Open
newTxt.WriteText currentContent
newTxt.savetofile newPath, 1
Kill currentPath
End Sub
For simplicity I have only included the essential steps and omitted all error handling.
My goal: Finding some method to simply rename the current file without fiddling with its content.
No need to deal with ADODB.Stream: You can use the VBA command name not only to rename a file but also to move it to a different folder:
Name currentPath As newPath
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.
I am creating a macro in Excel that should open a PDF document on a specified page with chrome.
Generally, the opening part works. My problem is that when I add the page number (e.g. #page=15) to the url, the shell encodes the "#" symbol into "%23", which Chrome is not able to interpret correctly (file not found).
Here is my code
'The path to the file, replaces spaces with the encoding "%20"
Path = Replace((filePath& "#Page=" & iPageNum), " ", "%20")
Dim wshShell, chromePath As String, shellPath As String
Set wshShell = CreateObject("WScript.Shell")
chromePath = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe\")
shellPath = CStr(chromePath) & " -url " & Path
If Not chromePath = "" Then
'how I first tried it:
Shell (shellPath)
'for testing purposes, led to the same result though:
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")
End If
Is there a different way to do this with Chrome? I haven't found anything that reads the installation path dynamically.
You just need to specify the protocol file:/// if you want to load files from the local hard disk. Then # gets not translated into %23.
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""file:///C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")
If Adobe Acrobat Reader is installed on the system I would suggest to use the funcion openPDF from Daniel Pineault. This will open the file in Adobe Reader directly. You can find the source code of the function here
A test could look like that
Sub TestSO()
Dim fileName As String
Dim pageNo As Long
fileName = "Path and filename of PDF"
pageNo = 20
OpenPDF fileName, 20
End Sub
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()