Solved

Return Excel.Workbook in VBA function

Posted on 2011-03-08
3
499 Views
Last Modified: 2012-05-11
I have a function and I want to return an Excel.Workbook  

However, I'm getting an object variable or with variable not set.

Any ideas?
Sub invAuto()
    
    Dim wbk As excel.Workbook
    'Getting Error here   
    wbk = openInvDoc
    
    wbk.Close False
    
End Sub

Function openInvDoc() As excel.Workbook
    Dim xlApp As excel.Application
    Dim wks As excel.Worksheet
    Dim wbk As excel.Workbook
    Dim path As String
    path = "C:\Weatherization\WX Inventory\1611 WX Materials Inventory\1611 WX Materials Inventory.xls"
    
    If Dir(path) = "" Then
        path = whereInvReport()
    End If
    
    If path = "" Then
        MsgBox "File not found or operation cancelled", vbInformation, "Operation Cancelled"
        Exit Function
    End If
   
    Set xlApp = CreateObject("excel.application")
    Set wbk = xlApp.Workbooks.Open(path)
    xlApp.Visible = True
    'Set wks = wbk.Worksheets("rptContractorSchedule")
    
    openInvDoc = wbk
        
End Function

Open in new window

0
Comment
Question by:ClaudeWalker
3 Comments
 
LVL 4

Accepted Solution

by:
Limey1212 earned 250 total points
ID: 35071758
wbk = openInvDoc _ this line should read

set wbk = openInvDoc
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 250 total points
ID: 35071786
Similarly in the function you need:
[b] Set[/b] openInvDoc = wbk

Open in new window

0
 

Author Comment

by:ClaudeWalker
ID: 35071789
Nice!

It turns out I need to set the caller and the return value

'caller
Set wbk = openInvDoc


'return
Set openInvDoc = wbk

Thanks,
JOe K.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question