Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

excel VBA save as question

  • 16-09-2011 10:05am
    #1
    Registered Users, Registered Users 2 Posts: 1,740 ✭✭✭


    I have a macro in Excel that opens a file, does some work on it and converts it to a txt file. basically the user clicks a button, it asks them to select the source file and then the save destination for the txt file. I want to get rid of the steps to select a save location, i want to use teh same location all the time and overwrite teh text file.


    If MsgBox("Do you want to save the output file?", vbYesNo, "Save CSV file") = vbYes Then

    fname = Application.GetSaveAsFilename( _
    InitialFileName:="b:\Share\\Text_Files\file.txt", _
    FileFilter:="Text Files, *.txt", _
    Title:="Save file as txt")

    If Len(fname) <> 0 Then
    csvbook.SaveAs fname, FileFormat:=xlCSV
    csvbook.Close False
    frombook.Close

    End If

    the above will open the save as dialog box, give the file a default name of file.txt in the path i require. I want to basically suppress this dialog box so that teh file name is always the same and in the same path every time, automatically overwriting each time its run. anyone any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 1,456 ✭✭✭FSL


    If you do not want to give the user the option of choosing the name and location then you don't need a file save dialog box. The destination name and location is the same as the initial name and location.

    Unless I am completely misunderstanding your question I can not see what your problem is.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Use export instead of save.


  • Registered Users, Registered Users 2 Posts: 1,740 ✭✭✭Faolchu


    FSL wrote: »
    If you do not want to give the user the option of choosing the name and location then you don't need a file save dialog box. The destination name and location is the same as the initial name and location.

    .
    the destination file name & path will be different to the source name each time.

    basically its we have an excel sheet for each month of teh year so you have a folder for Month, then a sub folder for year, then teh excel file. so each month the source location changes. We then have a seperate application that needs to import some of the data in the excel sheet.

    So the easiest solution would be to create a macro that copies teh required info into a seperate file (in this case a csv file) that has the data in the right order and then import that data into our database. thats why the destination name & path stays static so I dont have to modify a SQL script each month before the users carry out their task.

    so each month the users will select a different source spreadsheet, but the file created will always be in teh same location. So they need to be able to select the source each time but not the destination.


    the full code is below

    Private Sub CommandButton1_Click()
    ' execute import

    Dim frombook As Workbook, csvbook As Workbook
    Dim fromfilename As String
    fromfilename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")


    ' Set frombook = Workbooks.Open(thissheet.Cells(2, 2).Value & thissheet.Cells(3, 2).Value, , True)

    If Len(fromfilename) <> 0 Then ' fromfilename is a nul string if canceled or no file selected

    Set frombook = Workbooks.Open(fromfilename, , True)
    Set csvbook = Workbooks.Add ' create the ouput file

    Call Make _csv(frombook, csvbook)

    If MsgBox("Do you want to save the output file?", vbYesNo, "Save CSV file") = vbYes Then

    fname = Application.GetSaveAsFilename( _
    InitialFileName:="b:\Share\ Text_Files\file.txt", _
    FileFilter:="Text Files, *.txt", _
    Title:="Save file as txt")


    If Len(fname) <> 0 Then
    csvbook.SaveAs fname, FileFormat:=xlCSV
    csvbook.Close True
    frombook.Close

    End If
    Else
    MsgBox "The files remain open - if saving the output file later make sure to name the file with an extesion of txt.", vbOKOnly
    End If ' msgbox


    End If 'fromfilename <> 0

    End Sub


  • Closed Accounts Posts: 577 ✭✭✭Galtee


    Faolchu wrote: »
    the destination file name & path will be different to the source name each time.

    basically its we have an excel sheet for each month of teh year so you have a folder for Month, then a sub folder for year, then teh excel file. so each month the source location changes. We then have a seperate application that needs to import some of the data in the excel sheet.

    So the easiest solution would be to create a macro that copies teh required info into a seperate file (in this case a csv file) that has the data in the right order and then import that data into our database. thats why the destination name & path stays static so I dont have to modify a SQL script each month before the users carry out their task.

    so each month the users will select a different source spreadsheet, but the file created will always be in teh same location. So they need to be able to select the source each time but not the destination.


    the full code is below

    Private Sub CommandButton1_Click()
    ' execute import

    Dim frombook As Workbook, csvbook As Workbook
    Dim fromfilename As String
    fromfilename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")


    ' Set frombook = Workbooks.Open(thissheet.Cells(2, 2).Value & thissheet.Cells(3, 2).Value, , True)

    If Len(fromfilename) <> 0 Then ' fromfilename is a nul string if canceled or no file selected

    Set frombook = Workbooks.Open(fromfilename, , True)
    Set csvbook = Workbooks.Add ' create the ouput file

    Call Make _csv(frombook, csvbook)

    If MsgBox("Do you want to save the output file?", vbYesNo, "Save CSV file") = vbYes Then

    fname = Application.GetSaveAsFilename( _
    InitialFileName:="b:\Share\ Text_Files\file.txt", _
    FileFilter:="Text Files, *.txt", _
    Title:="Save file as txt")


    If Len(fname) <> 0 Then
    csvbook.SaveAs fname, FileFormat:=xlCSV
    csvbook.Close True
    frombook.Close

    End If
    Else
    MsgBox "The files remain open - if saving the output file later make sure to name the file with an extesion of txt.", vbOKOnly
    End If ' msgbox


    End If 'fromfilename <> 0

    End Sub

    So you want to remove the

    fname = Application.GetSaveAsFilename( _
    InitialFileName:="b:\Share\ Text_Files\file.txt", _
    FileFilter:="Text Files, *.txt", _
    Title:="Save file as txt")

    section altogether? and replace it with

    fname = "b:\Share\Text_Files\yourFileName.txt"?


  • Registered Users, Registered Users 2 Posts: 1,740 ✭✭✭Faolchu


    Galtee wrote: »
    So you want to remove the

    fname = Application.GetSaveAsFilename( _
    InitialFileName:="b:\Share\ Text_Files\file.txt", _
    FileFilter:="Text Files, *.txt", _
    Title:="Save file as txt")

    section altogether? and replace it with

    fname = "b:\Share\Text_Files\yourFileName.txt"?


    spot on that worked a treat thanks.


  • Advertisement
Advertisement