Re: Page Break in Excel from VB 2005



obook.o***.HPageBreaks.Add(Excel.Range
("A72"))

I get the blue line under Excel.Range

Error 1 'Range' is a type in 'Excel' and cannot be used as an expression.

"rowe_newsgroups" <rowe_email@xxxxxxxxx> wrote in message news:1184005431.309811.162690@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jul 9, 1:18 pm, "Stephen Plotnick" <the...@xxxxxxxxxxxxx> wrote:
'o***.HPageBreaks.Add.Range("A72")
.

"rowe_newsgroups" <rowe_em...@xxxxxxxxx> wrote in message

news:1183999625.014207.264600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

> On Jul 9, 12:43 pm, "Stephen Plotnick" <the...@xxxxxxxxxxxxx> wrote:
>> Seth,

>> THanks for the reply. That is what I did and I always get an exception >> at
>> run time. I googled and seemed to have tried everything and it never
>> seems
>> to work.

>> Steve"rowe_newsgroups" <rowe_em...@xxxxxxxxx> wrote in message

>>news:1183998991.748123.138610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

>> > On Jul 9, 12:24 pm, "Stephen Plotnick" <the...@xxxxxxxxxxxxx> wrote:
>> >> I'm using VB 2005 and Excel 2007 and cannot figure out how to >> >> insert a
>> >> page
>> >> break before line 72. I've tried several ways but always get an
>> >> exception
>> >> error.

>> >> Thanks in advance.

>> > I would recommend you use the "Record Macro" functionality of Excel
>> > and record a macro of you adding the pagebreak. Then you just have >> > to
>> > modify that classic vb code to Visual Basic .Net and use it in your
>> > application.

>> > Thanks,

>> > Seth Rowe

> Post your code.

> Thanks,

> Seth Rowe

You need to specify the range using the excel application object. The
following works fine (except for a late binding warning for using
HPageBreaks) for me:

Option Strict Off

'// Of course you'll need to reference
'// Microsoft Excel Object Library 12.0 for this to work
Imports Microsoft.Office.Interop.Excel

Module Module1

Sub Main()
Dim excel As New Application()
Dim workbook As Workbook = excel.Workbooks.Add()
workbook.Worksheets(1).HPageBreaks.Add(excel.Range("A5"))
excel.Visible = True
End Sub

End Module

Hope That Helps!

Thanks,

Seth Rowe


.