2016-11-06 84 views
0

(使用Windows 10和MS Word 2016年全球模板是:Normal.dotx和Autoload.dotm附加的模板有些文檔是:Reference.dotx)微軟Word VBA:獲取文檔的附加模板

大家好,

我在VBA中獲取文檔的附加模板時出現問題。

我有一個全局模板,當我加載MS Word時調用Autoload.dotm。但是,對於某些特定的文檔,它們使用附加的模板,而不是全局模板(Autload.dotm)或常規模板(Normal.dotx)。這個附加模板被稱爲Reference.dotx。

所以我使用ActiveDocument.AttachedTemplate。但是這會返回Autoload.dotm,而不是Reference.dotx。我需要找出在Developer-> Document Template-> Templates選項卡 - >文檔模板中定義的附加模板是否爲Reference.dotx。 (不要以爲它會有所作爲,但選中「自動更新文檔樣式」複選框。)有誰知道如何查找文檔是否使用了Reference.dotx?我不需要任何返回的全局模板。

我使用的是設法得到附加的模板的代碼很簡單:

If (ActiveDocument.AttachedTemplate = "Reference.dotx") Then 
     PrepareDocument_enabled = True 
    End If 
+0

難道說是錯誤的路徑?你可以使用'AttachedTemplate.Name和.Path'。你有沒有嘗試過'Debug.print activedocument.attachedtemplate.name'或'.path'? – Niclas

+0

Nah。不幸的是,只是返回Normal.dotm。它甚至不會返回我添加的全局模板。:/ –

+1

而這正是問題(?)。它無法找到文檔reference.dotx。 – Niclas

回答

0

也許這將幫助你?它會顯示使用的模板。

Sub Macro1() 
Dim strPath As String 
    strPath = Dialogs(wdDialogToolsTemplates).Template 
    MsgBox strPath 
End Sub 

否則,你可以用它來改變模板

Sub ChangeAttachedTemplate() 
Dim oDoc As Document 
Dim oTemplate As Template 
Dim strTemplatePath As String 

Set oDoc = ActiveDocument 

If oDoc.Type = wdTypeTemplate Then Exit Sub 

Set oTemplate = oDoc.AttachedTemplate 

Debug.Print oTemplate.FullName 

' Path is probably: C:\Users\USERNAME\AppData\Roaming\Microsoft\Templates\ 
If InStr(UCase(oTemplate.FullName), UCase("Path of the template")) > 0 Then 
    oDoc.AttachedTemplate = "PATH TO TEMPLATE" & "TEMPLATE NAME.dotm" 
End If 
End Sub