Rename file where it was extracted - vba

I've got a VBA macro that renames a file. I have the full path where this file exist but I want to rename this file independent from its location.
Private Sub Workbook_Open()
Dim sFileName As String, sNewFileName As String
sFileName = "C:\Users\me\Desktop\text.txt"
sNewFileName = "C:\Users\me\Desktop\test1.txt"
If Dir(sFileName, 16) = "" Then MsgBox "File not found", vbCritical, "Error": Exit Sub
Name sFileName As sNewFileName 'rename file
MsgBox "file has been renamed"
End Sub
I mean if you extract the archive with this Excel file and text.txt file and start it, it will find test.txt and rename it independent from its location.

You are looking for ThisWorkbook.Path which gives you the path of the workbook you are using. So if your txt file is in the same directory you can use something like this:
sFileName = ThisWorkbook.Path & "\text.txt"
sNewFileName = ThisWorkbook.Path & "\test1.txt"

When the excel file location is the same as the text file location, you don't have to write the direction path.
Just write the filename without its direction path:
sFileName = "test.txt"
sNewFileName = "test1.txt"

Related

Copy password from notepad file and use that to open and excel

Need quick help here
I am working on a project where my macro File (Lets call it JP.xlsm) has to open another file which contains user data (User_data file is password protected) and copy data from it to macro file.
But problem is USER_data file is password protected and its dynamic it changes every month and as per our policy we cannot store passwords in macros/scripts. So we are storing the password in a txt file in a folder.
I want that my macro should open the User_admin file and then txt file copies the password from txt file and puts into user admin file.
Please someone help me here
Previously i had written this below code
Sub OpenHardLockedUsers()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\xxxxx"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xls", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
'*************Giving password to open the file*************
Workbooks.Open (MyPath & LatestFile), Password:="d1xl+6/ET3KB"
Dim pw
'...
'...
pw = GetContent("path\To\The\File\pw.txt")
Workbooks.Open (MyPath & LatestFile, Password:=pw)
'...
For reading the file with the password:
'read and return all content from a files
Function GetContent(f As String) As String
GetContent = CreateObject("scripting.filesystemobject"). _
opentextfile(f, 1).readall()
End Function

VBA Open File From This Workbook's Folder Knowing Part of The Name

I am trying to open file from the same folder as the main workbook. The problem is that the name is not permanent and just one word stays always inside the name - "NAME".
I want to use specific method with Thisworkbook.Path to open the xlsx file but it is not finding the workbook with the code.
that is the relavant part of code:
Sub RemoveDuplicats()
Dim Harel As Workbook
Dim SAP As Workbook
Dim Path As String
Dim Found As String
Path = ThisWorkbook.Path
Found = Dir(Path & "*NAME*.xlsx") 'open SAP report
If Found <> "" Then
Set SAP = Workbooks.Open(Path & Found)
End If
End Sub
ThisWorkbook.Path Returns the path without trailing backslash,
try
Found = Dir ( Path & "\" & "*NAME*.xlsx")
You would need to Loop though all Fiels in this Folder and compare the File Names like this:
Dim StrFile As String
StrFile = Dir(ThisWorkbook.Path & "\*" & ".xlsm")
Do While Len(StrFile) > 0
If StrFile Like "*Name*" Then
MsgBox StrFile 'This will be your File
End If
StrFile = Dir
Loop

Copying file from one destination folder to another

I am currently trying to copy a file from one folder to another specified folder using Excel VBA Macro,my data is on Sheet 1 on Excel, I've set my file name to be on cell B5, the source folder is on B6, and the destination folder is on cell B7. This is my code below :
'In this Example I am Copying the File From one loaction to another location
'as per the details specified in the Worksheet.
Sub sbCopyingAFileReadFromSheet()
'Declaration
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
sFile = Sheets("Sheet1").Range("B5")
sSFolder = Sheets("Sheet1").Range("B6")
sDFolder = Sheets("Sheet1").Range("B7")
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FileExists(sSFolder & sFile) Then
MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), sDFolder, True
MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If
End Sub
but a "Specified File Not Found in Source Folder" error message keeps on popping up even though the file is in the source folder. Please assist
when using sSFolder & sFile make sure you have a "\" between the 2 variables, like this
sSFolder & "\" & sFile
Use path separator:
If Not FSO.FileExists(sSFolder & Application.PathSeparator & sFile) Then

Rename File extension from .tmp to .txt

I see there is a filecopy class that is a member of the filesystem but I wanted to just change the file extension. Is the class that will rename the file name?
Using this macro you can rename all .tmp files in a folder to .txt.
Take a look at the VBA Name method:
The Name statement renames a file and moves it to a different
directory or folder, if necessary. Name can move a file across drives,
but it can only rename an existing directory or folder when both
newpathname and oldpathname are located on the same drive. Name cannot
create a new file, directory, or folder.
Code:
Sub rename_files()
Dim MyFolder As String, MyFile As String, NewName As String, i As Integer
MyFolder = "C:\Users\User1\Desktop\test_folder\"
MyFile = Dir(MyFolder & "*.tmp")
Do While MyFile <> ""
i = InStrRev(MyFile, ".")
NewName = Left(MyFile, i - 1) & ".txt"
Name MyFolder & MyFile As MyFolder & NewName
MyFile = Dir
Loop
End Sub

Excel macro to rename the file extension

I am naive to scripts, and I started working with Excel macros. I am looking for a macro to rename only the file extension from *.xml to *.qml. The *.xml file is in the same workbook path. So, I appreciate if anyone could help in this.
Try following code:
Sub RenameFiles()
Dim StrFile As String, newName As String
Dim filePath As Variant
filePath = ActiveWorkbook.Path & "\"
StrFile = Dir(filePath & "*.xml")
Do While Len(StrFile) > 0
newName = Replace(StrFile, ".xml", ".qml")
Name filePath & StrFile As filePath & newName
StrFile = Dir
Loop
End Sub
NOTE: Code will rename all .xml files to .qml so its better to take the back up before executing the code.