How get MYTEMPLATE not NORMAL if reference broken

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
 
C

Cindy Meister

Hi Lauro

You say "But the latter doesn't work..." At the end of your message, after
all the code, but you don't specify what "the latter" is.

If you mean the last function in the code you show, then a couple of remarks
may help you

1. The dialog boxes in Word come from the "old days" of WordBasic. That
means they're very much related to the current selection in Word. In this
case, that would be the ActiveDocument. When you query a dialog box, it's
only going to return values for the active whatever. You can try using
objWordDoc.Activate before querying the dialog box.

2. The dialog box route is not always reliable in later versions of Word.
Microsoft hasn't been concerned about keeping this old technology (which they
consider "deprecated" just because it's old) working in newer versions. I
don't think that's the problem, in this case, but it bears keeping in mind.

3. You might try checking this property, to see if it returns the
information you need:
objWordDoc.BuiltInDocumentProperties("Template").value

-- Cindy
 
L

Lauro

Thanks Cindy for your answear and sorry for the crossposting.

Yes, I meant the last function.

I tried to use objWordDoc.Activate before reading the Dialog Property
and seems to work.

I have just stubled upon BuiltInDocumentProperties) and I have tried
it in this way:

'-- a global variable
gArrFilesOpen() as string

Function PopulateArray() As Long
'report -1 if an error occurs, otherwise the number of files found
Dim objWordApp As Object 'Word.Application -- early or late binding
Dim objWordDoc As Object ' Word.Document -- is the same
Dim lngNumFileOpen As Long

On Error GoTo Err_PopulateArray
lngNumFileOpen = 0
ReDim gArrFilesOpen(lngNumFileOpen)
Set objWordApp = GetObject(, "Word.Application")
For Each objWordDoc In objWordApp.Documents
If objWordDoc.BuiltInDocumentProperties( _
wdPropertyTemplate) = "MyTemplate.dot" Then
gArrFilesOpen(lngNumFileOpen) = objWordDoc.Name
lngNumFileOpen = lngNumFileOpen + 1
ReDim Preserve gArrFilesOpen(lngNumFileOpen)
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 whe I pass trough BuiltInDocumentProperties this code (which I run
in Access) CRASHES Word. Do you know why?

Thanks again, Lauro
 
C

Cindy Meister

Hi Lauro

No, I don't know why it would crash Word. But there are some
trouble-shooting steps you can try to see if you can narrow down the problem.

First, though, a remark about a potential problem in the code you posted:
You should always release object variables in the reverse order in which
they're instantiated. so Set objWordDoc = Nothing should precede Set
objWordapp = Nothing

Strictly speaking, you should be asking for the .VALUE property of a
Document property. It's possible, since you don't specify this, that the code
could be causing the crash.

Something else to test is whether querying this variable from within a Word
macro causes any problems in the document(s) you're testing.

The next thing I'd do is to set up a function that basically just requests
this property (and shows the result in a MsgBox, for example), in order to
track down whether it's the call to the property, or something else that's
causing the problem.

-- Cindy
 
L

Lauro

Hi Cindy,

I have tried a very simple routine from a new database in Access

Public Sub pp()
Dim objWordApp As Object
Dim objWordDoc As Object

Set objWordApp = GetObject(, "Word.Application")
For Each objWordDoc In objWordApp.Documents
MsgBox objWordDoc.BuiltInDocumentProperties(6) & " " & _
objWordDoc.Name
Next
End Sub

And also this one crashes Word.

But I noticed that the crash happens only on a my particular document.

I've Tried to open this Document alone in Word and click on
File|Properties and strange enough also just in Word without any VBA
when I click OK sometime (not always) Word crashes.

Ciao Lauro
 
L

Lauro

Hi Cindy,

the mistery continues...

I tried to open the Culprit Document with Word on another computer. It
still crashes Word when I do File|Properties and then OK.
I tried with a similar Document (based on the same Template) and is
OK. Both documents are based on a Template not present in the
computer.

The crash happens only if the Culprit Document is the only one opened
in Word.

Do you think that that in someway that document got corrupted and I
don't have to warry with the others?

Thanks, Lauro
 
C

Cindy Meister

Hi Lauro

Yes, I'd guess that this document's internal structures are damaged. This
doesn't mean you'd never have to worry about others; the same could
conceivably happen to them. But it does mean the problem is not with your
application code :)

You could try copying all the text in the problem file - WITHOUT the last
paragraph mark - to a new document and see if this removes the problem. If it
does, you'll need to recreate headers, footers, document properties etc. But
at least you can save the main part of the document.

-- Cindy
 
L

Lauro

Thanks for the help

Lauro


Hi Cindy,

the mistery continues...

I tried to open the Culprit Document with Word on another computer. It
still crashes Word when I do File|Properties and then OK.
I tried with a similar Document (based on the same Template) and is
OK. Both documents are based on a Template not present in the
computer.

The crash happens only if the Culprit Document is the only one opened
in Word.

Do you think that that in someway that document got corrupted and I
don't have to warry with the others?

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