Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Class Module problem in Word VBA

Posted on 2012-03-21
3
Medium Priority
?
316 Views
Last Modified: 2012-03-24
Dear Experts:

I'm doing VBA automation in Word 2010.  A few years ago while working at a different company I had a friend help me with a similar project.  He helped me figure out a solution which involves a Class Module.  Now it's not working and can't figure it out because I never quite understood how class modules work.  Actually, sometimes it works, which is even more maddening because I can't figure out a pattern.   Here's what I want to accomplish:

In response to a user choice, VBA opens a certain preview document.
Word "watches" for that document to close.
After it closes, VBA fires another macro.

The relevant parts of the code are below:

'In the main module:
Public wdPreview as Word.Document

Set wdPreview = Documents.Open(FileName:=filetoopen) 


'The following code is in a Class Module named wdClassEvents:
------------------------
Private WithEvents wdPreviewApp As Word.Application
-----------------------
Private Sub Class_Initialize()
  Set wdPreviewApp = Word.Application
End Sub
----------------------
Private Sub wdPreviewApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

If Not (wdPreview Is Nothing) Then
     If Doc = wdPreview Then    'It is our document
        wdPreview.Close
        Cancel = True
        Call AfterPreview
    End If
End If

End Sub

Open in new window


Can you spot what might be missing or give me any pointers to get this to work consistently?  Many Thanks in advance.
0
Comment
Question by:Bryce Bassett
[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
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 37751656
You seem to be missing a declaration and instantiation of the Class that contains the application.
Option Explicit
'In the main module:
Dim wdAppClass As New Class1 'Class1 is the name of the Class module
Public wdPreview As Word.Document


Sub AutoExec()
    Set wdAppClass.wdPreviewApp = Word.Application
End Sub

Open in new window


Here is a full article on the subject:
http://word.mvps.org/faqs/macrosvba/appclassevents.htm
0
 

Author Closing Comment

by:Bryce Bassett
ID: 37759939
Thanks.  This is still a bit of a mystery to me, but by following your sample and the article, I got it to work.

I'm curious why you use Dim rather than Public when declaring wdAppClass the first time?

Thanks again.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37760239
For clarity, perhaps I should have explicitly used the word Public. Dim in the declaration section of modules is treated as Public. The word Private would have to be used if the variable is not meant to be Public.

Dims in form code are the opposite, and are the same as Private, so they are only accessible from the form code. If a Public variable is needed there, it must be explicitly declared as such.

Also, if you see the word Global used anywhere, be aware that that is precisely the same as Public.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

718 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