Solved

Class Module problem in Word VBA

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Problem: You created a new custom form in Outlook for your contacts (added fields, deleted fields, changed the layout of fields, whatever) and made it the default form for contacts. The good news is that all new contacts will utilize the new form. T…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now