简体   繁体   中英

ActiveDocument.SaveAs2 not working in Excel 2000 but fine in 2010 and 2016

I have used a modified version of code supplied by Jtchase08 in another thread and it works fine in Excel 2010 and 2016 when I change the object library to the relevant Microsoft word version, however in an attempt to make the same thing work in 2000 I get

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

Debug takes me to here

截图

The full code I am using is below, if anyone can help modify this to work in 2000 it would be much appreciated.

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

I think the best solution would be

If Val(Application.Version) < 14 Then
    AppWord.ActiveDocument.SaveAs Filename:=DocPath, FileFormat:=wdFormatText
Else
    AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
End If

So for versions before Office 2010 the old function SaveAs is used. And for Office 2010 and newer the new function SaveAs2 is used.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM