调用Excel 2007到Visual Basic 2005窗口窗体 [英] Calling Excel 2007 to Visual Basic 2005 window form

查看:81
本文介绍了调用Excel 2007到Visual Basic 2005窗口窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Excell称为VB.继续在获取excel文件时出现错误.是因为我正在调用Excel 2007应用程序.当我打电话给Excel 2003时,它起作用了


I want to call Excell to VB. keep on giving a error when getting the excel file. Is it because i''m calling the Excel 2007 application. when I calling Excel 2003 it works


Dim xlApp As New Excel.Application
        Dim xcFileInfo As IO.FileInfo
        Dim xcFileDialog As New OpenFileDialog()
        Dim strFileName As String
        xcFileDialog.Filter = "Excel Spreadsheet Files!|*.xls"
        xcFileDialog.Title = "Select estimate in excel spreadsheet file!"
        If xcFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            xcFileInfo = New IO.FileInfo(xcFileDialog.FileName)
            xlApp = CreateObject("Excel.Application")
            strFileName = xcFileDialog.FileName
            'MessageBox.Show(strFileName)
            xlApp.Workbooks.Open(strFileName)
        End If
        xlApp.Visible = True
    End Sub

推荐答案

为什么要使用CreateObject("Excel.Application").您已经取消了Dim xlApp As New Excel.Application excel应用程序对象.

打开excel文件的代码,相同的代码将在2003/2007年运行
Why you are use CreateObject("Excel.Application"). you have already declear Dim xlApp As New Excel.Application excel application object.

code to open excel file same code will run for 2003/2007
Dim objExcel As Microsoft.Office.Interop.Excel.Application
Dim objWB As Microsoft.Office.Interop.Excel.Workbook
Try
   objExcel = New Microsoft.Office.Interop.Excel.Application
   objWB = excel.Workbooks.Open("c:\\test.xls")
   objExcel.Visible = True
   objWB.Activate()
Catch ex As COMException
   MessageBox.Show("Error accessing Excel: " + ex.ToString())
End Try


这篇关于调用Excel 2007到Visual Basic 2005窗口窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆