Solved

How to open .ods spreadsheet with VBA?

Posted on 2013-05-15
2
2,277 Views
Last Modified: 2013-05-15
I have an excel workbook (.xlsx, no vba in the workbook - "workbook1") that I open with vba contained in a second workbook ("workbook 2") to pull the data into another spreadsheet("workbook 3"). The code works like a dream normally, but if I open workbook 1 in Open Office Calc, make no changes and save as an .ods file, I get a "run-time error '1004': Method 'Open' of object 'Workbooks' failed". Here is the line that throws the error:

Set InsuredForm = Application.Workbooks.Open(TxtInsuredForm1.Text)

Open in new window


Here is additional relevant code:

Public Sub InsuredFormBrowse1_Click()
    Dim Filex1 As Variant
    
'SET INITIAL PATH FOR OPEN FILE DIALOG.
    sPath = "H:\"
    ChDrive sPath
    ChDir sPath

'OPEN FILE DIALOG.
    Filex1 = Application.GetOpenFilename _
        (FileFilter:="Excel Worksheets(*.xls; *.xlsx; *.ods), *.xls; *.xlsx; *.ods", _
        Title:="Please select the Insured Form.", MultiSelect:=False)
        
'SHOW SELECTED FILE IN TEXTBOX.
    If Filex1 <> False Then
        TxtInsuredForm1 = Filex1
    End If
    
End Sub

Public Sub ContinueButton1_Click()
Dim InsuredForm As Workbook

Application.ScreenUpdating = False
Application.EnableEvents = False

'OPEN THE FORM.
        Set InsuredForm = Application.Workbooks.Open(TxtInsuredForm1.Text)
        If InsuredForm.FileFormat = 51 Or InsuredForm.FileFormat = 56 Then
            Set sourceSheet = InsuredForm.Sheets("Inventory Form")
        ElseIf InsuredForm.FileFormat = 60 Then
            Set sourceSheet = InsuredForm.Sheets("Inventory_Form")
        Else
            WorksheetError.Show
        End If
End Sub

Open in new window


When I try to open the .ods file manually, I get a message that says
"Excel found unreadable content in 'Insured Form.ods'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
I'll click 'ok' and Excel is able to open the workbook and tells me that some parts of the workbook may have been repaired or discarded, but everything looks intact. In the running to win "most useless error log of the year" award is the xml log listing the repairs made to the file:

  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <logFileName>error031280_01.xml</logFileName> 
  <summary>Errors were detected in file 'C:\Documents and Settings\emmonsad\Desktop\Insured Form 20011644.ods'</summary> 
- <additionalInfo>
  <info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info> 
  </additionalInfo>
  </recoveryLog>

Open in new window


How do I get around this? Is there a way in vba to tell Excel to open and attempt to repair damaged files?
0
Comment
Question by:Animaldrummer04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 350 total points
ID: 39169601
Try:
Set InsuredForm = Application.Workbooks.Open(filename:=TxtInsuredForm1.Text, corruptload:=xlrepairfile)

Open in new window

0
 

Author Closing Comment

by:Animaldrummer04
ID: 39169653
Awesome! Thanks. I guess I was searching for the wrong thing.
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question