Capturing Word Events from Access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fly Girl

    #1

    Capturing Word Events from Access

    Gurus needed! I'm trying to use a Word class object in order to
    capture events in Word. Here's what I have, but the event code
    appWord_BeforeD ocumentSave... doesn't ever fire.

    In a Class Module +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++

    Public WithEvents appWord As Word.Applicatio n
    Public WithEvents docWord As Word.Document

    Public Sub appWord_Documen tBeforeSave(ByV al Doc As Word.Document,
    SaveAsUI As Boolean, Cancel As Boolean)

    MsgBox "This record is locked. You cannot save this file.", _
    vbInformation + vbOKOnly, "Locked Record"
    Cancel = True

    End Sub

    Public Sub OpenNewDocument (strFileName As String)
    Set docWord = appWord.Documen ts.Open(strFile Name)
    End Sub

    Public Sub Class_Initializ e()
    Set appWord = New Word.Applicatio n
    appWord.Visible = True
    End Sub

    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++

    Then in my regular code module
    +++++++++++++++ +++++++++++++++ +++++++++++++++ ++

    Public Function OpenFile()
    On Error GoTo Err_OpenFile

    Dim frm As Form
    Dim myWord As clsMSWord
    Dim strFile As String

    Set frm = Screen.ActiveFo rm

    If frm![EventsSub].Form!FileType = "doc" Then

    Set myWord = New clsMSWord
    strFile = frm![EventsSub].Form.txtLinkFi lePath

    myWord.OpenNewD ocument (strFile)

    Etc....
    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++

    This creates an instance of the class and opens Word and the file.
    However, I cannot get the _BeforeDocument Save event to work.

    Am I completely delusional or should there be a way to do this?

    Thanks!
  • rkc

    #2
    Re: Capturing Word Events from Access

    This answer comes strictly from reading the Word VBA help file.
    Never done this myself.
    The added code is inline below.

    "Fly Girl" <gschipper@rgl. net> wrote in message
    news:446785ab.0 312031407.65789 05b@posting.goo gle.com...[color=blue]
    > Then in my regular code module
    > +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
    >
    > Public Function OpenFile()
    > On Error GoTo Err_OpenFile
    >
    > Dim frm As Form
    > Dim myWord As clsMSWord
    > Dim strFile As String
    >
    > Set frm = Screen.ActiveFo rm
    >
    > If frm![EventsSub].Form!FileType = "doc" Then
    >
    > Set myWord = New clsMSWord[/color]

    <add this maybe>
    Set myWord.App = Word.Applicatio n
    </add this maybe>
    [color=blue]
    > strFile = frm![EventsSub].Form.txtLinkFi lePath
    >
    > myWord.OpenNewD ocument (strFile)
    >
    > Etc....[/color]



    Comment

    • rkc

      #3
      Re: Capturing Word Events from Access

      Fiddled with this for a bit an actually got it working, but when
      run from Access the msgbox does not pop up on top of
      the open document. I got an hourglass cursor and had to set
      focus back to Access to see the msgbox.


      "rkc" <rkc@yabba.dabb a.do.rochester. rr.bomb> wrote in message
      news:f9xzb.1844 69$ZC4.26827@tw ister.nyroc.rr. com...[color=blue]
      > This answer comes strictly from reading the Word VBA help file.
      > Never done this myself.
      > The added code is inline below.[/color]



      Comment

      • Gail Schipper

        #4
        Re: Capturing Word Events from Access

        What? READ the Help files. How shocking.

        Actually I did that several times and what I evetually ended up with is
        below. I instantiated the class object in the class module because when
        I tried it in the code module I kept getting "Active X can't create
        object". The class module Sub Class_Initializ e runs whenever I assign a
        variable to a new class object.

        However, I don't even get the hourglass--you lucky devil, getting that
        hourglass. Nothing, but nothing happens when I click Save on the open
        Word .doc. I keep seeing posts from people who seem to have accomplished
        this feat, though.

        Code in Class Module:

        Public WithEvents appWord As Word.Applicatio n
        Public WithEvents docWord As Word.Document

        Public Sub appWord_Documen tBeforeSave(ByV al Doc As Word.Document,
        SaveAsUI As Boolean, Cancel As Boolean)

        MsgBox "This record is locked. You cannot save this file.",
        vbInformation + vbOKOnly, _
        "Locked Record"
        Cancel = True

        End Sub

        Public Sub OpenNewDocument (strFileName As String)
        Set docWord = appWord.Documen ts.Open(strFile Name)
        End Sub

        Public Sub Class_Initializ e()
        Set appWord = New Word.Applicatio n
        appWord.Visible = True
        End Sub


        Code in regular Module:

        Public Function OpenFile()
        On Error GoTo Err_OpenFile

        Dim frm As Form
        Dim bolWordOpen As Boolean
        Dim myWord As clsMSWord
        Dim strFile As String

        Set frm = Screen.ActiveFo rm

        If frm![EventsSub].Form!FileType = "doc" Then


        Set myWord = New clsMSWord

        strFile = frm![EventsSub].Form.txtLinkFi lePath

        myWord.OpenNewD ocument (strFile)

        Etc....

        At this point the doc is open but clicking on Save, SaveAs etc. doesn't
        trigger the event routine in the Class Module.

        I'm not sure if I need a magic wand or a bigger stick.

        Thanks for your input, though!

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Gail Schipper

          #5
          Re: Capturing Word Events from Access

          Whohoo! I got it to work and it's quite spiff! Peter Huang on one of the
          MSDN boards showed me the error of my ways. The reason I wasn't getting
          any event action was because I declared the class object within the
          function instead of as a module-level variable. This should have been
          obvious, but, hey, sometimes you just can't see the forest for all those
          dang trees!

          To get the message box to be visible I had to minimize Word for a
          moment. In any case, my users can now look at, print preview, print,
          etc. but they can't make changes.

          Here's the glorious code:

          Class Module-----------------------------------------

          Option Compare Database
          Option Explicit

          Public WithEvents appWord As Word.Applicatio n
          Public WithEvents docWord As Word.Document

          Private Sub appWord_Documen tBeforeClose(By Val Doc As Word.Document,
          Cancel As Boolean)

          docWord.Close False
          appWord.Quit

          End Sub

          Public Sub appWord_Documen tBeforeSave(ByV al Doc As Word.Document,
          SaveAsUI As Boolean, Cancel As Boolean)

          appWord.WindowS tate = wdWindowStateMi nimize
          MsgBox "This record is locked. You cannot save this file.",
          vbInformation + vbOKOnly, _
          "Locked Record"
          Cancel = True
          appWord.WindowS tate = wdWindowStateNo rmal

          End Sub

          Public Sub OpenNewDocument (strFileName As String)
          Set docWord = appWord.Documen ts.Open(strFile Name)
          End Sub

          Public Sub Class_Initializ e()
          Set appWord = New Word.Applicatio n
          appWord.Visible = True
          End Sub

          ----------------------------------------------------------

          Code Module-----------------------------------------------

          Dim myWord As clsMSWord

          Public Function OpenFile()
          On Error GoTo Err_OpenFile

          Dim frm As Form
          Dim bolWordOpen As Boolean
          Dim strFile As String

          Set frm = Screen.ActiveFo rm

          If IsNull(frm![EventsSub].Form.oleLinked ) Then
          MsgBox "There is no file attached to this event.", vbOKOnly,
          "Attachment Error"
          Exit Function
          End If

          ' Word files should be opened by Word so we can manipulate them
          appropriately
          If frm![EventsSub].Form!FileType = "doc" Then

          ' Check to see if the record is locked. Open the file
          appropriately.
          If frm![EventsSub].Form.Locked Then

          ' Open as class object so events can be controlled
          Set myWord = New clsMSWord

          ' Opening the file errors if I pass it the file name without
          assigning it to a variable
          strFile = frm![EventsSub].Form.txtLinkFi lePath

          myWord.OpenNewD ocument (strFile)

          Else
          ' Otherwise let Access OLE open the attached file
          frm![EventsSub].Form.oleLinked .Action = acOLEActivate

          End If

          Else
          ' Otherwise let Access OLE open the attached file
          frm![EventsSub].Form.oleLinked .Action = acOLEActivate
          End If


          Exit Function

          Err_OpenFile:

          Call adcRunTimeErr(E rr.Number, Err.Description , "OpenFile")

          End Function

          -----------------------------------------------------------

          Hope this saves someone else some time!


          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • rkc

            #6
            Re: Capturing Word Events from Access


            "Gail Schipper" <gschipper@rgl. net> wrote in message
            news:3fcf533a$0 $88382$75868355 @news.frii.net. ..[color=blue]
            > What? READ the Help files. How shocking.
            >
            > Actually I did that several times and what I evetually ended up with is
            > below. I instantiated the class object in the class module because when
            > I tried it in the code module I kept getting "Active X can't create
            > object". The class module Sub Class_Initializ e runs whenever I assign a
            > variable to a new class object.
            >
            > However, I don't even get the hourglass--you lucky devil, getting that
            > hourglass. Nothing, but nothing happens when I click Save on the open
            > Word .doc. I keep seeing posts from people who seem to have accomplished
            > this feat, though.[/color]

            Here's the code I used with your class module.
            Result: Document opened
            File-Save fired the event in your class module
            Cursor changed to hourglass
            Clicking Access application in the taskbar showed the msgbox
            Dismissing the msgbox set focus to the original calling form
            I posted again and went back to sleep.

            <form module>
            Option Compare Database
            Option Explicit

            Dim myword As clsMSWord

            Public Function OpenFile()
            Set myword = New clsMSWord
            RegisterEventHa ndler
            myword.OpenNewD ocument ("d:\newWord.do c")
            End Function

            Sub RegisterEventHa ndler()
            Set myword.appWord = Word.Applicatio n
            End Sub

            Private Sub Command0_Click( )
            Call OpenFile
            End Sub
            </form module>







            Comment

            Working...