Solved

Class Module problem in Word VBA

Posted on 2012-03-21
3
309 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:versatilebb
[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 500 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:versatilebb
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

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

726 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