Djblois
12-11-2007, 01:30 PM
I have code that I use througout my add-in to bring up a save as dialog. However, I am trying to add it so it suggests a file name but I can't figure out how to do that part. Here is the code that I am using:
Sub SaveCode(Optional ByVal stgDirectory As String = "H:")
Dim wbFileName As String, stgSelectedDirectory As String, blnShowAlert As Boolean
On Error GoTo SaveNoDirectory
ChDrive "H:"
ChDir stgDirectory 'Change Directory to the users default directory
SaveNoDirectory:
'If the user hasn't set a Default Directory
wbFileName = Application.GetSaveAsFilename
On Error GoTo SaveBook
If wbFileName = "False" Then
blnShowAlert = GetSetting("Business Reporting Today", "General", "Alert-Save", True)
If blnShowAlert = True Then
frmErrorAlert.Show
If frmErrorAlert.cbDontShow Then
SaveSetting "Business Reporting Today", "General", "Alert-Save", False
Else
SaveSetting "Business Reporting Today", "General", "Alert-Save", True
End If
Unload frmErrorAlert
End If
errUpdateLogFile "SaveCode-NoSave"
Exit Sub
End If
SaveBook:
'Get the User specified directory
stgSelectedDirectory = Left(wbFileName, InStrRev(wbFileName, "\") - 1)
'Check if Directory the user is trying to save in is Read-Only - this part still
'Doesn't work
If GetAttr(stgSelectedDirectory) And vbReadOnly Then
MsgBox "Test"
End If
'If the user didn't type the file extension, this will add it
If Right(wbFileName, 5) = ".xlsx" Then
ActiveWorkbook.SaveAs wbFileName, FileFormat:=xlNormal
Else
ActiveWorkbook.SaveAs wbFileName & ".xlsx", FileFormat:=xlNormal
End If
End Sub
Sub SaveCode(Optional ByVal stgDirectory As String = "H:")
Dim wbFileName As String, stgSelectedDirectory As String, blnShowAlert As Boolean
On Error GoTo SaveNoDirectory
ChDrive "H:"
ChDir stgDirectory 'Change Directory to the users default directory
SaveNoDirectory:
'If the user hasn't set a Default Directory
wbFileName = Application.GetSaveAsFilename
On Error GoTo SaveBook
If wbFileName = "False" Then
blnShowAlert = GetSetting("Business Reporting Today", "General", "Alert-Save", True)
If blnShowAlert = True Then
frmErrorAlert.Show
If frmErrorAlert.cbDontShow Then
SaveSetting "Business Reporting Today", "General", "Alert-Save", False
Else
SaveSetting "Business Reporting Today", "General", "Alert-Save", True
End If
Unload frmErrorAlert
End If
errUpdateLogFile "SaveCode-NoSave"
Exit Sub
End If
SaveBook:
'Get the User specified directory
stgSelectedDirectory = Left(wbFileName, InStrRev(wbFileName, "\") - 1)
'Check if Directory the user is trying to save in is Read-Only - this part still
'Doesn't work
If GetAttr(stgSelectedDirectory) And vbReadOnly Then
MsgBox "Test"
End If
'If the user didn't type the file extension, this will add it
If Right(wbFileName, 5) = ".xlsx" Then
ActiveWorkbook.SaveAs wbFileName, FileFormat:=xlNormal
Else
ActiveWorkbook.SaveAs wbFileName & ".xlsx", FileFormat:=xlNormal
End If
End Sub