ThisDocument Macros

F

Fuzzhead

I have the following macros in the ThisDocument of a Word Document that I am
having problems with. We have a third party program that we use to open Word
documents. When I open a document with these macros in it, it won’t work. But
if I have the original template opened and minimized it will work. Is there a
different way to do this without putting it in the ThisDocument?

Option Explicit
'reserve memory for an application variable
Private WithEvents wdApp As Word.Application

Private Sub Document_New()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub Document_Open()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
Dim Group As Bookmark
'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument Then Exit Sub
'get out of the header if we're in it
Select Case Sel.StoryType

Case wdMainTextStory
If Sel.Range.InRange(ActiveDocument.Bookmarks("PL").Range) Then
ActiveDocument.Bookmarks("P1").Select
frmplan.Show
Set frmplan = Nothing
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("CC").Range) Then
ActiveDocument.Bookmarks("C1").Select
frmCorc.Show
Set frmCorc = Nothing
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("WC").Range) Then
ActiveDocument.Bookmarks("W1").Select
frmWC.Show
Set frmWC = Nothing
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("AL").Range) Then
ActiveDocument.Bookmarks("A1").Select
frmRP.Show
Set frmRP = Nothing
End If

Exit Sub

End Select
End Sub
 
S

Shauna Kelly

Hi
I have the following macros in the ThisDocument of a Word Document

Is this code really in a Word document, or is it in a template? If it's in a
document, then can you tell us what you mean by "if I have the original
template opened". On the other hand, if it's in a template, then is this the
template attached to the documents that are opened by the third-party
program?

Finally, how do people create a new document? By doing File > New and
choosing a (this?) template? Or by using the third-party tool?

And... what version of Word are you using?

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
F

Fuzzhead

Hi Shauna,

We are using Word 2003. I created a template with the macros in it and then
loaded the template into the third-party program which then calls it a Word
Standard. People create new documents using that Work Standard in the
third-party program. If I have the template I created opened and go into the
third-party program and create a new document then the macros work. But if I
just go into the third-party program and create a new document without the
template opened the macros will not work.

I think the problem is when the template gets loaded into the third-party
program it just becomes a Word document and not a template any longer.

I hope this explains it a little better.
 
J

Jay Freedman

I don't think you're going to be able solve this problem by working on
it from the Word end (which is all we can answer). The third-party
program can do all sorts of things that could break your macros, such
as suppressing automatic macros. Without knowing what that program is
doing, you're left poking in the dark. Contact whoever supplied and
supports the third-party program for help.
 
F

Fuzzhead

I was thinking that was going to be the answer but I wanted to see if it
could be something in Word first.

THanks for the help.

Fuzzhead
 
F

Fuzzhead

Hi Russ,

I tried that and the only way that worked is if I have the template opened
at the same time that the third-party program opens a document. THen the
macros work. But if I close the template and only have the document opened
they do not work. It's like the document does not see the ThisDocument
module. Is there a way to force the document to ready the ThisDocument Module?
 
R

Russ

With the global template method, you for instance put the global template
into each user's Startup folder, like the website said, and whenever that
user's copy of Word starts up, it should 'inherit' all the 'attributes'
(buttons, menus, macros, styles, layout, boilerplate text, autotext, etc.)
of their global template.
Like Jay said, if the third party program is doing something 'out of the
ordinary', then that may break the normal global template method.
You might ask the third party program people whether they have some other
way to replicate the global template method (sometimes called an add-in).
They may limit what macros can be run for security reasons, but they should
have a way to 'sign' trusted macros to allow them to work.
 
S

Shauna Kelly

Hi

The global template solution from Russ might solve the problem.

If you're using the Document_New or Document_Open events in the ThisWorkbook
module, then another thing to try (instead of - not as well as - the global
template solution) is to replace them with Subs named AutoNew and AutoOpen
in an ordinary module.

In theory, they should work more or less the same. In practice, I sometimes
find the older AutoNew and AutoOpen work more reliably, especially on very
large documents.

(And if that makes a difference, please let us know!)

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top