AttchedTemplate of no use, if broken reference

L

Lauro

I want to populate an array with the name of the file based on a
template "MyTemplate.dot" also if the reference of the full path of
MyTemplate.dot is not valid anymore (for instance if the template is
not on the computer of the user or if it is in a different folder).

' -- I have a global variable
Public gArrFilesOpen() as string

'--using the following routine everything is OK only if Word is
'--finding "MyTemlate.dot" in the right place, otherwise
'--AttachedTemplate reports "normal.dot"

Function PopulateArray() as Long
'report -1 if an error occurs, otherwise the number of files found
dim objWordApp as Word.Application
dim objWordDoc as Word.Document
dim lngNumFileOpen as Long

On Error Goto Err_PopulateArray
lngNumFileOpen = 0
Set objWordApp = GetObject(, "Word.Application")
For Each objWordDoc In objWordApp.Documents
If objWordDoc.AttachedTemplate.Name = "MyTemplate.dot" Then
lngNumFileOpen = lngNumFileOpen + 1
ReDim Preserve gArrFilesOpen(lngNumFileOpen)
gArrFilesOpen(lngNumFileOpen) = objWordDoc.Name
End If
Next
PopulateArray= lngNumFileOpen

Exit_PopulateArray:
set objWordApp = nothing
set objWordDoc = nothing
exit Function
Err_PopulateArray:
lngNumFileOpen=-1
Resume Exit_PopulateArray
end Function

'--To include also the file with broken reference I tried to use the
'--right iformation provided by the window "Add-in and Templates"
' --so I used a simple function to get rid of the path and get only
'--the name of the template and then I used
'-- Dialogs(wdDialogToolsTemplates).Template instead of
'--Attachedtempalte as follows:

Function GetNameFromFullName(strFullName As String) As String
'just to get rid of the path
Dim i As Integer
i = InStrRev(strFullName, "\")
If i > 0 Then
GetNameFromFullName = Mid(strFullName, i + 1)
Else
GetNameFromFullName = ""
End If
End Function


Function PopulateArray() as Long
'report -1 if an error occurs, otherwise the number of files found
dim objWordApp as Word.Application
dim objWordDoc as Word.Document
dim lngNumFileOpen as Long

On Error Goto Err_PopulateArray
lngNumFileOpen = 0
Set objWordApp = GetObject(, "Word.Application")
For Each objWordDoc In objWordApp.Documents
If GetNameFromFullName( _ objWordDoc.Application.Dialogs( _
wdDialogToolsTemplates).Template) = "MyTemplate.dot" Then
lngNumFileOpen = lngNumFileOpen + 1
ReDim Preserve gArrFilesOpen(lngNumFileOpen)
gArrFilesOpen(lngNumFileOpen) = objWordDoc.Name
End If
Next
PopulateArray= lngNumFileOpen

Exit_PopulateArray:
set objWordApp = nothing
set objWordDoc = nothing
exit Function
Err_PopulateArray:
lngNumFileOpen=-1
Resume Exit_PopulateArray
end Function

'-- But the latter doesn't work because it all depend from the first
Word document found. It is like the information doesn't change for
every file.

Any suggestions?

Thanks, Lauro
 

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