Copying Files To a Dynamic Folder Using VBA - vba

I have a VBA code to create a folder every day I run it, based on the date. I use the MkDir for this;
MkDir "C:\Users\Stark\Desktop\Automation\Report" & " " & Format$(Date, "dd-mm-yyyy")
Once this folder is created, I have to copy a few files to this folder on which I will have to work. Since this path keeps changing every time I run, I am not sure how to copy files to the newly created folder in such a case. The FileCopy function throws an error if I use the same path as mentioned above.
FileCopy "C:\Users\Stark\Desktop\Templates\RawData.xlsx", "C:\Users\Stark\Desktop\Automation\Report" & " " & Format$(Date, "dd-mm-yyyy")\RawData.xlsx"
This part throws an error when I want to copy the file from Templates folder to the newly created folder.
Kindly help me.

Your code runs well here, if I correct the concatenation at the end of the FileCopy-line:
\RawData.xlsx" must be concatenated properly using a & (like you do in the other places too):
FileCopy "C:\Users\Stark\Desktop\Templates\RawData.xlsx", "C:\Users\Stark\Desktop\Automation\Report" & " " & Format$(Date, "dd-mm-yyyy") & "\RawData.xlsx"

Related

Local copy of word document loaded from shared drive not working with VBA saveas PDF method. Doesn't save the document

I have a macro that works fine in the shared drive when it is only accessed by one/first person. When it is accessed by the second person while the first person still has it open, it says it's already open. When I click "create a local copy and merge changes later" the macro will work up until it saves the file.
The macro basically invokes a userform to collect information, fill in the document, and then it should save the document as a PDF to the desktop. The PDF isn't saving in the local copy version for some reason. When the "Follow hyperlink" comes up, it says "Error 4198, command failed". I check my desktop and the file isn't there, leading me to believe that this error is in relation to the file not being created....
I just need the macro to allow the document to be saved to their desktop as a pdf, whether they're in the normal version, or the local copy created as a byproduct of the shared drive rules.
Main_Form.hide
enviro = CStr(Environ("USERPROFILE"))
sName = Format(Date, "mm-dd-yyyy") & " Denial Letter - Invoice " & Invoice_Text.Value & ".pdf"
sPath = enviro & "\Desktop\"
ThisDocument.SaveAs2 FileName:=sPath & sName, fileformat:=wdFormatPDF
fullName = sPath & sName
ThisDocument.FollowHyperlink fullName
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Use a template (dotm), not a document. Create new documents from the template, that way there will be no document "lock".
Also, don't use ThisDocumentas that means specifically the document in which the VBA code is located, use ActiveDocument, instead.

VB.NET get last changed project date

Good morning,
When I want to show the project name & version I use:
System.Windows.Forms.Application.ProductName
System.Windows.Forms.Application.ProductVersion
Is there a similar thing for the last changed date of a project?
At the moment I have a valid workaround (see below), but I wonder if there is a build in solution like with the ones above.
Dim strFile = Application.StartupPath & "\" & Application.ProductName & ".exe"
Return System.IO.File.GetLastWriteTime(strFile.ToString()).ToShortDateString()

excel vba won't execute .bat but manually executing bat works fine

Hi I have a perfectly working bat named: start.bat
containing:
start C:\Users\*user*\Documents\*path*\hidebat.vbs
and once it is manually opened it works perfectly, meaning it opens hidebat.vbs, which opens a .bat minimized which uploads files to my cloud. Hence it's verified.
I've added
pause
to the start.bat to see what it does and when I tell excel to open the start.bat it will open cmd and display the exact command as required, but it will not execute the hidebat.vbs.
I expect that there is somehow some path constraint or environment constraint when it is run from excel that prevents it to actually reach out of that limited environment.
Within excel I have tried calling the .bat in 3 different ways with:
Dim path As String
path = Application.ActiveWorkbook.path
path = path & "\"
Dim MY_FILENAME3 As String
MY_FILENAME3 = path & "start.bat"
1.
retVal = Shell(MY_FILENAME3, vbNormalFocus)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
2.
PathCrnt = ActiveWorkbook.path
Call Shell(PathCrnt & "start.bat")
3.
Dim batPath As String: batPath = path
Call Shell(Environ$("COMSPEC") & " /c " & batPath & "start.bat", vbNormalFocus)
Does anybody have any clue on why it will not execute the .bat file, or what I could do to ensure it will run correctly?
Note. I think it is because it opens the default path, so I'm gonna tell it to "cd" to the actual path where the excel is saved and where the .bat files are.
Yes that was it, the path was set to some random/standard/working/current path by command, so I had to add:
Print #FileNumber, "cd " & path
to the excel macro
so that start.bat looked like:
cd *path*
start *path*\hidebat.vbs
Hope this helps future me's.

Writing to %USERPROFILE% issue

I have an IO program that I just finished except for one thing. I want the program to automatically detect the User's Profile without hard coding their profile into it. This is what I have now and that works.
My.Computer.FileSystem.WriteAllText("C:\Users\Joshua\Documents\Horoscope\Monthly.txt", Chr(34) & finish & Chr(34), True)
And this is what I want, that doesn't work.
My.Computer.FileSystem.WriteAllText("C:\Users\%USERPROFILE%\Documents\Horoscope\Monthly.txt", Chr(34) & finish & Chr(34), True)
Is there any way to get this to work? The error is telling me that the path doesn't exist. Thanks!
I noticed you're writing to your Documents folder. This folder can be redirected, and so that specific folder name might not even exist for some profiles. You should let the system tell you not only where the user's profile is, but where the user's Documents folder is located, like this:
Dim fileName As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt")
My.Computer.FileSystem.WriteAllText(fileName, Chr(34) & finish & Chr(34), True)
Use this to build your path:
Path.Combine(Environment.GetEnvironmentVariable("userprofile"), "Documents\Horoscope\Monthly.txt")
Documentation:
Path.Combine
Environment.GetEnvironmentVariable

Kill Command Deleting Wrong File(s)i

In Access VBA, I have a procedure I've put together to do this:
Allow the user to select zip file(s)
Extract any files from the zip files to the same directory (In this
specific use-case instance, it is ALWAYS extracting Excel files from
Zip files, never any change, and always using the same password)
Then I want the code to Delete the Zip file after extracting the
.xls file.
Everything works beautifully except the delete file portion. The issue is that when I tell it to delete "FileName.Zip", it is deleting "FileName.Zip" AND "FileName.xls"
Is there any way to make sure that he kill command ONLY deletes what I want it to delete? I've used it before on various occasions, and never had this happen before.
Here is the code I am using:
Dim fd As FileDialog
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tblProjectPath")
Set fd = FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = True
fd.Title = "Select TOC Return File(s) to process"
fd.InitialFileName = rs.Fields("ProjectPath") & "\FilingReports\*.zip"
fd.Show
For Each i In fd.SelectedItems
'Debug.Print i
Debug.Print '------------------------'
Debug.Print i
Unzip (i) 'The bit calling the command line unzip utility to unzip the file - just telling it to extract all files to the current folder.
Debug.Print i
'Kill i
'had to take out the kill bit, b/c it was deleting both the .zip and .xls files which is not desired nor expected
If InStr(i, ".zip") Then
Kill i 'Tried to specify only .zip files even though think I shouldn't need to, but it's still deleting .xls files
End If
Next i
Edit: Add Unzip code to post:
Unzip code:
Sub Unzip(Path As String)
Dim strUnzip As String
Dim QU As String 'quotation mark
QU = Chr(34)
strUnzip = QU & "c:\program files (x86)\winzip\wzunzip" & QU & " -s" & _
"ZipPassword " & _
Path & " " '& _
Call Shell(strUnzip)
End Sub
At this point, I don't really think a "real" answer will come about. However, I'll post what I've decided to do with the particular process I'm writing this code for anyway.
I'm going to use a folder structure to divide up the files:
1. Place zip file(s)
2. Unzip files to a 2nd folder
3. After processing Excel files in 2nd folder, move to a 3rd "complete" folder.
This will get around the deleting wrong files bit.
Also, it appears that the cause for the issue is related to something to do with the call to the WinZip Command Line Unzip utility (wzunzip) in the Unzip code above, or else something with the tool itself. I thought that maybe it was b/c the tool was asking me if I wanted to overwrite existing files, but that wasn't the case, b/c I had the same issue when there were no files to overwrite.
Anyway, I'm attempting to close this one up at this point. Thanks to Wayne G. Dunn for his assistance on this.