gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:56:55 25 4
gpt4 key购买 nike

我几天前就可以使用这段代码,但忘记保存工作副本。我花了 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)

这暂时可行,但我想用另一种方式解决它。

最佳答案

换行

Dim objWorkBook As Excel.Workbook

Dim objWorkBook As Excel.Workbook = Nothing

此外,在您尝试在 Dim FullName As String = objWorkBook.FullName 行中使用它之前,您的 objWorkBook 对象未分配给任何对象

这是你正在尝试的吗?

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

编辑:评论的跟进

But let's say I have 9 Workbooks already open, how do I get me app to index, manipulate, switch between them... without knowing the Full Path and File Names ahead of time?

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

关于vb.net 获取事件 excel 工作簿的完整路径和文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19903203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com