这里有两件坏消息。
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