?
Solved

Return Excel.Workbook in VBA function

Posted on 2011-03-08
3
Medium Priority
?
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

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

set wbk = openInvDoc
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 1000 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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

764 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