這裏有兩件壞消息。
1)文件必須有一個模板。你不能刪除它,只能將它改成別的東西。
2)改變一個模板不會做任何事情。見this page。
我在想,如果Open方法的問題是您試圖打開「.doc」擴展名文件,而不是現代的「.docx」擴展名文件。你鏈接的VBA子程序只做「.doc」文件。此VBA代碼確實有兩個:
Function StringEndsWith(_
ByVal strValue As String, _
CheckFor As String) As Boolean
Dim sCompare As String
Dim lLen As Long
lLen = Len(CheckFor)
If lLen > Len(strValue) Then Exit Function
sCompare = Right(strValue, lLen)
StringEndsWith = StrComp(sCompare, CheckFor, vbTextCompare) = 0
End Function
Sub ChangeTemplates()
Dim strDocPath As String
Dim strTemplateB As String
Dim strCurDoc As String
Dim docCurDoc As Document
' set document folder path and template strings
strDocPath = "C:\tmp\"
' get first doc - only time need to provide file spec
strCurDoc = Dir(strDocPath & "*.doc*")
' ready to loop (for as long as file found)
Do While strCurDoc <> ""
If (StringEndsWith(strCurDoc, ".doc") Or StringEndsWith(strCurDoc, ".docx")) Then
' open file
Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc)
' change the template back to Normal
docCurDoc.AttachedTemplate = ""
' save and close
docCurDoc.Close wdSaveChanges
End If
' get next file name
strCurDoc = Dir
Loop
MsgBox "Finished"
End Sub
對不起,我對VBA /宏的引用以及'visual-studio-2010'和'vba'標籤有點困惑。 VBA是Office內部使用的宏語言。你是否試圖通過VS或VSTO中的Interop來做到這一點(這不會是一個VBA應用程序,這將是一個VS應用程序)? 「Document.Open」的哪一部分讓你起牀?你是否只是試圖將它放在'Document.Open'事件接收器中而引用了代碼? – 2011-03-30 17:26:46
我已經更改了你的標籤以符合你的問題,但如果我弄錯了,請更改它們並澄清你的問題。請詳細說明Document.Open的情況。 – 2011-03-31 19:49:57
Hej ...你是否在你提到的頁面上尋找第一或第二宏的幫助? – MikeD 2011-03-31 20:39:08