vb.net get full path and filename of active excel workbook

rogue5pawn

I had this code working a few days ago, but forgot to save the working copy. It took me 4 weeks just to find this answer and would not like to take that much time again, so...

Everything here works, except the objWorkBook lines, which return the error: "Variable 'objWorkBook' is used before it has been assigned a value. A null reference exception could result at runtime."

Any suggestions?

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")

I forgot what Value I had assigned.

My objective is to compare an open Excel Workbook name with one in a known location so that if they match, my program can proceed. I need the code above so I can compare it to the following code in an If-Then so that my program can proceed.

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

On a positive note, I pieced together A solution, even though it's not the answer I was looking for....

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)

This will work for now, but I would like to solve it the other way.

Siddharth Rout

Change the line

Dim objWorkBook As Excel.Workbook

to

Dim objWorkBook As Excel.Workbook = Nothing

Also your objWorkBook object is not assigned to anything before you are trying to use it in the line Dim FullName As String = objWorkBook.FullName

Is this what you are trying?

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

EDIT: Followup from comments

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

vb.net get full path and filename of active excel workbook

From Dev

vb.net registry - get rid of full path from childnodes

From Dev

Saving Excel workbook to constant path with filename from two fields

From Dev

In Excel VBA using VB.NET DLLs, is it possible for the DLL to contain code that manipulates values of the Active Workbook cells/ranges/etc?

From Dev

How do I get just the filename and just the path separately in a DragDrop in VB.net?

From Dev

Excel set active Workbook

From Dev

Get data from excel worksheet and copy it into the active workbook

From Dev

Programatically retrieve the full data directory path in VB.NET

From Dev

vb.net get filename list from wildcard

From Dev

Using the filename of workbook to find image with same filename Excel VBA

From Dev

get the path and filename of an object

From Dev

How to get a value for form active state in vb.net

From Dev

Error "Select method of Range class failed" when selecting range in VB.Net Excel workbook

From Dev

How To Extract Excel Sheet Name From Workbook Using VB.NET?

From Dev

How to attach active Excel workbook to an email

From Dev

How to attach active Excel workbook to an email

From Dev

vba reference workbook if active and last 7 characters of filename contain x?

From Dev

VB Script If Statement - Opening Excel Workbook

From Dev

Excel SaveAs changes active workbook - How to SaveAs and keep active workbook active?

From Dev

MVC.NET Get full view path in ActionFilterAttribute

From Dev

PowerShell add full path to filename and copy

From Dev

How to show the full path and filename in OpenFileDialog

From Dev

How to get server local drive path in vb.net?

From Dev

Get VB.NET Relative \Upload path to the running application?

From Dev

VB.NET get path from the file the user opened

From Dev

Excel Generate New Workbook in a dynamic directory Path

From Dev

Getting path or file name of Excel workbook

From Dev

Batch get filename from path

From Dev

Returning a message instead a error in case of opening a excel workbook with wrong filename

Related Related

  1. 1

    vb.net get full path and filename of active excel workbook

  2. 2

    vb.net registry - get rid of full path from childnodes

  3. 3

    Saving Excel workbook to constant path with filename from two fields

  4. 4

    In Excel VBA using VB.NET DLLs, is it possible for the DLL to contain code that manipulates values of the Active Workbook cells/ranges/etc?

  5. 5

    How do I get just the filename and just the path separately in a DragDrop in VB.net?

  6. 6

    Excel set active Workbook

  7. 7

    Get data from excel worksheet and copy it into the active workbook

  8. 8

    Programatically retrieve the full data directory path in VB.NET

  9. 9

    vb.net get filename list from wildcard

  10. 10

    Using the filename of workbook to find image with same filename Excel VBA

  11. 11

    get the path and filename of an object

  12. 12

    How to get a value for form active state in vb.net

  13. 13

    Error "Select method of Range class failed" when selecting range in VB.Net Excel workbook

  14. 14

    How To Extract Excel Sheet Name From Workbook Using VB.NET?

  15. 15

    How to attach active Excel workbook to an email

  16. 16

    How to attach active Excel workbook to an email

  17. 17

    vba reference workbook if active and last 7 characters of filename contain x?

  18. 18

    VB Script If Statement - Opening Excel Workbook

  19. 19

    Excel SaveAs changes active workbook - How to SaveAs and keep active workbook active?

  20. 20

    MVC.NET Get full view path in ActionFilterAttribute

  21. 21

    PowerShell add full path to filename and copy

  22. 22

    How to show the full path and filename in OpenFileDialog

  23. 23

    How to get server local drive path in vb.net?

  24. 24

    Get VB.NET Relative \Upload path to the running application?

  25. 25

    VB.NET get path from the file the user opened

  26. 26

    Excel Generate New Workbook in a dynamic directory Path

  27. 27

    Getting path or file name of Excel workbook

  28. 28

    Batch get filename from path

  29. 29

    Returning a message instead a error in case of opening a excel workbook with wrong filename

HotTag

Archive