• File conversion function (2003 SP2)

    Author
    Topic
    #430671

    Reports are generated from our mainframe in text format and sent to various recipients. The layout of the reports is in a spreadsheet so to facilitate opening of the file in Excel they are given the xls extension. Each recipient modifys their report and saves it in a folder where I grab it for further processing. When they save their file, some save it as an Excel workbook, while others just save it so it remains in text format. I have to import the data from these files into Access so I created a Function to convert the files to Excel format, but of course it doesn’t work. When I step through the code each variable changes with each loop as expected, and I get no error messages. What am I missing?

    Dim RS As DAO.Recordset, DB As DAO.Database
    Dim strFileName As String
    Dim xlObj As Excel.Application
    Dim xlWbk As Excel.Workbook
    Set DB = CurrentDb()
    Set RS = DB.OpenRecordset(“tblFileNames”)
    On Error Resume Next
    RS.MoveFirst
    Do Until RS.EOF
    strFileName = RS(“Folder”) & “” & RS(“FileNames”)
    Set xlObj = CreateObject(“Excel.Application”)
    Set xlWbk = xlObj.Workbooks.Open(strFileName)
    ActiveWorkbook.SaveAs FileName:= _
    strFileName, FileFormat:=xlNormal, Password:=””, WriteResPassword:=””, _
    ReadOnlyRecommended:=False, CreateBackup:=False
    RS.MoveNext
    xlWbk.Close SaveChanges:=True
    Set xlWbk = Nothing
    xlObj.Quit
    Set xlObj = Nothing
    Loop
    RS.Close

    Viewing 1 reply thread
    Author
    Replies
    • #1006299

      Hi,
      You get no error messages because you have an on error resume next statement in there. I suspect your problem is down to your use of an unqualified ActiveWorkbook variable. Try changing ActiveWorkbook.SaveAs to xlWbk.SaveAs
      HTH

    • #1006300

      Referring to ActiveWorkbook without specifying that it belongs to xlObj is dangerous – it may cause a second instance of Excel to be started that is not controlled by your application. Try replacing ActiveWorkbook with xlWbk.

      • #1006306

        Works every time now. Is there a way to turn off the warnings? Since I’m not renaming the files I get the “file already exists” warning for each file as it is saved.

        • #1006311

          You can insert a line

          xlObj.DisplayAlerts = False

          before the loop, and

          xlObj.DisplayAlerts = True

          after it.

          • #1006316

            That did it! Thanks again. Just curious though…why set the alerts to true after each loop? Why not just at the end?

            • #1006324

              You could set DisplayAlerts to True at the very end of the code, or even omit it – Excel resets DisplayAlerts to True automatically when code execution ends. But I prefer to set it to True explicitly the moment it is no longer necessary to suppress alerts and warnings, to prevent something slipping by without noticing it.

    Viewing 1 reply thread
    Reply To: File conversion function (2003 SP2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: