Solved

Class Module problem in Word VBA

Posted on 2012-03-21
3
305 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

786 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