问题
I am trying to figure out if a document is a simple document (.doc, .docx, .docm) or a template (.dot, .dotx, .dotm).
I know there are several ways to figure this out (like check file extension), but I am looking for the most "safe" one.
My current guess is:
Private Function isTemplate(ByVal Doc As Document) As Boolean
isTemplate = (Doc.FullName = Doc.AttachedTemplate.FullName)
End Function
This works as expected, but I would like to know if there is a more fashioned way to achieve this goal. Maybe some missing isTemplate property? dunno.
Why: I've created a code on a template to don't allow a user to save a file if it is not password protected. The code works like a charm, but it bores me when I am editing something in the template because it doesn't have a password.
回答1:
Thanks to kmote, I found the solution.
Just check in the SaveFormat property if the document is a Template (here is the list of possible values for this property).
Private Function isTemplate(ByVal Doc As Document) As Boolean
Select Case Doc.SaveFormat
Case wdFormatTemplate, wdFormatDocument97, _
wdFormatXMLTemplate, wdFormatXMLTemplateMacroEnabled, _
wdFormatFlatXMLTemplate, wdFormatFlatXMLTemplateMacroEnabled
isTemplate = True
Case Else
isTemplate = False
End Select
End Function
来源:https://stackoverflow.com/questions/17643251/how-to-determine-if-document-is-a-template