« Previous Next »

Thread: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

Last post 09-09-2008 10:34 AM by tomkmvp. 7 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (8 items)

Sort Posts:

  • 09-08-2008, 12:42 PM

    Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    Hi ALL

    Im trying to programmatically read a comma delimited file using vbscript and excel, in a windows XP and IIS6 environment as follows:

    Dim objExcelApp  'the Excel Application
    Dim objExcelBook  'the Excel Workbook
    Dim objExcelSheet  'the Excel Worksheet we are currently getting data from
    Const xlMSDOS = 437
    Const xlDelimited = 1
    Const xlTextQualifierDoubleQuote = 1
    Const xlGeneralFormat = 1

    Set objExcelApp = CreateObject("Excel.Application")
    objExcelApp.DisplayAlerts = 0
    objExcelApp.visible = true

    Set objExcelBook = objExcelApp.Workbooks.OpenText(strUploadFile,xlMSDOS, 1,xlDelimited,xlTextQualifierDoubleQuote, False, True, True, True, False, False, Array(Array(1, 1), Array(2, 1)),False)

    but I get the following error message:

    Microsoft Office Excel error '800a03ec'
    Unable to get the OpenText property of the Workbooks class

    I believe its something to do with IIS and permisions to properties in Excel but I dont know how to correct it. Does anyone have any ideas ?

    Thanks Fletcher74

  • 09-08-2008, 1:46 PM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Central NJ
    • Posts 6,187
    • IIS MVPs

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

  • 09-08-2008, 2:07 PM In reply to

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    Thanks Tom for your reply

    I've read the kb article and although Microsoft appears not to recommend server side automation with IIS and Excel, Im not sure how else to do it as I need to convert it to an excel file. I should have mentioned that the site is to be secured using SecurId cards so only a restricted and known number of users will be able to access the site. Is it technically possible to use the OpenText property via IIS ?

    regards Fletcher74

  • 09-08-2008, 3:27 PM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Central NJ
    • Posts 6,187
    • IIS MVPs

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    After it's converted to Excel, what do you need to do with it?  I'm asking because maybe we can come up with a better way ...

  • 09-09-2008, 3:52 AM In reply to

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    Hi Tom

    Essentially, what I need to do is the following:

    - read a comma delimited file uploaded by user
    - validate the values of the file
    - mark each value of the file as valid or invalid
    - save validated file to server and display results
    - if user is happy with results, update to database with valid results

    What I was hoping to do was to use the opentext method to read the comma delimited file as it appeared to be an simple and elegant way of reading a comma delimited file.
    Having said that, I've also written a work around (here's a snippet)

    Dim objFSO,objFile,strContents
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    REM Read all contents of file into a string
    Set objFile = objFSO.OpenTextFile(strUploadFile, 1)
    strContents = objFile.ReadAll
    objFile.Close

    REM Replace all occurances of comma in file with tab character
    strContents = Replace(strContents, ",", vbTab)

    REM Write updated contents back to file
    Set objFile = objFSO.OpenTextFile(strUploadFile, 2)
    objFile.Write strContents
    objFile.Close

    REM Now open as excel file and read contents
    Set objExcelBook = objExcelApp.Workbooks.open(strUploadFile, false, false)

    REM how many columns are used in the current worksheet
    Set currentWorkSheet = objExcelApp.ActiveWorkbook.Worksheets(1)
    usedColumnsCount = objExcelSheet.UsedRange.Columns.Count

    REM For simplicity only allow one used column of data.
    If usedColumnsCount <> 1 Then
     response.write "<br>Only one column of data allowed. Please correct and retry."
     response.end
    Else
     REM how many rows are used in the current worksheet
     usedRowsCount = currentWorkSheet.UsedRange.Rows.Count

     REM What is the topmost row in the spreadsheet that has data in it
     topMostRow = currentWorkSheet.UsedRange.Row

     REM What is the leftmost column in the spreadsheet that has data in it.
     leftMostCol = currentWorkSheet.UsedRange.Column

     REM Read the cell data from the worksheet
     Set cells = currentWorkSheet.Cells

     REM Only look at columns that are in the "used" range. nb only one column allowed
     curCol = leftMostCol

     REM Loop through each row in the worksheet
     For row = 0 to (usedRowsCount-1)

      REM Only look at rows that are in the "used" range
      curRow = topMostRow + row

      REM Get the value/word that is in the cell
      word = cells(curRow,curCol).Value

      REM Validate value
      If isValidValue(word, strErrMsg) = False Then
       cells(curRow,curCol+1).Value = "FAIL"

      Else
       cells(curRow,curCol).Value = word
       cells(curRow,curCol+1).Value = "PASS"
      End if
     Next
    End if

    REM We are done with the current worksheet, release the memory
    Set currentWorkSheet = Nothing

    REM Save the workbook and quit
    objExcelApp.Workbooks(1).Save
    objExcelApp.Workbooks(1).Close
    objExcelApp.Quit

    REM We are done with the Excel object, release it from memory
    Set objExcelBook = Nothing
    Set objExcelApp = Nothing

    thanks Fletcher

  • 09-09-2008, 8:32 AM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Central NJ
    • Posts 6,187
    • IIS MVPs

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    I don't see any need for Excel at all.  Open the file with objFSO like you do, then read each line and use the Split function to dump the line into an Array.  Loop through the array and test each value.

  • 09-09-2008, 9:24 AM In reply to

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    Hi Tom

    Thanks for the feedback. Given that the opentext method is not supported in IIS and there is the recommendation by MS not to use server side automation with office, I'll remove the use of the excel object. Thanks very much for your time looking into this.

     fletcher

  • 09-09-2008, 10:34 AM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Central NJ
    • Posts 6,187
    • IIS MVPs

    Re: Unable to get the OpenText property of the Workbooks class in Excel using VBscript

    fletcher74:
    I'll remove the use of the excel object
    You'll be much happier in the end!

Page 1 of 1 (8 items)
Microsoft Communities