none
Returning a Word.Document object from function RRS feed

  • Question

  • I am trying to create the following function:

    Function OpenFile(strFilePath As String, strFileName As String)
    
        Dim strFilenameNPath As String
        strFilenameNPath = strFilePath & strFileName
      
                On Error GoTo Error_Handler
                
                'Requires a reference to the Word object library
                Dim oApp As Word.Application
                Dim oDoc As Word.Document
                 
                Set oDoc = Nothing
                 
                Application.ScreenUpdating = False
                
                On Error Resume Next
                    Set oApp = GetObject(, "Word.Application")
                    If Err.Number <> 0 Then 'Word isn't running so start it
                        Set oApp = CreateObject("Word.Application")
                    End If
                    
                On Error GoTo -1
                
                    Set oDoc = oApp.Documents.Open(strFilenameNPath)
                    OpenFile = oDoc
                    Exit Function
                 
    Error_Handler_Exit:
                    On Error Resume Next
                    Set oDoc = Nothing
                    Exit Function
                 
    Error_Handler:
                    MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
                            "Error Number: " & Err.Number & vbCrLf & _
                            "Error Source: OpenWordDoc" & vbCrLf & _
                            "Error Description: " & Err.Description, _
                            vbCritical, "An Error has Occured!"
                    Resume Error_Handler_Exit
     
    
    End Function
    

    but when I called the function is the form of:

    Set oInvSettingDoc  = OpenFile(settingFilePath, settingFileName)

    nothing is being returned.  I have stepped through the module with the debugger and confirmed that proper file is opened within the function OpenFile, but I received a error "Object Required" when I try to manipulate the document object from the calling function.

    Any guidance would be greatly appreciated.

    Thank you,

    Michael

    Tuesday, August 23, 2016 9:00 PM

Answers

  • Change the line

                    OpenFile = oDoc

    to

                    Set OpenFile = oDoc


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Tex_16 Wednesday, August 24, 2016 1:23 AM
    Tuesday, August 23, 2016 10:03 PM

All replies

  • Change the line

                    OpenFile = oDoc

    to

                    Set OpenFile = oDoc


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Tex_16 Wednesday, August 24, 2016 1:23 AM
    Tuesday, August 23, 2016 10:03 PM
  • Thank you, thank you.

    I thought I tried that already, but guess not.

    Thanks again,

    Michael

    Wednesday, August 24, 2016 1:23 AM