+ Reply to Thread
Results 1 to 5 of 5

code from Excel Help doesn't work

  1. #1
    Registered User
    Join Date
    11-10-2010
    Location
    Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    11

    code from Excel Help doesn't work

    I have the code below copied verbatim from Excel help. I am trying to open a .txt file, add text to the end of the file and save the .txt file. I have created a test file that is c:\testfile.txt and cannot figure this simple procedure out. When I run I get Run-time error 5 - Invalid procedure call or argument. It is hanging up on the line that I underlined.

    Sub OpenTextFileTest()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.OpenTextFile("C:\testfile.txt", ForAppending, TristateUseDefault)
    f.write "Hello world!"
    f.Close
    End Sub
    Last edited by ryan darrow; 01-04-2011 at 01:21 PM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: code from Excel Help doesn't work

    Another way:

    Please Login or Register  to view this content.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: code from Excel Help doesn't work

    Please Login or Register  to view this content.
    Where and how exactly did you find the help example?
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: code from Excel Help doesn't work

    If you don't set a reference to Microsoft Scripting Runtime, then the values of the constants (e.g., ForAppending) are unknown (they come from the Scripting library).

    And since you didn't use Option Explicit, you just end up with an Empty variable instanced on the fly.

    EDIT: Just to elaborate: Using late binding as in your example avoids the need to set a reference, but in doing so, you must define all constants that may be associated with a given object whose values come from that library. Using Option Explicit is always a good practice, but for avoiding problems like these, it's essential.

    Something I don't know is whether your use of the last constant is correct. If a file is saved in Unicode and then opened as ASCII, I don't know whether that works.
    Last edited by shg; 01-04-2011 at 12:36 PM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    11-10-2010
    Location
    Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: code from Excel Help doesn't work

    I found it in the OpenTextFile Method section of the help in Excel. The 8 fixed it though. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1