The lines below are an extract from code used to generate a XML.
Access 2010 freezes then crashes when executing the Set dlgSaveAs line. This seems to only happen when data is imported using .xls files, .csv's seem to work fine.
Don't see what's wrong with the code and why it doesn't work with .xls/.xlsx files.
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
With dlgSaveAs
.InitialFileName = strDate & " " & Replace(strTime, ":", "") & " " & strFFI_Filename & " XML Export" & ".xml" 'Set the default filename and directory
.InitialView = msoFileDialogViewDetails 'Set the default folder view
.Title = "Please provide a file name" 'Set your own dialog title
End With
Have you tried doing the following?
With Application.FileDialog(msoFileDialogSaveAs)
.InitialFileName = strDate & " " & Replace(strTime, ":", "") & " " & strFFI_Filename & " XML Export" & ".xml" 'Set the default filename and directory
.InitialView = msoFileDialogViewDetails 'Set the default folder view
.Title = "Please provide a file name" 'Set your own dialog title
End With
Obviously any other code associated with dlgSaveAs would need moving in the With-End With block
Related
I open an Excel file, fill some cells and then save it in a new folder.
The generated files include today's date that includes periods.
If the filename for example is "Template_Name_01.01.2022" the fileformat changes to .2022
Dim OriginalFileName As String
fileName = "Template_" & Nz(rs!Street, "Address") & "_" & Date
OriginalFileName = fileName
Dim fileNumber As Integer
fileNumber = 1
Do Until nameFree = True
nameCheck = Dir("G:\Argus\_Deal Tracker 3.0\Deals_Inv Mgmt\" & fileName)
If nameCheck = "" Then
xlBook.SaveAs fileName:="G:\Argus\_Deal Tracker 3.0\Deals_Inv Mgmt\" & fileName, FileFormat:=xlOpenXMLStrictWorkbook
nameFree = True
Else
fileName = OriginalFileName
fileName = fileName & " (" & fileNumber & ")"
fileNumber = fileNumber + 1
End If
Loop
Even though I determine the fileFormat it saves the file as .2022
Saved files
If I add an ".xlsx" extension to the filename it works for me but not on other PCs, I am guessing it is because they have file extensions hidden.
If they run the function they get this error.
Is there a way to prevent the file format changing if periods appear in the name?
You need to format the Date to remove the forward slashes / from the file name as they're not allowed. You also need to supply the file extension in the path.
So, change this:
fileName = "Template_" & Nz(rs!Street, "Address") & "_" & Date
to this:
fileName = "Template_" & Nz(rs!Street, "Address") & "_" & Format(Date, "dd.mm.yyyy") & ".xlsx" 'change to your extension
Performing the Save As from script, you have to contruct the whole filename, including the extension.
Basically you have to add something like:
fileName = "Template_" & Nz(rs!Street, "Address") & "_" & Date & ".csv"
I have a report in which I allow multiple users to generate their specific version and then save the file but I do not want them to overwrite my master file.
So I created a save-as dialog that saves the file with a specific name (the parameters after .show property are just different parts of the file name).
But the dialog allows users to save the file in the same folder I have the master file by default and I would like to change this to by default offering Documents folder. Is there a way to change this?
Application.Dialogs(xlDialogSaveAs).Show FYandQName & " " & _
CountryName & " " & BusValue & " " & "Financial Narratives"
Try this:
Dim s As String
'assuming an .xlsx file
s = FYandQName & " " & CountryName & " " & BusValue & " " & "Financial Narratives.xlsx"
With Application.FileDialog(msoFileDialogSaveAs)
.InitialFileName = Environ$("USERPROFILE") & "\Documents\" & s
.Show
.Execute
End With
Sub Submit_Click()
Dim wApp As Object
Dim wDoc As Object
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
'Retrieves the word doc template and inserts values from the userform using bookmarks
Set wDoc = wApp.Documents.Open(Filename:="C:\Users\Documents\template1.docx ", ReadOnly:=False)
With wDoc
.Bookmarks("bookmark1").Range.Text = Me.TextBox1.Value
.Bookmarks("bookmark2").Range.Text = Me.TextBox3.Value
.Bookmarks("bookmark3").Range.Text = Me.TextBox4.Value
.Bookmarks("bookmark4").Range.Text = Me.TextBox5.Value
'set the default filename
ProposedFileName = Format(Now(), "DD-MMM-YYYY") & "Serial Number" & " " & TextBox1.Value _
& " " & TextBox2.Value & "- RMA" & ".docx"
'trying to save file back to .doc instead of the default .xlms format
Set fd = Application.FileDialog(msoFileDialogSaveAs)
With fd
.FilterIndex = 2
.InitialFileName = ProposedFileName
If .Show Then
ActiveDocument.SaveAs2 Filename:=.SelectedItems(1), _
FileFormat:=wdFormatDocumentDefault
Else
Call CommandButton4_Click 'cancel save
End If
End With
Set fd = Nothing
End Sub
Hi all,
My script above is only a partial one that is taken from my userform. Basicall the scenario is my userform opens a word document template and inserts texts in the document from the excel userform using bookmarks.
After I click submit on the userform, the filedialog opens with the default .xlms and does not allow me to save it back to .doc
I have been searching and modifying my script for ages and cannot seem to get it right. I would appreciate if someone can tell me how. Thank you.
Regards,
Kev
Private Sub SubmitButton_Click()
'set default file name and file path
ProposedFileName = Format(Now(), "DDMMMYYYY") & " " & TextBox1.Value & "-" & TextBox2.Value & ".doc"
ProposedFilePath = "C:\Users\"
'save the word document called by excel to a .doc format
With wApp.FileDialog(msoFileDialogSaveAs)
wDoc.SaveAs2 ProposedFilePath & ProposedFileName, _
FilterIndex = 1, _
FileFormat:=wdFormatDocument
End With
'unloads the userforms and .doc file after the document is saved
Unload Me
wApp.Quit
'a dialog box pops up after document is saved to say where the file is saved since I was't unable to implement the browse folder option
MsgBox "The document is saved in " & ProposedFilePath, vbOKOnly
Cancel = False
Exit Sub
End Sub
Hi All,
Thank you for the help. I have managed to solve my problem with the above code but unfortunately could not do it with the browse location dialog box. I hope this will become useful for everyone who needs it.
However, if anyone knows how to implement the browse folder location with this code will be better and useful for others.
I am trying to set up an auto save function on an excel template so that the original template isnt over written.
Dim NameFile As String
'Gets the users username
UserName = Environ$("UserName")
'User is to input data type
datat= Application.InputBox("Enter a Data Type", "Data Type")
With Worksheets("Home")
'Sets up auto filename with YearMonthDay - Username - Filename(From a specific Cell) - Data Type
NameFile = Format(Date, "yyyymmd") & " - " & UserName & " - " & Range("A1") & " - " & datat & ".xlsm"
End With
'Sets up save location
NameFile = Application.GetSaveAsFilename(InitialFileName:=Environ("USERPROFILE") & "\Desktop\" & NameFile, Filefilter:=" Excel (*.xlsm), *.xlsm")
If NameFile = False Then
'Tell user with a caution that the file has not been saved
MsgBox "File not saved", vbCritical, "Caution"
Exit Sub
Else
ThisWorkbook.SaveAs Filename:=NameFile
MsgBox "File Saved"
End If
When I dont save the file and click cancel i get the message box telling me that the file hasnt been save... which is what I want.
But when I do save the file with the given name I get a Run-Time error '13', Type Mismatch
What am I doing wrong?
You should Dim NameFile as Variant
That way it can hold either a Boolean or a String.
I have this VBA sub in an Excel 2007 project. It records user name, report name, date, and version on a .txt file in a Sharepoint site. Some of my users are getting a Run-Time error'76' Path not found issue.
Here's my code:
Sub logReport(ReportName As String)
Call AppendTxt("//myaviall/teamsites/AviallReportingSolutions/Airplane_Usage_Log/Airplane_ACT.txt", UNameWindows & ";" & ReportName & ";" & Now & ";" & VersionNum)
Dim oFS, TS, FileObj
'Get text stream
'Set oFS = CreateObject("Scripting.FileSystemObject")
'Set FileObj = oFS.GetFile("//myaviall/teamsites/AviallReportingSolutions/Airplane_Usage_Log/Airplane_ACT.txt")
'Set TS = FileObj.OpenAsTextStream(8, -2) 'ForWriting, TristateUseDefault)
' Write to file
'TS.WriteLine UNameWindows & ";" & ReportName & ";" & Now & ";" & VersionNum
'TS.Close
'Set TS = Nothing
'Set FileObj = Nothing
'Set oFS = Nothing
End Sub
Function AppendTxt(sFile As String, sText As String)
On Error GoTo Err_Handler
Dim FileNumber As Integer
FileNumber = FreeFile ' Get unused file number
Open sFile For Append As #FileNumber ' Connect to the file
Print #FileNumber, sText ' Append our string
Close #FileNumber ' Close the file
Exit_Err_Handler:
Exit Function
Err_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: AppendTxt" & vbCrLf & _
"Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
GoTo Exit_Err_Handler
End Function
If there is only one user that is getting the issue the first thing that I would look at it access, as Scott points out.
I run a very similar procedure which writes a line to a csv file from outlook based on the code here
http://www.devhut.net/2011/06/06/vba-append-text-to-a-text-file/
This is probably not your answer but if the access is ok then it couldnt hurt to try another method.
Update
I also include this into my code to test if the file exists
exists = Dir$(sFile) <> fileName
Where fileName = "Airplane_ACT.txt" But I would maybe try this with a msgBox to see what it returns.
Also try try changing your string to Airplane_ACT_test.txt and run the code, this should create a new txt file, if this is the case then the issue may be related to your initial txt file.
Last thing: try with a different path eg: to the user's desktop.