3

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

Screenshot

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Parky
  • 35
  • 1
  • 6
  • It's probable that 2000 uses an earlier version of VBA, which is why it won't work. It might be possible to use an alternate subroutine which does the same job for 2000. Good question. – AStopher Apr 11 '17 at 09:58
  • 1
    it could be that you need to add a reference to word object library in vba – mojo3340 Apr 11 '17 at 10:00
  • @mo.h Please take your time to read the question, the OP has already done that: `when I change the object library to the relevant Microsoft word version`. – AStopher Apr 11 '17 at 10:01
  • @cybermonkey thanks. parky, i got an error on the same line but it said that the Filename is not valid. – mojo3340 Apr 11 '17 at 10:05
  • `SaveAs2` wasn't introduced until 2010. Before that is was `SaveAs`. The difference between the two is in added properties dealing with compatibility. Perhaps that is the problem here. It so, it wouldn't be in the code at all but in the file. – Variatus Apr 11 '17 at 10:46
  • 3
    You also could use `Application.Version` to determine the version and run the old `SaveAs` on version number `<14.0` and the new `SaveAs2` on the new versions – Pᴇʜ Apr 11 '17 at 11:08
  • Thanks for the help Peh, This is the last bit I need for this little project to work, is it as simple as just using SaveAs in place of SaveAs2 or is it totally diffierent code from the comment Creand and Save txt file? – Parky Apr 11 '17 at 13:40

1 Answers1

5

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.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73