How to determine if document is a template?

一曲冷凌霜 提交于 2019-12-12 15:14:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!