ActiveDocument.SaveAs2不在Excel 2000中工作,但在2010年和2016年表现良好
我在另一个线程中使用了Jtchase08提供的代码的修改版本,当我将对象库更改为相关的Microsoft word版本时,它在Excel 2010和2016中运行良好,但是在2000年尝试使同样的事情工作时,我得到了 运行时错误“438”:对象不支持此属性或方法 调试带我到这里 下面是我正在使用的完整代码,如果有人能帮助修改此代码使其在2000年工作,将不胜感激ActiveDocument.SaveAs2不在Excel 2000中工作,但在2010年和2016年表现良好,excel,vba,excel-2010,excel-2016,excel-2000,Excel,Vba,Excel 2010,Excel 2016,Excel 2000,我在另一个线程中使用了Jtchase08提供的代码的修改版本,当我将对象库更改为相关的Microsoft word版本时,它在Excel 2010和2016中运行良好,但是在2000年尝试使同样的事情工作时,我得到了 运行时错误“438”:对象不支持此属性或方法 调试带我到这里 下面是我正在使用的完整代码,如果有人能帮助修改此代码使其在2000年工作,将不胜感激 Sub ExportToHTML() Dim DocPath As String Dim MsgBoxCo
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
我认为最好的解决办法是
If Val(Application.Version) < 14 Then
AppWord.ActiveDocument.SaveAs Filename:=DocPath, FileFormat:=wdFormatText
Else
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
End If
如果Val(Application.Version)<14,则
AppWord.ActiveDocument.SaveAs文件名:=DocPath,文件格式:=wdFormatText
其他的
AppWord.ActiveDocument.SaveAs2文件名:=DocPath,文件格式:=wdFormatText
如果结束
因此,对于Office 2010之前的版本,将使用。对于Office 2010及更新版本,将使用
信息Office 2010引入了
SaveAs2
功能。据我所知,唯一的区别是
SaveAs2
函数接受一个附加(最后)参数CompatibilityMode
(请参见枚举)
因此,旧的SaveAs
也可以在新版本中使用,因为出于兼容性原因,它仍然可以实现。但是我们永远不知道它是否会在将来的版本中被删除,因此,如果旧的SaveAs
从VBA中删除,那么使用上面的解决方案,您可以与将来的版本兼容
2000很可能使用了VBA的早期版本,这就是它无法工作的原因。可能会使用一个替代子程序,在2000年执行相同的任务。好问题。可能是您需要在中添加对word对象库的引用vba@mo.h请慢慢阅读问题,OP已经完成了:
当我将对象库更改为相关的Microsoft word版本时
@cybermonkey谢谢。parky,我在同一行上遇到一个错误,但它说文件名无效。您还可以使用应用程序.Version
确定版本,并在版本号上运行旧的SaveAs
,非常感谢,这是我需要的解决方案。