Solved

Class Module problem in Word VBA

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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