问题描述
我试图确定文档是简单文档(.doc,.docx,.docm)还是模板(.dot,.dotx,.dotm).
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.
我目前的猜测是:
Private Function isTemplate(ByVal Doc As Document) As Boolean
isTemplate = (Doc.FullName = Doc.AttachedTemplate.FullName)
End Function
这可以按预期工作,但是我想知道是否有一种更时尚的方式来实现这一目标.也许缺少isTemplate属性?邓诺.
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.
推荐答案
感谢kmote,我找到了解决方案.
Thanks to kmote, I found the solution.
只需签入 SaveFormat 属性,如果该文档是模板(这里是此属性的可能值列表.
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
这篇关于如何确定文档是否为模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!