Find non normal templates

J

jocker

I have several hundred documents received from other offices and am
attempting to list the templates which are not "normal.dot". The attached
VBA lists both normal and non-normal however. Can some kind soul help.

Sub not_normal_search()
Dim i As Long, currentDoc As Document, listingdoc As Document, z As Long
Dim docname As String, docpath As String, currentRange As Range
Documents.Add
Set listingdoc = ActiveDocument
With ActiveDocument.PageSetup
.LeftMargin = CentimetersToPoints(1.6)
.RightMargin = CentimetersToPoints(1.6)
End With
With Application.FileSearch
.NewSearch
.LookIn = "G:\a_cv_test_new\"
.FileType = msoFileTypeWordDocuments
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Documents.Open FileName:=.FoundFiles(i)
Set currentDoc = ActiveDocument
Set mytemplate = ActiveDocument.AttachedTemplate

listingdoc.Activate
'''''''''''
If Dialogs(wdDialogToolsTemplates).Template <> "normal" Then _
Selection.TypeText Dialogs(wdDialogToolsTemplates).Template & vbCr Else GoTo
q
MsgBox Dialogs(wdDialogToolsTemplates).Template
q:

currentDoc.Close wdDoNotSaveChanges

Next i
End If
End With
ScreenUpdating = True
AnimateScreenMovements = False
Application.Visible = True
ChangeFileOpenDirectory "G:\a_CV_test\"
ActiveDocument.SaveAs FileName:="no details.doc"
Documents.Open FileName:="no details.doc"

End Sub
 
J

Jonathan West

Hi jocker,

The problem with your approach is that when you open a document and if the
document is based on a template not available to you, the document
automatically attaches to normal.dot instead.

The attached template is one of the document properties that can be read
using the dsofile.dll add-in available from Microsoft. I have produced a
Word add-in that makes use of dsofile, which allows you to select a folder
and produce a file listing, specifying whichever properties you want to
include. Take a look here.

Getting access to the Document Properties of a Word file
http://word.mvps.org/FAQs/MacrosVBA/DSOFile.htm
 
J

jocker

Many thanks, Jonathan. What would we all do without you.
HOWEVER
I'm loath to produce a list of over 13,000 documents, is there a way to
simply select templates which are not "normal"
These are CVs from my daughter's office

regards from a 68 newcomer to VBA

jocker
++++++++++++++
 
J

Jonathan West

jocker said:
Many thanks, Jonathan. What would we all do without you.
HOWEVER
I'm loath to produce a list of over 13,000 documents, is there a way to
simply select templates which are not "normal"
These are CVs from my daughter's office

regards from a 68 newcomer to VBA

In principle yes. The code in the Listprops template is open. So you could
modify it so that documents are only listed where the template is not
normal.dot.

The change is quite minimal. In the frmFileList module, there is the
cmdCreateList_Click routine. This is the routine that needs alteration. In
that routine, scroll down until you come to the following line of code

For iPropIndex = 0 To SelectedProps.ListCount - 1

Before that line of code, insert the following

If Instr(LCase(oDocProp.Template), "normal.dot") = 0 Then


Then scroll down until you come to this line of code

MyRange.InsertParagraph

After that line of code, insert the following

End If


That should fix it so that only documents not based on normal.dot are
listed.
 
J

jocker

I'm getting the following error message:-
Run-Time error '91'
Object variable or With block variable not set

jocker
++++++++++++
 
J

Jay Freedman

Hi jocker,

The problem is that there are *two* lines in the procedure that both
say
For iPropIndex = 0 To SelectedProps.ListCount - 1
It sounds like you inserted the new If statement before the first of
these, but Jonathan meant the second one -- the one just before the
line
Select Case SelectedProps.List(iPropIndex)
The error occurs because at the first location the oDocProp object
hasn't been assigned a value yet.
 
J

jocker

How would I omit the listing of several templates.
Sorry if I'm pushing my luck

jocker
+++++++++++
 
J

Jay Freedman

Hi jocker,

That's not quite clear -- are you seeing the templates themselves in the
list, or are these documents based on certain templates?

If you want to suppress the listing of documents based on specific
templates, you do it the same way Jonathan suppressed documents based on
normal.dot. You just have to extend the If statement, for example:

If Instr(LCase(oDocProp.Template), "normal.dot") = 0 And _
Instr(LCase(oDocProp.Template), "notme.dot") = 0 Then

You can add more Instr(...) = 0 expressions, with And between them. Each
Instr expression will be True if the current document's template is *not*
the name in quotes. The whole If expression is True if all the Instr
expressions in it are True -- that is, if the current document's template is
not any of the listed names.

Note: when you put in your own template names, be sure to use all lower-case
to match the result of the LCase function.
 
Top