COM Exception (code 0x800A03EC) thrown when programmatically changing page breaks in VB.Net

StackOverflow https://stackoverflow.com/questions/3815544

  •  26-09-2019
  •  | 
  •  

문제

I am attempting to use the VB.Net Excel COM interop to programmatically change the location of the first horizontal page break on an Excel spreadsheet being generated by the program. Code to do so is as follows:

    Dim range As Excel.Range
    xlActualWS.Activate()
    xlActualWS.PageSetup.PrintArea = "$A$1:$K$68"
    range = xlActualWS.Range("A68", "A68")
    xlActualWS.HPageBreaks(1).Location = range
    System.Runtime.InteropServices.Marshal.ReleaseComObject(range)

On the line setting HPageBreaks, COM exception code 0x800A03EC is thrown, and I can't really find a thing related to this searching. Anyone have any idea what I'm missing here?

도움이 되었습니까?

해결책

Based onthe code it looks like either the location of the page break cannot be set or that there are 0 page breaks and hence you're accessing an invalid index. A quick way to test this out is to do the following

  • Check the Count property on xlActualWS.HPageBreaks and see how many are available
  • Remove the set of the Location property and see if the error dissapears

Additionally you should probably remove the ReleaseComObject call. That's a very difficult API to get correct and the CLR is quite good at cleaning up COM object references on it's own.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top