我创建了一个函数来保存模板并退出连接到Excel的文档 . 我认为它在Excel中存在一个严重的缺陷,即连接无法从代码中断开 . 我也没有找到识别对象的方法(当工作表上有更多的.dot模板时) . 我现在正在使用AltText .
Public Function SaveFileFromObject(EW As Excel.Worksheet _
, sAltText As String, sFilename As String) As Boolean
On Error GoTo EH
Dim OLE As OLEObject, WD As Word.Document, bFound As Boolean
bFound = False
For Each OLE In EW.OLEObjects
If InStr(1, OLE.progID, "Word.Document", vbTextCompare) > 0 Then
If OLE.ShapeRange.AlternativeText = sAltText Then
OLE.Verb xlOpen
Set WD = OLE.Object
WD.SaveAs sFilename, FileFormat:=WdSaveFormat.wdFormatDocument97
WD.Close False
bFound = True
Exit For
End If
End If
Next
SaveFileFromObject = bFound
EX: On Error Resume Next
Set OLE = Nothing
Set WD = Nothing
On Error GoTo 0
Exit Function
EH: MsgBox "Error in SaveFileFromObject: '" & err.Description & "'"
SaveFileFromObject = False
Resume EX
Resume ' debug code
End Function