vb.net获取活动excel工作簿的完整路径和文件名

时间:2013-11-11 09:45:33

标签: vb.net visual-studio-2010 excel

我几天前有这个代码工作,但忘了保存工作副本。我花了4个星期才找到这个答案,并且不想再花那么多时间,所以......

此处的所有内容均有效,但 objWorkBook 行除外,它们返回错误: “变量'objWorkBook'在被赋值之前使用。在运行时可能会产生空引用异常。”

有什么建议吗?

Dim objExcel As Excel.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
Dim objWorkBook As Excel.Workbook

Dim totalWorkBooks As Integer = objExcel.Workbooks.Count
MsgBox(totalWorkBooks & " is Number of Open Workbooks")

Dim ActiveBookIndex As Integer = objExcel.ActiveWindow.Index
MsgBox(ActiveBookIndex & " is Active Window Index")

Dim FullName As String = objWorkBook.FullName
MsgBox(FullName & " is FullName")

Dim OnlyName As String = objWorkBook.Name
MsgBox(OnlyName & " is Name without the Path")

我忘了我分配的价值。

我的目标是将打开的Excel工作簿名称与已知位置中的名称进行比较,以便在匹配时,我的程序可以继续。我需要上面的代码,所以我可以将它与If-Then中的以下代码进行比较,以便我的程序可以继续。

Dim dir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim FullFileName As String = dir & "\My_File_Name.xlsx"

从积极的方面来说,我拼凑了一个解决方案,尽管这不是我想要的答案....

Dim p() As Process = System.Diagnostics.Process.GetProcessesByName("Excel")
Dim Title As String = p(0).MainWindowTitle
Dim dir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim FullFileName As String = dir & "\" & Replace(Title, "Microsoft Excel - ", "") & ".xlsx"

MsgBox(dir)
MsgBox(Title)
MsgBox(FullFileName)

这将暂时有用,但我想以其他方式解决。

1 个答案:

答案 0 :(得分:1)

更改行

Dim objWorkBook As Excel.Workbook

Dim objWorkBook As Excel.Workbook = Nothing

在尝试在objWorkBook

行中使用它之前,您的Dim FullName As String = objWorkBook.FullName对象也未分配给任何内容

这是你在尝试的吗?

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim objExcel As Excel.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
        Dim objWorkBook As Excel.Workbook

        Dim totalWorkBooks As Integer = objExcel.Workbooks.Count
        MsgBox (totalWorkBooks & " is Number of Open Workbooks")

        Dim ActiveBookIndex As Integer = objExcel.ActiveWindow.Index
        MsgBox (ActiveBookIndex & " is Active Window Index")

        '~~> Set the workbook to say first workbook.
        '~~> You can use a loop here as well to loop through 
        '~~> the workbooks count
        objWorkBook = objExcel.Workbooks(1)

        Dim FullName As String = objWorkBook.FullName
        MsgBox (FullName & " is FullName")

        Dim OnlyName As String = objWorkBook.Name
        MsgBox (OnlyName & " is Name without the Path")

        '
        '~~> Rest of the code
        '
    End Sub
End Class

编辑:来自评论的跟进

  

但是,假设我已经打开了9个工作簿,如何让我的应用程序对它们进行索引,操作,切换...而不知道提前完整路径和文件名?

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim objExcel As Excel.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
        Dim objWorkBook As Excel.Workbook = Nothing
        Dim FullName As String = ""
        Dim OnlyName As String = ""

        Dim totalWorkBooks As Integer = objExcel.Workbooks.Count
        MsgBox (totalWorkBooks & " is Number of Open Workbooks")

        For i As Integer = 1 To totalWorkBooks
            objWorkBook = objExcel.Workbooks(i)

            With objWorkBook
                FullName = .FullName
                OnlyName = .Name

                MessageBox.Show (FullName & " is FullName and " & OnlyName & " is Name without the Path")

                '
                '~~> Rest of the code here to manipulate the workbook. For example
                ' objWorkBook.Sheets(1).Range("A1").Value = "Blah Blah"
                '

            End With
        Next i

        releaseObject (objExcel)
        releaseObject (objWorkBook)
    End Sub

    '~~> Release the objects
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class