Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys!

What is the conditional statement to determine if Microsoft Excel or Microsoft Office is not installed? I have a function which it will export data to an excel file. I wanted to prompt an error message if there is no excel.

Thanks.

here is the codes:

VB
Dim path As String
        path = AppDomain.CurrentDomain.BaseDirectory
        'MessageBox.Show(path)
        'Export DataGridView to Excel
        Dim xlapp As Excel.Application

        Dim xlworkbook As Excel.Workbook
        Dim xlworksheetAmort As Excel.Worksheet
        Dim xlworksheetSOA As Excel.Worksheet
        Dim misvalue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlapp = New Excel.Application
        xlworkbook = xlapp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "Template.xls")
        xlworksheetAmort = xlworkbook.Sheets("Amort")

        xlworksheetAmort.Cells(1, 1) = "Period"
        xlworksheetAmort.Cells(1, 2) = "Principal"
        xlworksheetAmort.Cells(1, 3) = "Interest"
        xlworksheetAmort.Cells(1, 4) = "Payment"
        xlworksheetAmort.Cells(1, 5) = "O/S Balance"

        For i = 0 To grdvwAmortization.RowCount - 2
            For j = 0 To grdvwAmortization.ColumnCount - 1
                xlworksheetAmort.Cells(i + 1, j + 1) = _
                    grdvwAmortization(j, i).Value.ToString()
            Next
        Next

        xlworksheetSOA = xlworkbook.Sheets("SOA")

        'Account Details
        xlworksheetSOA.Cells(4, 14) = Date.Now.ToString("MM/dd/yyyy")
        xlworksheetSOA.Cells(6, 4) = lblLastName.Text + ", " + lblFirstName.Text
        xlworksheetSOA.Cells(7, 4) = getLoanType(lblLoanType.Text)
        xlworksheetSOA.Cells(12, 4).NumberFormat = "@"
        xlworksheetSOA.Cells(12, 4) = txtbxPNNum.Text

        'Copy datagridview to excel
        Dim Row As Integer = 13

        For i = 0 To grdvwSOA.RowCount - 2
            For j = 0 To grdvwSOA.ColumnCount - 1
                xlworksheetSOA.Cells(Row, j + 1) = grdvwSOA(j, i).Value.ToString()
            Next
            Row = Row + 1
        Next

        Dim dialog As New SaveFileDialog
        Dim result As DialogResult = dialog.ShowDialog

        Try
            xlworkbook.SaveAs(dialog.FileName)
            'xlworksheet.SaveAs(dialog.FileName)
        Catch exerr As Exception
        End Try

        xlworkbook.Close()
        xlapp.Quit()

        releaseObject(xlapp)
        releaseObject(xlworkbook)
        'releaseObject(xlworksheetAmort)
        'releaseObject(xlworksheetSOA)
Posted
Updated 10-Aug-14 16:56pm
v2

 
Share this answer
 
v2
Comments
mitchiee1226 10-Aug-14 23:51pm    
Thank you. The second link helped me. :)
Sergey Alexandrovich Kryukov 11-Aug-14 1:01am    
It answers the question, a 5.
However, it does not address the root of the problem: the product depends on installation of Office. For better alternative, please see Solution 2.
—SA
Manoj Kumar Choubey 11-Aug-14 1:06am    
Sergey I am agree with you,
I have try to give answered in very simple way for detecting excel (office) application installed or not.
Your solution heavily depends on Office products. This seriously compromises its value, because not all customers will have Excel installed? What's the use if you detect that it does not installed? The expected export still won't be available.

Much better solution would be elimination of dependency on Office. This is possible to achieve by using Microsoft Open XML SDK. Please see my past answer and answers I referenced: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

On Excel document creation, see also this answer: Creating basic Excel workbook with Open XML[^].

—SA
 
Share this answer
 

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