-
Jun 23rd, 2020, 10:08 AM
#1
Thread Starter
Fanatic Member
Get path of first active excel document from VB.NET
Hello:
I have been reading a few documents online, and for some reason I cannot read the open excel workbook in vb.net.
What am I missing?
Code:
xlsApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
For c As Integer = 1 To xlsApp.Workbooks.Count
MessageBox.Show(c)
xlsApp.Workbooks(1).Activate()
Exit For
Next
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook = xlsApp.ActiveWorkbook
Dim FullName As String = xlsWorkBook.FullName
txtFile.Text = FullName
Thanks.
-
Jun 24th, 2020, 04:16 AM
#2
Re: Get path of first active excel document from VB.NET
from my previous attempt with vb.net, many of the properties of office objects are not available in managed code, i could never understand why
while i was able to work around the issue using unmanaged code it is probably not to be recommended
someone else maybe able to shed more light on this, else possibly ask for this to moved to the vb.net forum
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 24th, 2020, 05:44 AM
#3
Re: Get path of first active excel document from VB.NET
hello,
Either you are not giving all your code or you are missing a lot. This will give you the Fullname of the workbook but you know it already as you need it to open it as it is "path"
Code:
Dim XlApp As New Microsoft.Office.Interop.Excel.Application
Dim Workbook As Microsoft.Office.Interop.Excel.Workbook
Workbook = XlApp.Workbooks.Open("path")
Label3.Text = Workbook.FullName
Workbook.Close()
XlApp.Quit()
so what are you trying to do ? read properties of the workbook or open it to have access to it?
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jun 24th, 2020, 01:42 PM
#4
Thread Starter
Fanatic Member
Re: Get path of first active excel document from VB.NET
I want to fill in the name of the open workbook, in the event it is already open. I realize there can be more than one, so I suppose the topmost one? I have no problem selecting and opening it in code, but I think at times the user will already have the file open.
-
Jun 24th, 2020, 03:37 PM
#5
Re: Get path of first active excel document from VB.NET
Ok
try that
Code:
private sub check_workbooks()
Dim OpenedWorkBooks As New List(Of Workbook)()
Dim ExcelInstances As Process() = Process.GetProcessesByName("EXCEL")
If ExcelInstances.Count() = 0 Then
Exit Sub
End If
Dim ExcelInstance As Excel.Application = TryCast(Marshal.GetActiveObject("Excel.Application"), Excel.Application)
If ExcelInstance Is Nothing Then Exit Sub
For Each WB As Workbook In ExcelInstance.Workbooks
OpenedWorkBooks.Add(WB)
Next
If OpenedWorkBooks.Count > 0 Then
ListBox2.Items.Add(OpenedWorkBooks(0).FullName)
End If
End sub
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jun 24th, 2020, 03:46 PM
#6
Re: Get path of first active excel document from VB.NET
If you want only the active one :
Code:
Dim ExcelInstances As Process() = Process.GetProcessesByName("EXCEL")
If ExcelInstances.Count() = 0 Then
Exit Sub
End If
Dim ExcelInstance As Excel.Application = TryCast(Marshal.GetActiveObject("Excel.Application"), Excel.Application)
If ExcelInstance Is Nothing Then Exit Sub
For Each WB As Workbook In ExcelInstance.Workbooks
ListBox2.Items.Add(WB.FullName)
Next
Dim activeWb As Workbook = ExcelInstance.ActiveWorkbook
Label1.Text = activeWb.Name
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jun 25th, 2020, 09:01 AM
#7
Thread Starter
Fanatic Member
Re: Get path of first active excel document from VB.NET
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|