Here's the application. I'm building a Find and Replace dialog box, but one
that just works on styles.

For the Find list, it's easy to get the styles in use from the active
document.

The Replace list is supposed to come from the template attached to the
document.

So the idea is you can find any style in the document, but you can only
replace them with "valid" styles as defined in the template.

To get the styles from the attached template ... That's the problem. I have
to avoid creating a new document from the target template, as that will
trigger the ThisDocument > New processing.

Here's what I finally settled on.

Set objTemplate = Documents.Add( _
Template:=NormalTemplate.FullName, _
DocumentType:=wdNewBlankDocument, _
Visible:=False)
objTemplate.CopyStylesFromTemplate (ActiveDocument.AttachedTemplate.FullName)
For Each objStyle In objTemplate.Styles
If objStyle.InUse = True Then
Me.cboWithStyleName.AddItem (objStyle.NameLocal)
End If
Next objStyle
objTemplate.Close savechanges:=wdDoNotSaveChanges


The idea is to create a new document from Normal.dot (which has no
ThisDocument > New code) and copy the styles from the target active document
into the new document. Then I load the styles from that document into the
list and close the dummy document.

BUT

For some reason when Visible:=False, the ThisDocument > New processing for
the target document runs anyway! Try it. You'll see.

Anybody know how to avoid this? Anybody know a slicker way of getting a list
of the style names from the active document's attached template?

Bear
--
Windows XP, Word 2000

Re: Getting the styles from a template by Jay

Jay
Tue Dec 04 19:30:57 PST 2007

Hi Bear,

I think you can avoid the whole problem and just open the template as
a document (as suggested in the Help topic of the Styles collection)
if you suppress the New processing. I know you can do this with this
command (see
http://www.word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm):

WordBasic.DisableAutoMacros 1

if your template uses AutoNew; I'm not so sure about Document_New.

On Tue, 4 Dec 2007 16:03:01 -0800, Bear <david.chinell@ge.com(nospam)>
wrote:

>Here's the application. I'm building a Find and Replace dialog box, but one
>that just works on styles.
>
>For the Find list, it's easy to get the styles in use from the active
>document.
>
>The Replace list is supposed to come from the template attached to the
>document.
>
>So the idea is you can find any style in the document, but you can only
>replace them with "valid" styles as defined in the template.
>
>To get the styles from the attached template ... That's the problem. I have
>to avoid creating a new document from the target template, as that will
>trigger the ThisDocument > New processing.
>
>Here's what I finally settled on.
>
>Set objTemplate = Documents.Add( _
> Template:=NormalTemplate.FullName, _
> DocumentType:=wdNewBlankDocument, _
> Visible:=False)
>objTemplate.CopyStylesFromTemplate (ActiveDocument.AttachedTemplate.FullName)
>For Each objStyle In objTemplate.Styles
> If objStyle.InUse = True Then
> Me.cboWithStyleName.AddItem (objStyle.NameLocal)
> End If
>Next objStyle
>objTemplate.Close savechanges:=wdDoNotSaveChanges
>
>
>The idea is to create a new document from Normal.dot (which has no
>ThisDocument > New code) and copy the styles from the target active document
>into the new document. Then I load the styles from that document into the
>list and close the dummy document.
>
>BUT
>
>For some reason when Visible:=False, the ThisDocument > New processing for
>the target document runs anyway! Try it. You'll see.
>
>Anybody know how to avoid this? Anybody know a slicker way of getting a list
>of the style names from the active document's attached template?
>
>Bear

--
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.

Re: Getting the styles from a template by david

david
Wed Dec 05 06:57:00 PST 2007

Jay:

I appreciate your input, but the crux of my problem is in the display. I'm
trying to open the template "invisibly". With the method you suggest, it's
impossible to open the template without showing in. If I use
application.screenrefresh there's still quite a noticeable flicker.

The odd thing about the code I'm currently using, is that it only causes
execution of the ThisDocument > Document_New code the FIRST time I run the
code after opening the document. On subsequent runs, no problems.

Bear

--
Windows XP, Word 2000



Re: Getting the styles from a template by Jay

Jay
Wed Dec 05 07:52:38 PST 2007

Sorry, I think you're stuck.

Are you able to modify the Document_New code in the template? If so, you
might be able to put in code at the beginning that checks either the
.ActiveWindow.Visible or the .FullName property of the new document and
immediately exits if it's a dummy document.

--
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.

Bear wrote:
> Jay:
>
> I appreciate your input, but the crux of my problem is in the
> display. I'm trying to open the template "invisibly". With the method
> you suggest, it's impossible to open the template without showing in.
> If I use application.screenrefresh there's still quite a noticeable
> flicker.
>
> The odd thing about the code I'm currently using, is that it only
> causes execution of the ThisDocument > Document_New code the FIRST
> time I run the code after opening the document. On subsequent runs,
> no problems.
>
> Bear