Par*_*rky 3 excel vba excel-2010 excel-2000 excel-2016
我在另一个线程中使用了 Jtchase08 提供的修改后的代码版本,当我将对象库更改为相关的 Microsoft Word 版本时,它在 Excel 2010 和 2016 中工作正常,但是为了使相同的事情在 2000 年工作,我得到了
运行时错误“438”:对象不支持此属性或方法
调试带我到这里
我正在使用的完整代码如下,如果有人可以帮助修改它以在 2000 年工作,将不胜感激。
Sub ExportToHTML()
Dim DocPath As String
Dim MsgBoxCompleted
Worksheets("Final Code").Activate
Worksheets("Final Code").Range("A1:A322").Select
Dim AppWord As Object
Set AppWord = CreateObject("Word.Application")
AppWord.Visible = False
Selection.Copy
DocPath = CurDir & Application.PathSeparator & Range("U15")
'Create and save txt file
AppWord.Documents.Add
AppWord.Selection.Paste
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
Application.CutCopyMode = False
AppWord.Quit (wdDoNotSaveChanges)
Set AppWord = Nothing
MsgBoxCompleted = MsgBox("Process complete.", vbOKOnly, "Process complete")
Worksheets("User Input").Activate
End Sub
Run Code Online (Sandbox Code Playgroud)
我认为最好的解决方案是
If Val(Application.Version) < 14 Then
AppWord.ActiveDocument.SaveAs Filename:=DocPath, FileFormat:=wdFormatText
Else
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
End If
Run Code Online (Sandbox Code Playgroud)
因此对于 Office 2010 之前的版本,使用旧功能SaveAs
。对于 Office 2010 和更新版本,使用新功能SaveAs2
。
信息
该SaveAs2
函数是在 Office 2010 中引入的。
据我所知,唯一的区别是该SaveAs2
函数需要一个额外的(最后一个)参数CompatibilityMode
(请参阅WdCompatibilityMode枚举)。所以旧
SaveAs
版本也可以在新版本中工作,因为出于兼容性原因它仍然被实现。但是我们永远不知道它是否会在任何未来版本中被删除,因此使用上面的解决方案,您可以获得与未来版本的兼容性,以防旧版本SaveAs
从 VBA 中删除。