Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have written the code for dynamic filling the cells and for printing this excel sheet .My code is working properly but i am facing issues if any other excel file is already opened and minimized ..
after running my code ,other exiting excel is getting maximize ..
my code is as below :-


VB
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

        Dim strCashDepExcelfile As String = Nothing
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        xlApp = New Excel.ApplicationClass

        Dim path As String = Application.StartupPath + "\xxx.xlsx"
        xlWorkBook = xlApp.Workbooks.Open(path)  

        xlWorkSheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)
        Try
           
            Dim cashData As DataClassesCashDepositDenominationsDO = CType(arrDenominationDetail(0), DataClasses.CashDepositDenominationsDO)

            xlWorkSheet.Cells(8, 2) = cashData.Note1000
            xlWorkSheet.Cells(9, 2) = cashData.Note500
            xlWorkSheet.Cells(10, 2) = cashData.Note100
            xlWorkSheet.Cells(11, 2) = cashData.Note50
            xlWorkSheet.Cells(12, 2) = cashData.Note20
            xlWorkSheet.Cells(13, 2) = cashData.Note10
            xlWorkSheet.Cells(14, 2) = cashData.Note5
            xlWorkSheet.Cells(15, 2) = cashData.Note2
            xlWorkSheet.Cells(16, 2) = cashData.Note1
            xlWorkSheet.Cells(16, 2) = cashData.TotalCoinsAmt
            xlWorkSheet.Cells(18, 2) = cashData.TotalNotesAmt
            xlApp.ScreenUpdating = False
            xlApp.DisplayAlerts = False
            xlWorkSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape
            xlWorkBook.SaveAs(Application.StartupPath + "\aaa.xlsx")
            
            Dim printSettings As System.Drawing.Printing.PrinterSettings = New System.Drawing.Printing.PrinterSettings()         
           
            Dim oProcess As New System.Diagnostics.Process
            With oProcess.StartInfo
                .CreateNoWindow = True
                .WindowStyle = ProcessWindowStyle.Hidden
                .Verb = "print"
                .UseShellExecute = True
                .FileName = Application.StartupPath + "\aaa.xlsx" 
            End With
            xlWorkBook.Close(SaveChanges:=True)
            oProcess.Start()
            oProcess.Dispose()
            xlApp.Quit()
            xlApp = Nothing
            MessageBox.Show("aaa printed successfully", "success")
        Catch ex As Exception
            ShowErrorMessage(ex)
            ExceptionManager.Publish(ex)
        Finally
            releaseObject(xlWorkSheet)
            releaseObject(xlWorkBook)
            releaseObject(xlApp)
            If System.IO.File.Exists(Application.StartupPath + "\aaa.xlsx") = True Then
                System.IO.File.Delete(Application.StartupPath + "\aaa.xlsx")
            End If
        End Try
    End Sub
Posted
Updated 26-Oct-15 23:47pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900