gpt4 book ai didi

excel - ActiveDocument.SaveAs2 在 Excel 2000 中不起作用,但在 2010 和 2016 中正常

转载 作者:行者123 更新时间:2023-12-01 18:33:38 24 4
gpt4 key购买 nike

我在另一个线程中使用了 Jtchase08 提供的代码的修改版本,当我将对象库更改为相关的 Microsoft Word 版本时,它在 Excel 2010 和 2016 中工作正常,但是试图使同样的事情在2000 我得到

Run-time error '438': Object doesn't support this property or method

调试将我带到这里

Screenshot

我正在使用的完整代码如下,如果有人可以帮助修改它以使其在 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用来。对于 Office 2010 及更高版本,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.

关于excel - ActiveDocument.SaveAs2 在 Excel 2000 中不起作用,但在 2010 和 2016 中正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43342512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com