Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

ThisDocument Macros

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fuzzhead - 06 Oct 2007 00:51 GMT
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
Shauna Kelly - 06 Oct 2007 02:24 GMT
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

>I have the following macros in the ThisDocument of a Word Document that I
>am
[quoted text clipped - 54 lines]
> End Select
> End Sub
Fuzzhead - 06 Oct 2007 16:24 GMT
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.

>"Shauna Kelly" wrote:

> Hi
>
[quoted text clipped - 13 lines]
> Shauna Kelly.  Microsoft MVP.
> http://www.shaunakelly.com/word
Jay Freedman - 06 Oct 2007 17:33 GMT
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.

>Hi Shauna,
>
[quoted text clipped - 30 lines]
>> Shauna Kelly.  Microsoft MVP.
>> http://www.shaunakelly.com/word

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Fuzzhead - 06 Oct 2007 18:02 GMT
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

> 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
[quoted text clipped - 43 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Russ - 07 Oct 2007 01:00 GMT
Maybe you really want to use a global template?
See this information. It applies to Windows, too, except for the pathname
punctuation, of course.
http://www.mcgimpsey.com/macoffice/word/globaltemplate.html

> I was thinking that was going to be the answer but I wanted to see if it
> could be something in Word first.
[quoted text clipped - 53 lines]
>> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
>> all may benefit.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Fuzzhead - 07 Oct 2007 16:18 GMT
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?



> Maybe you really want to use a global template?
> See this information. It applies to Windows, too, except for the pathname
[quoted text clipped - 58 lines]
> >> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> >> all may benefit.
Russ - 07 Oct 2007 20:22 GMT
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.

> Hi Russ,
>
[quoted text clipped - 74 lines]
>>>> so
>>>> all may benefit.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Shauna Kelly - 08 Oct 2007 00:07 GMT
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

> Hi Russ,
>
[quoted text clipped - 81 lines]
>> >> newsgroup so
>> >> all may benefit.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.