Results 1 to 2 of 2
  1. #1
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82

    Setting Word document to a class

    I am attempting to set a word document to a class such that I can trap a document Close event.

    The class is set within a Command Button click event.

    The event sets the word.application, set the word.document, then attempts to set the document to a class which traps the document close.

    The problem is that this works sometimes. Sometimes not. I can't tell why it works only sometimes. Naturally I need it to work all the time. Every time.

    I have tried to set new instances of the document also, but that hasn't worked either.

    Note that this is basically all that is contained in the DB, so there is nothing else related to this issue.


    Code:
    Option Compare Database
    '*********************
    ' Class Module (Cls_Word)
    '*********************
    Option Compare Database
    Option Explicit
    Public WithEvents WdClsDoc As Word.Document
    Private Sub WdClsDoc_Close()
    'Purpose: Detect if a linked document is closed
        MsgBox "Doc Close event"
    End Sub
    Code:
    '*********************
    ' Form Module (Frm_Word)
    '*********************
    Option Compare Database
    Option Explicit
    Dim WdApp As Word.Application
    Dim wdDocLink As Word.Document ' Or New Document, but that is still not consistant.
    Dim ClsWord As New Cls_Word
    
    
    Private Sub BtnLink_Click()
    'Purpose:  Links a word document(or application) with a Class.
    Dim strFileName As String
    
    
        DoCmd.Hourglass True
        strFileName = "C:\My Documents\Word\DocNew.docx"
        
        'Set Application
        On Error Resume Next
        Set WdApp = Nothing
        Set WdApp = GetObject(, "Word.Application")
        If WdApp Is Nothing Then
            Set WdApp = CreateObject("Word.Application")
        End If
        On Error GoTo 0
        WdApp.Visible = True
        WdApp.Activate
    
    
        'Set Document
        Set wdDocLink = WdApp.Documents.Open(FileName:=strFileName)
    
    
        'Set Class
        On Error GoTo ErrHandler
        Set ClsWord.WdClsDoc = wdDocLink
        On Error GoTo 0
    
    
    Quitsub:
        DoCmd.Hourglass False
        Exit Sub
    ErrHandler:
        MsgBox "Class was not correctly set." & vbCrLf & Err.Description
        GoTo Quitsub
    End Sub
    I should add that the resulting error is:



    Code:
    462 - The remote server machine does not exist or is unavailable.
    Which I don't get because the server machine is set in module declarations

    Cheers and thanks in advance for assistance.
    Last edited by journeyman; 07-25-2023 at 03:11 PM.

  2. #2
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Update:

    The error regarding the remote serve did not appear right, since I had definitely set the Word Application and the Word Document.

    And I was definitely releasing them also

    Finally I wondered if the class itself was not releasing on the close event.

    So I added a '= Nothing' to the class document, which seems to release the class after the event has run.

    Code:
    Option Compare Database
    '*********************
    ' Class Module (Cls_Word)
    '*********************
    Option Compare Database
    Option Explicit
    Public WithEvents WdClsDoc As Word.Document
    Private Sub WdClsDoc_Close()
    'Purpose: Detect if a linked document is closed
        MsgBox "Doc Close event"
        Set WdClsDoc = Nothing  '<= Include this here.
    End Sub
    Now, I don't yet understand this as I thought it should automatically release on the document close, but it seems to work.... everytime.

    Somebody smart might be able to better explain it, but this is a nice bit of functionality for those who might later want to link and dock their word document to Access for any reason.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Word Document closing class
    By journeyman in forum Access
    Replies: 1
    Last Post: 12-12-2022, 03:05 PM
  2. Imported word document
    By Xypopcorn in forum Import/Export Data
    Replies: 1
    Last Post: 01-12-2020, 05:49 AM
  3. Replies: 3
    Last Post: 06-27-2017, 12:06 PM
  4. Create new Word document.
    By Bill H in forum Programming
    Replies: 3
    Last Post: 06-12-2012, 06:40 AM
  5. Import Word document using VBA
    By degras in forum Import/Export Data
    Replies: 4
    Last Post: 04-12-2011, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums