Here is the code
Module Module1
Public conn As OLEDBConnection()
Public Filename As String
Public PDFFilename As String
Public ImageFileName As String
Public chkexcel As Boolean
Public oexcel As ApplicationClass = Nothing
Public obook As Workbook = Nothing
Public osheet As Worksheet = Nothing
Public LineNumber As Integer
Public cfColorScale As ColorScale = Nothing
Public cfIconSet As IconSetCondition = Nothing
Public R As Integer
Public InputNumber As Integer
Public MultiLetter As String
Public line As String
Public LastColumn As Integer
Public WorkCost As Long
Public WorkRetail As Long
Public WorkArea1 As Long
Public WorkArea2 As Long
Public WorkArea3 As Long
Public YTDNAME As String
Public CURMONTH As String
Public TWELVEMONTHNAME As String
Public YTDNAMELY As String
Public CURMONTHLY As String
Public TWELVEMONTHNAMELY As String
Sub Main()
Filename = AppDomain.CurrentDomain.BaseDirectory & "Steve.xlsx"
ImageFileName = AppDomain.CurrentDomain.BaseDirectory &
"GroupCBFLogoBetterWay.JPG"
PDFFilename = AppDomain.CurrentDomain.BaseDirectory & "Steve.pdf"
'check if file already exists then delete it to create a new file
If File.Exists(Filename) Then
File.Delete(Filename)
End If
If Not File.Exists(Filename) Then
chkexcel = False
'create new excel application
oexcel = CreateObject("Excel.Application")
'oexcel = New ApplicationClass()
'add a new workbook
obook = oexcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
'set the application alerts not to be displayed for confirmation
oexcel.Application.DisplayAlerts = True
'check total sheets in workbook
Dim S As Integer = oexcel.Application.Sheets.Count()
'leaving first sheet delete all the remaining sheets
If S > 1 Then
oexcel.Application.DisplayAlerts = False
Dim J As Integer = S
Do While J > 1
oexcel.Application.Sheets(J).delete()
J = oexcel.Application.Sheets.Count()
Loop
End If
'to check the session of excel application
chkexcel = True
Console.WriteLine("Generating Steve Report")
osheet = obook.Worksheets(1)
osheet.Name = "Measure and Monitoring"
oexcel.Visible = False
'Try
'Dbopen()
Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("workdept.txt")
LineNumber = 1
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Generate_Sheet()
LineNumber = LineNumber + 1
Loop Until line Is Nothing
sr.Close()
End Using
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
osheet.Range("A2:AZ400").Font.Size = 8.5
osheet.Range("1:1").Font.Size = 8
osheet.Range("A1:AZ400").Font.Bold = True
osheet.Range("A1:AZ400").Font.Name = "Segoe(UI)"
osheet.Range("A1").Value = "Report for Steve"
osheet.Range("A1").EntireColumn.AutoFit()
With osheet.Range("A1").Borders(XlBordersIndex.xlEdgeBottom)
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
End With
With osheet.Range("A1").Borders(XlBordersIndex.xlEdgeTop)
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
End With
With osheet.Range("A1").Borders(XlBordersIndex.xlEdgeRight)
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
End With
With osheet.Range("A1").Borders(XlBordersIndex.xlEdgeLeft)
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
End With
'format headings
osheet.Rows("1").RowHeight = 40
osheet.Columns("A").ColumnWidth = 35
osheet.Range("B:ALN").ColumnWidth = 14
osheet.Range("A1:AZ1").WrapText = True
osheet.Rows("4").Hidden = True
osheet.Rows("9").Hidden = True
osheet.Rows("12").Hidden = True
osheet.Rows("15").Hidden = True
osheet.Rows("20").Hidden = True
osheet.Rows("23").Hidden = True
osheet.Rows("26").Hidden = True
osheet.Rows("31").Hidden = True
osheet.Rows("34").Hidden = True
osheet.Rows("56").Hidden = True
osheet.Rows("59").Hidden = True
osheet.Rows("71").Hidden = True
osheet.Rows("77").Hidden = True
osheet.Rows("82").Hidden = True
osheet.Rows("85").Hidden = True
osheet.PageSetup.CenterHeader = "&C&""Segoe(UI),Bold""&16 Steve"
osheet.PageSetup.CenterFooter = "&C&""Segoe(UI),Bold""&12 Steve
Reporting© for Month of June 2007"
osheet.PageSetup.LeftMargin = 40
osheet.PageSetup.RightMargin = 0
osheet.PageSetup.PrintGridlines = True
With osheet.PageSetup.LeftHeaderPicture
.Filename = ImageFileName
.Height = 25.25
.Width = 100.5
End With
osheet.PageSetup.LeftHeader = "&G"
osheet.PageSetup.PrintTitleRows = ("$A$1")
osheet.PageSetup.PrintTitleColumns = ("$A:$A")
osheet.PageSetup.Orientation = XlPageOrientation.xlLandscape
'obook.osheet.HPageBreaks.Add("A72")
'osheet.HPageBreaks.Add(Before:=ActiveCell)
'Dim hpagebreaks As HPageBreaks = osheet.HPageBreaks
'hpagebreaks.Add("A72")
Thanks for all you help,
Steve
rowe_newsgroups said:
I can't believe this simple problem...
I'm very new to VB...
My code is very similar but my initial DIM is here.
Public oexcel As ApplicationClass = Nothing
oexcel = New ApplicationClass()
Here is my line in the program.
obook.osheet.HPageBreaks.Add(oexcel.Range("A72"))
Here is the exception.
System.MissingMemberException was unhandled
Message="Public member 'osheet' on type 'WorkbookClass' not found."
Source="Microsoft.VisualBasic"
StackTrace:
at
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object
o, Type objType, String name, Object[] args, String[] paramnames,
Boolean[]
CopyBack)
at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at MeasureMonitorSpreadsheet.Module1.Main() in
C:\Users\SP\Documents\Visual Studio
2005\Projects\MeasureMonitorSpreadsheet\Module1.vb:line 565
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
obook.osheet.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.
'osheet.HPageBreaks.Add.Range("A72")
.
On Jul 9, 12:43 pm, "Stephen Plotnick" <
[email protected]>
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.
On Jul 9, 12:24 pm, "Stephen Plotnick" <
[email protected]>
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.
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.
Seth Rowe
Post your code.
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:
'// Of course you'll need to reference
'// Microsoft Excel Object Library 12.0 for this to work
Imports Microsoft.Office.Interop.Excel
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
Hope That Helps!
Seth Rowe- Hide quoted text -
You looked at my code wrong - I used the instance "excel" not the type
"Excel." You need to use the instance of Excel.Application that you
use to create the excel workbooks.
Seth Rowe
I don't see where you dimension obook or osheet. Could you please post
the entire code you use?
Thanks,
Seth Rowe