wrong number of arguments error while trying to save? - excel

I keep getting a wrong number of arguments or invalid property assignment error when trying to tack on a password:="xxxxxx" at the end of this line of code.
Wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
ValidFileName(Login & " - " & Last & " - " & "Move to $15" & ".xlsx"), Password:="xxxxxx"
What could be causing this? The files saved fine beforehand, but stopped with that error once I tried including a password.

SaveCopyAs doesn't take a password parameter. Only a filename.
Syntax expression. SaveCopyAs( Filename )
expression A variable that represents a Workbook object.
Parameters Name Required/Optional Data type Description
Filename Required Variant Specifies the file name for the copy.

Related

Error when determining whether excel file is already open

I was trying to use the code from the following post to determine whether a different excel file is open:
Detect whether Excel workbook is already open
but my code was errorring out every time. After a bit of testing, I determined it appears to be because I have spaces in the filename/path. Is there a way to allow for the file path to include spaces?
You need to wrap the whole path and filename in double quotes if you have spaces in your file name.
So if you have a vba path & file name of:
WBPath = WB.Path & Application.PathSeparator & WBName
You need to add double quotes to the beginning and end of the string.
WBPath = """" & WB.Path & Application.PathSeparator & WBName & """"
What is """"? (The string starts with a " then the next two " " resolve to be a single double quote of text, then the last " ends the string.)
So you are in essence concatenating a Double Quote to the front of the path\file name and to the end.
Hope that helps. :)

vba excel scrip for attachments failure

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

Save copy of worksheet as CSV with date in the save name

I need to save a copy of the worksheet I'm using as a CSV file.
The save name is to be the date and the user's name. I set up variables to get this information.
I get
Run-time error '1004'; Application-defined or object-defined error
on the line
Activeworkbook.saveAs Filename
Code is:
Sub SaveCSV()
rundate = DateTime.Now
runname = Application.UserName
savename = rundate & runname
sfilename = ActiveWorkbook.Path & "\" & savename & ".csv"
ActiveWorkbook.Save
ActiveWorkbook.SaveAs Filename:=sfilename, FileFormat:=xlCSV
Application.DisplayAlerts = True
End Sub
Value of rundate (based on default regional settings) will be something similar to below:
3/12/2019 10:25:11
of which forward slash (/) & colon (:) are illegal characters as per the file name conventions in Windows.
To resolve this specific issue, use the below code to assign current-time to the rundate variable:
rundate = Format(DateTime.Now, "dd-MMM-YYYY_hh-mm-ss_")
Another good way is to use generic function because we don't always know what illegal characters are creating the trouble. Because there could be more illegal characters in the filename. Above approach is correct but it's not comprehensive list of illegal characters to remove or replace from the filename before saving it. For eg. these characters are missing from the array in your code -> : & . However it is advised to keep filename rid of other allowed special characters too.
Below, I am providing the function which returns a safe string that can be used to produce filename before saving.
Function ReplaceIllegalCharacters(strIn As String, strChar As String) As String
Dim strSpecialChars As String
Dim i As Long
strSpecialChars = "~""#%&*:<>?{|}/\[]" & Chr(10) & Chr(13)
For i = 1 To Len(strSpecialChars)
strIn = Replace(strIn , Mid$(strSpecialChars, i, 1), strChar)
Next
ReplaceIllegalCharacters = strIn
End Function
Specifically, in your code, replace the ActiveWorkbook.SaveAs line with this line:
ActiveWorkbook.SaveAs Filename:= ReplaceIllegalCharacters(sfilename, "_") & _
, FileFormat:=xlCSV, CreateBackup:=False

term being added to a string without instruction to do so

I have a function which sets a variable for the current user. The variable name is prefixed with the name of the module - basically, the way the application is set up, Client is a class, Server is a class, Agency is a class and so on. This is an attempt to create a system whereby we can find out any key information about a client or one of their websites, across over 200 servers, with as few clicks as possible and using live data from their DB.
The function is as follows:
public sub setVariable(varName, varValue)
varValue = cstr(varValue)
def = ""
if varValue = "" then def = "1"
response.write vbcrlf & "Variable: " & varName & " : " & varValue & vbcrlf
if not cstr("" & getVariable(varName, def)) = cstr("" & varValue) then
response.write vbcrlf & varName & " : " & varValue & vbcrlf
prepend varName, "Module." & Name & "."
response.write vbcrlf & varName & " : " & varValue & vbcrlf
session(varName) = varValue
Core.RunSproc "SetUserVariable", array("#name", "#value"), array(varName, cstr(varValue)), setVar
end if
end sub
Now on line 5, where it is first output, only "ID" is output as the variable name. However 2 lines later the name is set to "Module.<module-name>.ID" (for example Module.Server.ID. 2 lines after that, after the prepend statement (which acts the same as doing varName = "something" & varName), it outputs the something + "Module.<module-name>.ID". In effect, in this case, it outputs "Module.Server.Module.Server.ID". Note that if I take anything out, it is taken out of the first Module.Server but not the 2nd. Does anyone have any idea what is causing this? It seems as though Module.Server is being prepended to the variable name between lines 5 & 7, but the only line there is an if statement. Thanks in advance.
Seems I have found the answer. It's the fact that ASP automatically assumes variables input into functions are byref. Thus, when calling getVariable, I was actually prepending "Module.Server." there too, and because it was byref the variable maintained the value.
Regards,
Clarkey

Getting the entire path where a .mdb file is present by providing the database(.mdb) name in excel-vba

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)

Resources