ActiveDocument.SaveAs2 在 Excel 2000 年不工作,但在 2010 年和 2016 年工作正常
ActiveDocument.SaveAs2 not working in Excel 2000 but fine in 2010 and 2016
我在另一个线程中使用了 Jtchase08 提供的代码的修改版本,当我将对象库更改为相关的 Microsoft word 版本时,它在 Excel 2010 和 2016 中工作正常,但是试图让同样的东西在 2000 年工作我得到
Run-time error '438': Object doesn't support this property or method
调试带我到这里
我正在使用的完整代码如下,如果有人可以帮助修改它以在 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
我认为最好的解决方案是
If Val(Application.Version) < 14 Then
AppWord.ActiveDocument.SaveAs Filename:=DocPath, FileFormat:=wdFormatText
Else
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
End If
因此对于 Office 2010 之前的版本,使用 old function SaveAs
is used. And for Office 2010 and newer the new function SaveAs2
。
Information
The SaveAs2
function was introduced in Office 2010.
As I know the only difference is that the SaveAs2
function takes an additional (last) argument CompatibilityMode
(see WdCompatibilityMode Enumeration).
So the old SaveAs
might work in new versions as well, because it is still implemented for compatibility reasons. But we never know if it gets removed in any future versions so with the solution above you get compatibility to future versions in case the old SaveAs
gets removed from VBA.
我在另一个线程中使用了 Jtchase08 提供的代码的修改版本,当我将对象库更改为相关的 Microsoft word 版本时,它在 Excel 2010 和 2016 中工作正常,但是试图让同样的东西在 2000 年工作我得到
Run-time error '438': Object doesn't support this property or method
调试带我到这里
我正在使用的完整代码如下,如果有人可以帮助修改它以在 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
我认为最好的解决方案是
If Val(Application.Version) < 14 Then
AppWord.ActiveDocument.SaveAs Filename:=DocPath, FileFormat:=wdFormatText
Else
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
End If
因此对于 Office 2010 之前的版本,使用 old function SaveAs
is used. And for Office 2010 and newer the new function SaveAs2
。
Information
TheSaveAs2
function was introduced in Office 2010.
As I know the only difference is that theSaveAs2
function takes an additional (last) argumentCompatibilityMode
(see WdCompatibilityMode Enumeration).So the old
SaveAs
might work in new versions as well, because it is still implemented for compatibility reasons. But we never know if it gets removed in any future versions so with the solution above you get compatibility to future versions in case the oldSaveAs
gets removed from VBA.