views:

25

answers:

1

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?

+1  A: 

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.

JaredPar
The error does disappear when the set of the "location" property is removed, and page breaks does show a count of 1. That's rather the issue, though-moving that page break is part of what I need to do in order to generate the worksheet properly. That code does work if I set it as an Excel macro, but some of my recipients cannot accept macros.
Todd
That was the place to look though. The fix is to set your Excel application object's ActiveWindow.View property to "xlPageBreakPreview" before moving the page break, then back to "xlNormalView" once you're done. Apparently it can't be set in the normal view.
Todd