enter image description hereenter image description hereI have just tried it with the modified code but unlucky it is still not working. So let me clearify my question further:
I have the pdf file
Risikomanagement-Report Pfandbrief Hypothekenpfandbrief 20180706.pdf
in the Folder:
T:\30_Deckungsstock\Deckungsnachweise\tgl.RMR
My aim is to grap this typ of file (with the current date) and attache it to an E-Mail.
My code for this goes as following:
.Attachments.Add "T:\30_Deckungsstock\Deckungsnachweise\tgl.RMR\Risikomanagement-Report Pfandbrief Hypothekenpfandbrief" & Format(datDatum, "YYYYMMDD") & ".pdf"
Issue: It can't find the file. I am pretty sure there is a small bug which I couldnt spot.
The msgbox states: the file cannot be found. Please check the path and the name of the file
Edit: The file exists:enter image description here
Try
Attachments.Add "T:\30_Deckungsstock\Deckungsnachweise\tgl.RMR\Risikomanagement-Report Pfandbrief Hypothekenpfandbrief " & Format$(datDatum, "YYYYMMDD") & ".pdf"
Note there is a space before the date part.
Edit:
Use
debug.print "T:\30_Deckungsstock\Deckungsnachweise\tgl.RMR\Risikomanagement-Report Pfandbrief Hypothekenpfandbrief " & Format(datDatum, "YYYYMMDD") & ".pdf"
to get the filepath being used and verify whether correct.
The current error is because you are not assigning a value to datDatum before using it. The default initialized value is being used.
If you don't assign datDatum an initial value it will be initialized as 00:00:00. When you apply the Format function it gets converted to 18991230
You can verify with the following:
Option Explicit
Public Sub test()
Dim datDatum As Date
MsgBox Format$(datDatum, "YYYYMMDD")
End Sub
You will see 18991230 appear.
Solution is to assign a value to datDatum e.g.
datDatum = Now
Related
I'm writing some VBA code that should go through all Excel files in a specific folder (folder names always formatted with Month Year, e.g. May 2020). In my code I also need to use the individual "Month" and "Year" strings e.g. "May" and "2020", and the date format mm/??/yy e.g. 5/??/20 (the day doesn't matter, so I just put ? as a placeholder) which are stored as variables.
So far, I am using Application.FileDialog(msoFileDialogFolderPicker) to let the user choose the folder, and I'm using InputBox("") three times to get the strings and date.
Is there a way to condense this so that the user only has to do one to two things, instead of four?
According to this answer combo box in a date format it seems like a combo box could work (maybe getting the month and year inputs as strings and getting the folder and date based on that?), but is there a better a way?
Any help would be appreciated!
This is the way it might work.
Ask the user for a date
From the date the macro creates the folder name
The path of the folder is stored in the macro
The code below implements and supports that work flow.
Sub GetFolderName()
Dim Inp As String
Dim FolderName As String
Dim FilePath As String
' make sure you ask for a date format that your computer can recognise
' it depends upon your Regional Settings (in Windows Control Panel)
Do
Inp = InputBox("Enter a date", "Date format dd/mm/yy", _
Format(Date, "dd/mm/yy"))
If Inp = "" Then Exit Sub ' blank or Cancel
If Not IsDate(Inp) Then
MsgBox "Sorry, I can't recognise your entry" & vbCr & _
"as a date. Please observe the date" & vbCr & _
"format requirement and try again."
End If
Loop While Not IsDate(Inp)
FilePath = Environ("userprofile") & "\Desktop\"
FolderName = Format(CDate(Inp), "mmmm yyyy")
MsgBox "Folder name is """ & FolderName & """" & vbCr & _
"File path = " & FilePath
' complete path is
Debug.Print FilePath & FolderName
End Sub
Note that the following options were not utilized but are still available.
Day(Cdate(Inp)) ' returns the day of the entered date
Month(Cdate(Inp)) ' returns the number of the month of the entered date
Year(Cdate(Inp)) ' returns the year of the entered date (45-digit)
I would like to save my file with the flexible name, which will change as the cell value changes.
The one answer is here:
Save a file with a name that corresponds to a cell value
however, I want also some fixed part of the name, which won't change unlike the part described in the query above.
Basing on this solution I tried to write something as follows:
Sub Save ()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = "NBU" & name & "- Opportunity list.xlsx"
ActiveWorkbook.SaveAs Filename:=Custom_Name
In the effect, I am getting an error:
This extension cannot be used with the selected file type. Change the file extension in the File name text box or select a different type file by changing the Save as type.
I would like to have this file in the .xlsx extension.
Excel VBA - save as with .xlsx extension
The answer above doesn't really match to my situation.
It will be vital to remove all form control buttons in the newly saved file, when possible.
Thanks & Regards,
End Sub
There is no action in the routine listed to save the file. It just simply takes the contents of a cell and creates a string with wrapped values.
I am not totally sure of what your goal is, but you need to add the action from the second link you provided. Workbook.SaveAs Method.
See the code below for a working example that I created to test.
Public Sub Save()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = ThisWorkbook.Path & "\" & "NBU" & name & " - Opportunity list.xlsx"
'Disable alert when saving
Application.DisplayAlerts = False
'Save the workbook.
ActiveWorkbook.SaveAs Filename:=Custom_Name, FileFormat:=51
End Sub
You should note that after this code has executed, you will now be in the newly created file. This is not an export.
Test this and let me know if you have any questions. There are a few things that seem to be unnecessary in your code, but we can address those if you find this answers your first issue.
Edit:
I would also call out specifically then worksheet with the range as well.
name = Worksheets("Sheet1").Range("A2")
You said the file name is 'Opportunity v1.0.xslm'. Would it have macros? My version of Excel complains about using .xlsx if there is code in the workbook.
Please help. I am pretty new. I am trying to create a folder using dates and got a compile error message "wrong number of arguments or invalid property assignments". The code worked perfectly for a month but not anymore.
Sub Test()
MkDir "\\AD.TEST.COM\nas\Team-AB-TEST\TEST FOLDER\TEST FOLDER \" & Format(Date, "yyyy") & " Email Tracking\" & Format(Date, "mm.dd.yy") & "\Attach\"
End Sub
Your code is compiling perfectly.
But when you add this next to your code:
Function Format(s As String)
Format = "tralala"
End Function
Then, it does not work anymore.
Can you verify you don't have some Format() function defined somewhere?
How can I get the entire path where a .mdb file is present by providing the database(.mdb) name in excel-vba.
When I install an application in my system, a database (.mdb file) will be created in the installation path. I want to take some data from that database(.mdb file) and use it in an excel file.
This would path would be different in different systems. I want my excel-vba code to automatically look for the database (.mdb file) and take the data from the database.
ActiveWorkbook.Path is where the current excel worksheet is found. assuming your database name does not change, then using this value & "MyDatabase.mdb" should find your database
May i suggest a different approach, as I understand the question in a the way, that it is necessary to find the .mdb file first - there is only the name provided.
In that case, you will find help in the following article - including some working code ;)
Microsoft KB185476
And for the part with the full-path:
Sub ShowFileAccessInfo(filespec)
Dim fs, d, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
s = UCase(f.Path) & vbCrLf
s = s & "Created: " & f.DateCreated & vbCrLf
s = s & "Last Accessed: " & f.DateLastAccessed & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
MsgBox s, 0, "File Access Info"
End Sub
*from excel-help (file-object)
I have an .xltm template spreadsheet that I'm wondering if I can get a macro to populate the "save as" file name based on cell data, is this possible?
There are over 50 people who will have this spreadsheet, it's more of a form, and we are trying to figure out a way to keep the filenames uniform. I know there is the ThisWorkbook.BeforeSave, but I'm not really having any luck there. I just need it to make a file named something like $A$1 R $B$1 T $B$3.xlsx
Any ideas on how to do this?
Sure.
Sub SaveMyWorkbook()
Dim strPath As String
Dim strFolderPath as String
strFolderPath = "C:\"
strPath = strFolderPath & _
Sheet1.Range("A1").Value & "R" & _
Sheet1.Range("B1").Value & "T" & _
Sheet1.Range("B3").Value & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strPath
End Sub
EDIT: After you clarified your question in your comment below, I can now safely say that the answer is: No, what you are asking is not possible.
What is possible is to put a big, fat command button on your sheet that says "Press me to save", and have that button call the above Sub. You can set a fixed folder, as in the example above, or have the user pick a folder using the FileDialog object (or the GetSaveAsFilename function, but then the user will be able to change the suggested filename, so less safe).