代表者の戯言

Let's go to the print preview




This is the program for going to the print preview immediately via excel.



GC.Collect()

Dim filename As String = "ENTERyourfilename"

' Excel open

Dim xlApp As New Excel.Application

xlApp.Visible = False

xlApp.DisplayAlerts = False

Dim xlBook As Excel.Workbook = Nothing

Dim xlSheet As Excel.Worksheet = Nothing

Try

' Excel file open

xlBook = xlApp.Workbooks.Open(filename)

' get sheet1(after opening xlBook)

xlSheet = CType(xlBook.Sheets(1), Excel.Worksheet)

' PrintPreview may stop if blind

xlApp.Visible = True

' preview or print

xlSheet.PrintPreview()

' ★ print automatically(can't see excel) xlApp.Visible = True/false

‘xlSheet.PrintOut()

Catch ex As Exception

MessageBox.Show("error:" & ex.Message)

Finally

' shut the Excel(if needed)

If xlBook IsNot Nothing Then

xlBook.Close(False)

End If

xlApp.Quit()

' Release COM object

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

GC.Collect()

GC.WaitForPendingFinalizers()

End Try

Me.Close()

データ例1