Getting the styles from a template

B

Bear

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
 
J

Jay Freedman

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

Bear

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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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