Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a pagebreak on an excel sheet 1

Status
Not open for further replies.

michelleHEC

Programmer
Oct 20, 2003
95
0
0
US
I have created an excel sheet using vb.net and I am able to set the row size and column size etc but I need to be able to set a page break when a variable changes.
Any ideas??
here is a snipit of the code I am using...

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
xlSheet.Cells.ColumnWidth = 13
xlSheet.Cells.RowHeight = 15
xlSheet.Cells.Font.Size = 12
xlSheet.PageSetup.PrintGridlines = True

xlSheet.Cells(1, 1) = "Truck No"
xlSheet.Cells(1, 2) = "SO Number"
Dim sqlC As String = "Select * from DelTracking order by TruckNo, SONumber"
Dim cmC As New SqlClient.SqlCommand(sqlC, SqlConnection3)
SqlConnection3.Open()
Dim rdrC As SqlClient.SqlDataReader
rdrC = cmC.ExecuteReader
Do While rdrC.Read
SO = rdrC.Item("SONumber")
truck = rdrC.Item("TruckNo")
If truckchange <> truck Then
'***this is where i want to create a page break
rowcount = 2
thanks in advance!
Michelle
 
You want something like:-

xlsheet.HPageBreaks.Add Before:=ActiveCell
 

do I need to declare "ActiveCell"? I am getting an error
 
Use the code below instead.

xlsheet.HPageBreaks.Add (Before:=xlapp.ActiveCell)

Note that you can't insert a horizontal pagebreak if you're activecell happens to be on row 1 of your sheet, therefore the above will error if this is the case. However you can select - hence make active - any other particular cell with code similar to this:-

' make the activecell D20
xlSheet.Range("D20").Select()
 
now I am getting this error
Exception from HRESULT:0x800A03EC
 
Where are you getting this error?

What is your activecell address when this error occurs?
 
Thank you! I have it working now with your help. The error was coming from a mistake on my part not the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top