Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Excel automation problem

Excel automation problem

Scheduled Pinned Locked Moved Visual Basic
helpcomtestingtoolsquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alsvha
    wrote on last edited by
    #1

    Greetings all. I've made a service which creates an Excel workbook automatically using the Excel COM object. I have however got a problem that when I try to enter a formula containing the sheet names to sum across all sheets that the service crashes with a "Exception from HRESULT: 0x800A03EC" error and I'm at my wits ends right now. I know the formula is correct, because I can copy it from the code and past it into the workbook afterwards and it runs fine, but when trying to do so automatically - it fails. I create a cell range for the cell I want to write this formula in, and use the "Formula" property to set the value, and here it crashes. I've made other formulas without issues, and if I do not contain sheetnames, I can write the formula easily enough (I substituted my SUM for a SUM(1+1)) in the relevant cellrange. So the issue is solely with a formula containing sheet names for me. Has anybody else experienced something similar, and possible have a solution or a heads up where to go for one?

    --------------------------- 127.0.0.1 - Sweet 127.0.0.1

    D 1 Reply Last reply
    0
    • A Alsvha

      Greetings all. I've made a service which creates an Excel workbook automatically using the Excel COM object. I have however got a problem that when I try to enter a formula containing the sheet names to sum across all sheets that the service crashes with a "Exception from HRESULT: 0x800A03EC" error and I'm at my wits ends right now. I know the formula is correct, because I can copy it from the code and past it into the workbook afterwards and it runs fine, but when trying to do so automatically - it fails. I create a cell range for the cell I want to write this formula in, and use the "Formula" property to set the value, and here it crashes. I've made other formulas without issues, and if I do not contain sheetnames, I can write the formula easily enough (I substituted my SUM for a SUM(1+1)) in the relevant cellrange. So the issue is solely with a formula containing sheet names for me. Has anybody else experienced something similar, and possible have a solution or a heads up where to go for one?

      --------------------------- 127.0.0.1 - Sweet 127.0.0.1

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It looks like that's a nice "generic" error returned by Excel when there is a problem with just about anything using the automation model. What does the code look like that's setting this formula?

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        It looks like that's a nice "generic" error returned by Excel when there is a problem with just about anything using the automation model. What does the code look like that's setting this formula?

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        A Offline
        A Offline
        Alsvha
        wrote on last edited by
        #3

        The code is something along this line (simplified to avoid writing unnecessary function call et al) myExcelObject.CreateRange("myCellRange") myExcelObject.CellRange.Formula = "=SUM('SheetName'!Cell;'SheetName'!Cell)" I can post the more exact code if needs be, but this is the gist of it. As mentioned, I write other formulas without problems, although these do not span multiple sheets - including array formulas - so there can't really be much at fault with my base code. The problem seems to lie somewhat deeper then just the normal issues, which is why I turned to this forum. I was thinking the issue might be connected to the fact that I need to "cheat" Excel to think it is a US-EN system by changing the culture when I create the excel objects needed (Workbook, Sheets etc), and when I then try to write the SheetName's in the formula that it has an localization issue or something. But if this is the case, then I'm really without much of a hope - it just really annoys me cause I can take the fomula from the code, paste it into the generated Excel document and have it work. I just can't print it out automatically :(

        --------------------------- 127.0.0.1 - Sweet 127.0.0.1

        D L 2 Replies Last reply
        0
        • A Alsvha

          The code is something along this line (simplified to avoid writing unnecessary function call et al) myExcelObject.CreateRange("myCellRange") myExcelObject.CellRange.Formula = "=SUM('SheetName'!Cell;'SheetName'!Cell)" I can post the more exact code if needs be, but this is the gist of it. As mentioned, I write other formulas without problems, although these do not span multiple sheets - including array formulas - so there can't really be much at fault with my base code. The problem seems to lie somewhat deeper then just the normal issues, which is why I turned to this forum. I was thinking the issue might be connected to the fact that I need to "cheat" Excel to think it is a US-EN system by changing the culture when I create the excel objects needed (Workbook, Sheets etc), and when I then try to write the SheetName's in the formula that it has an localization issue or something. But if this is the case, then I'm really without much of a hope - it just really annoys me cause I can take the fomula from the code, paste it into the generated Excel document and have it work. I just can't print it out automatically :(

          --------------------------- 127.0.0.1 - Sweet 127.0.0.1

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          A bit more exact would help, I mean a copy'n'paste of the real thing. The error that's returned is usually caused by an automation failure. Probably due to the code doing something it's not allowed to do. What your doing by putting the formula in a range of cells is not what your code is doing. I don't do Office Interop projects, so my experience is very limited.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          A 1 Reply Last reply
          0
          • A Alsvha

            The code is something along this line (simplified to avoid writing unnecessary function call et al) myExcelObject.CreateRange("myCellRange") myExcelObject.CellRange.Formula = "=SUM('SheetName'!Cell;'SheetName'!Cell)" I can post the more exact code if needs be, but this is the gist of it. As mentioned, I write other formulas without problems, although these do not span multiple sheets - including array formulas - so there can't really be much at fault with my base code. The problem seems to lie somewhat deeper then just the normal issues, which is why I turned to this forum. I was thinking the issue might be connected to the fact that I need to "cheat" Excel to think it is a US-EN system by changing the culture when I create the excel objects needed (Workbook, Sheets etc), and when I then try to write the SheetName's in the formula that it has an localization issue or something. But if this is the case, then I'm really without much of a hope - it just really annoys me cause I can take the fomula from the code, paste it into the generated Excel document and have it work. I just can't print it out automatically :(

            --------------------------- 127.0.0.1 - Sweet 127.0.0.1

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            I have no Excel automation experience, but maybe this idea could help: you could enter the formula in Excel manually, then use automation to see how it looks... :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            A 1 Reply Last reply
            0
            • D Dave Kreskowiak

              A bit more exact would help, I mean a copy'n'paste of the real thing. The error that's returned is usually caused by an automation failure. Probably due to the code doing something it's not allowed to do. What your doing by putting the formula in a range of cells is not what your code is doing. I don't do Office Interop projects, so my experience is very limited.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              A Offline
              A Offline
              Alsvha
              wrote on last edited by
              #6

              With mObjExcel .CreateRange(.GetExcelColumnName(EnuColumn.UnitPrice + mIntColumnCountFactor) & mIntColRow(intCounter) & ":" & .GetExcelColumnName(EnuColumn.UnitPrice + mIntColumnCountFactor) & mIntColRow(intCounter), mStrSheetNames(intCounter)) .CellRange.NumberFormat = "#.##0" .CellRange.Formula = "=SUM(" & strFormular & ")" .CellRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, , mObjBlack.ToInteger) End With The "GetExcelColumnName" is a function which just returns the Excel Column name such as A1, B2 based on a column and row number. Nothing special - just one I made to help myself. The strFormula variable is the formula mentioned which looks like 'Sheetname!Cell;Sheetname!Cell' and is build dynamically based on which actual sheetnames and cells are used. The formula works when I copy it into the generated excel document so there is not a problem with the formula as such. Just the generation of it automatically Inside my mObjExcel the CreateRange looks like Friend Sub CreateRange(ByVal range As Object, ByVal strSheetName As String) SetCultureFixStart() ''Changes the current Culture to en-US, casue otherwise excel automation crashes If Not mObjCells Is Nothing Then ReleaseComObject(mObjCells)''cleans up COM objects which might linger in memory mObjCells = Nothing ''cleans up COM objects which might linger in memory System.GC.Collect() ''cleans up COM objects which might linger in memory mObjCells = mObjSheets(strSheetName).Range(range).Cells SetCultureFixEnd() ''sets culture back End Sub And CellRange is just a property which returns my mObjCells (which is an Excel.Range). What strikes me as most annoying is that the above code works when I use 1+1 in the formula instead of Sheetnames!cell, or when I post out more complex formulas, even .FormulaArray work. So basically so far, I've been able to do all I wanted to, except use Sheetnames in a formula :/ Thanks so far for the feedback/time.

              --------------------------- 127.0.0.1 - Sweet 127.0.0.1

              A 1 Reply Last reply
              0
              • L Luc Pattyn

                I have no Excel automation experience, but maybe this idea could help: you could enter the formula in Excel manually, then use automation to see how it looks... :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                A Offline
                A Offline
                Alsvha
                wrote on last edited by
                #7

                The formula works, so that one is not a problem. I just can't generate the document automatically with this formula - but I can copy it into the document after generation where it would work.

                --------------------------- 127.0.0.1 - Sweet 127.0.0.1

                1 Reply Last reply
                0
                • A Alsvha

                  With mObjExcel .CreateRange(.GetExcelColumnName(EnuColumn.UnitPrice + mIntColumnCountFactor) & mIntColRow(intCounter) & ":" & .GetExcelColumnName(EnuColumn.UnitPrice + mIntColumnCountFactor) & mIntColRow(intCounter), mStrSheetNames(intCounter)) .CellRange.NumberFormat = "#.##0" .CellRange.Formula = "=SUM(" & strFormular & ")" .CellRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, , mObjBlack.ToInteger) End With The "GetExcelColumnName" is a function which just returns the Excel Column name such as A1, B2 based on a column and row number. Nothing special - just one I made to help myself. The strFormula variable is the formula mentioned which looks like 'Sheetname!Cell;Sheetname!Cell' and is build dynamically based on which actual sheetnames and cells are used. The formula works when I copy it into the generated excel document so there is not a problem with the formula as such. Just the generation of it automatically Inside my mObjExcel the CreateRange looks like Friend Sub CreateRange(ByVal range As Object, ByVal strSheetName As String) SetCultureFixStart() ''Changes the current Culture to en-US, casue otherwise excel automation crashes If Not mObjCells Is Nothing Then ReleaseComObject(mObjCells)''cleans up COM objects which might linger in memory mObjCells = Nothing ''cleans up COM objects which might linger in memory System.GC.Collect() ''cleans up COM objects which might linger in memory mObjCells = mObjSheets(strSheetName).Range(range).Cells SetCultureFixEnd() ''sets culture back End Sub And CellRange is just a property which returns my mObjCells (which is an Excel.Range). What strikes me as most annoying is that the above code works when I use 1+1 in the formula instead of Sheetnames!cell, or when I post out more complex formulas, even .FormulaArray work. So basically so far, I've been able to do all I wanted to, except use Sheetnames in a formula :/ Thanks so far for the feedback/time.

                  --------------------------- 127.0.0.1 - Sweet 127.0.0.1

                  A Offline
                  A Offline
                  Alsvha
                  wrote on last edited by
                  #8

                  I've made a much more simple example which also fails:

                  mObjExcel = New Excel.Application
                  mObjWorkBooks = mObjExcel.Workbooks
                  mObjWorkBook = mObjWorkBooks.Add
                  mObjSheets = mObjWorkBook.Worksheets
                  If mObjSheets.Count > 0 Then
                  For i As Integer = mObjSheets.Count To 2 Step -1
                  mObjSheet = mObjSheets.Item(i)
                  mObjSheet.Delete()
                  Next
                  mObjSheet = mObjSheets.Item(1)
                  Else
                  mObjSheet = mObjSheets.Add
                  End If
                  mObjSheet.Name = "A"
                  mObjSheet.Range("A1").Value = "1"
                  mObjSheet = mObjSheets.Add()
                  mObjSheet.Name = "B"
                  mObjSheet.Range("A1").Value = "1"
                  mObjSheet.Range("A2").Formula = "=SUM(A1;A!A1)"

                  This fails, but if I substitute the formula for =SUM(1+1) it writes correct. The formula is taken directly from what Excel wrote when I made the SUM in there.

                  A 1 Reply Last reply
                  0
                  • A Alsvha

                    I've made a much more simple example which also fails:

                    mObjExcel = New Excel.Application
                    mObjWorkBooks = mObjExcel.Workbooks
                    mObjWorkBook = mObjWorkBooks.Add
                    mObjSheets = mObjWorkBook.Worksheets
                    If mObjSheets.Count > 0 Then
                    For i As Integer = mObjSheets.Count To 2 Step -1
                    mObjSheet = mObjSheets.Item(i)
                    mObjSheet.Delete()
                    Next
                    mObjSheet = mObjSheets.Item(1)
                    Else
                    mObjSheet = mObjSheets.Add
                    End If
                    mObjSheet.Name = "A"
                    mObjSheet.Range("A1").Value = "1"
                    mObjSheet = mObjSheets.Add()
                    mObjSheet.Name = "B"
                    mObjSheet.Range("A1").Value = "1"
                    mObjSheet.Range("A2").Formula = "=SUM(A1;A!A1)"

                    This fails, but if I substitute the formula for =SUM(1+1) it writes correct. The formula is taken directly from what Excel wrote when I made the SUM in there.

                    A Offline
                    A Offline
                    Alsvha
                    wrote on last edited by
                    #9

                    I found a workaround which only confirms my suspecision that it must be something culture specific issue. I could instead of using a SUM formula add the cells together - and I was able to do this with the sheetnames in the addition, so instead of "SUM" I use "=SheetName!Cell+SheetName!Cell ...." and that looks to work when printed out as a .Value. Wheee. :) Thanks for the help otherwise

                    D 1 Reply Last reply
                    0
                    • A Alsvha

                      I found a workaround which only confirms my suspecision that it must be something culture specific issue. I could instead of using a SUM formula add the cells together - and I was able to do this with the sheetnames in the addition, so instead of "SUM" I use "=SheetName!Cell+SheetName!Cell ...." and that looks to work when printed out as a .Value. Wheee. :) Thanks for the help otherwise

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Hmmm...very weird. I wouldn't have thought that the culture would affect the sheetname.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups