What is wrong with this automation

  • Thread starter Thread starter HS1
  • Start date Start date
H

HS1

Hello all

In an Access form, I have a button that can open a Word template (letter).
Then I can click "Save as" in this Word document. I want to print this file
name (and its path). Following the suggestion from Alex in this forum:


I created a WithEvents in the module level:
--------
Public WithEvents objWdDoc As Word.Document
------

Then in a button events, I have


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objWdApp As Object

objWdApp = CreateObject("Word.Application")

objWdApp.Visible = True

objWdDoc = objWdApp.Documents.Open(Filename:="c:\wordTemplate.doc")

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

Then I have function to handle its Close event.
------

Private Sub objWdApp_Close()
Debug.Write("objWdDoc.FullName")
set objWdDoc = nothing
End Sub

However, the event seems not work
Could you please help
Many thanks
SH
 
The problem is that you've created two different objects with the name
objWdDoc. One's a module-wide variable that will be accessible anywhere,
while the other's a module-specific variable, unique to Button1_Click.
Unfortunately, it's the latter one that you're instantiating, so you're not
able to use your module-wide one (which is the one whose events you're
supposed to be capturing)

Simply remove the line

Dim objWdApp As Object

from Private Sub Button1_Click(...)

I also think you need to remove the " Handles Button1.Click" from the
declaration: I don't believe that's valid in VBA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top