Invalid Procedure Call or Argument 
Author Message
 Invalid Procedure Call or Argument

Why do I get this error when this is run?  Does anyone see anything wrong
with it?

CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\testfile.txt",
ForAppending).Write (Name)

Name is the variable I want to write.



Sun, 26 Jan 2003 03:00:00 GMT  
 Invalid Procedure Call or Argument

You don't have a left-hand argument for the CreateObject function.
CreateObject returns a reference to the created object:

   Set fso = CreateObject("Scripting.FileSystemObject")

Chad McCaffery
Microsoft Support / Newsgroup lurker
===================================

Quote:
> Why do I get this error when this is run?  Does anyone see anything wrong
> with it?

> CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\testfile.txt",
> ForAppending).Write (Name)

> Name is the variable I want to write.



Sun, 26 Jan 2003 03:00:00 GMT  
 Invalid Procedure Call or Argument
Hi Chad/Scott,

While Chad's method is neater, Scott's error appears to come from a missing
reference to the Scripting Runtime library.
The one-line solution works when properly referenced, and neither version
works wihout the reference.

HTH,
    Nick



Quote:
> You don't have a left-hand argument for the CreateObject function.
> CreateObject returns a reference to the created object:

>    Set fso = CreateObject("Scripting.FileSystemObject")

> Chad McCaffery
> Microsoft Support / Newsgroup lurker
> ===================================


> > Why do I get this error when this is run?  Does anyone see anything
wrong
> > with it?

CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\testfile.txt",

- Show quoted text -

Quote:
> > ForAppending).Write (Name)

> > Name is the variable I want to write.



Sun, 26 Jan 2003 03:00:00 GMT  
 Invalid Procedure Call or Argument
You don't need a reference with the CreateObject approach, Nick.  Drop this
in a module in new workbook and run it.  It lists all the subdirectories of
your C drive's root.

Sub ListFoldersOnC()
    Dim Counter As Integer
    Dim objFSO As Object, Fl As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Range("A1").CurrentRegion.ClearContents
    For Each Fl In objFSO.Drives("C").RootFolder.SubFolders
        Counter = Counter + 1
        Cells(Counter, 1).Value = Fl.Name
    Next
    Set objFSO = Nothing
End Sub

--
Jim Rech
Excel MVP



Sun, 26 Jan 2003 03:00:00 GMT  
 Invalid Procedure Call or Argument

That's true Nick, because "ForAppending" is defined by the Scripting Runtime
type library.

If Scott specified "Option Explicit" then he would have gotten "Variable not
defined" when he tried to run his code without the reference. Intead, VB(A)
created ForAppending on-the-fly to equal 0, which is an invalid value for
the OpenTextFile method.

Substituting "ForAppending" with 8 -- the enumerated value of "ForAppending"
constant -- then Scott's original code works without the reference:

    CreateObject("Scripting.FileSystemObject") _
        .OpenTextFile("c:\testfile.txt", 8) _
        .Write (Name)

I should have digged a little deeper with my first reply -- sorry 'bout
that.

Chad McCaffery
Microsoft Support / Newsgroup lurker
================================

Quote:
> Hi Chad/Scott,

> While Chad's method is neater, Scott's error appears to come from a
missing
> reference to the Scripting Runtime library.
> The one-line solution works when properly referenced, and neither version
> works wihout the reference.

> HTH,
>     Nick



> > You don't have a left-hand argument for the CreateObject function.
> > CreateObject returns a reference to the created object:

> >    Set fso = CreateObject("Scripting.FileSystemObject")

> > Chad McCaffery
> > Microsoft Support / Newsgroup lurker
> > ===================================


> > > Why do I get this error when this is run?  Does anyone see anything
> wrong
> > > with it?

> CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\testfile.txt",
> > > ForAppending).Write (Name)

> > > Name is the variable I want to write.



Sun, 26 Jan 2003 03:00:00 GMT  
 Invalid Procedure Call or Argument

Hi Jim,

I didn't _think_ you needed a reference with CreateObject, but that was the
only way I could reproduce the original problem (which of course I can't
seem to do now).  You will need a reference to get the value of the
ForAppending constant though.
So - the cause of the original problem?

Regards,
    Nick


Quote:
> You don't need a reference with the CreateObject approach, Nick.  Drop
this
> in a module in new workbook and run it.  It lists all the subdirectories
of
> your C drive's root.

> Sub ListFoldersOnC()
>     Dim Counter As Integer
>     Dim objFSO As Object, Fl As Object
>     Set objFSO = CreateObject("Scripting.FileSystemObject")
>     Range("A1").CurrentRegion.ClearContents
>     For Each Fl In objFSO.Drives("C").RootFolder.SubFolders
>         Counter = Counter + 1
>         Cells(Counter, 1).Value = Fl.Name
>     Next
>     Set objFSO = Nothing
> End Sub

> --
> Jim Rech
> Excel MVP



Sun, 26 Jan 2003 03:00:00 GMT  
 Invalid Procedure Call or Argument
Thanks Chad,

See my earlier reply to Jim re: the constant value.  I'd forgotten I'd
fiddled the Option Explicit statement to reproduce the original problem.

Regards,
    Nick



Quote:
> That's true Nick, because "ForAppending" is defined by the Scripting
Runtime
> type library.

> If Scott specified "Option Explicit" then he would have gotten "Variable
not
> defined" when he tried to run his code without the reference. Intead,
VB(A)
> created ForAppending on-the-fly to equal 0, which is an invalid value for
> the OpenTextFile method.

> Substituting "ForAppending" with 8 -- the enumerated value of
"ForAppending"
> constant -- then Scott's original code works without the reference:

>     CreateObject("Scripting.FileSystemObject") _
>         .OpenTextFile("c:\testfile.txt", 8) _
>         .Write (Name)

> I should have digged a little deeper with my first reply -- sorry 'bout
> that.

> Chad McCaffery



Sun, 26 Jan 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Error 5 Invalid Procedure call or argument

2. Invalid procedure call or argument on Dir functioon

3. VBA: Filecopy invalid procedure call or argument on Win NT workstation

4. Invalid procedure call or argument

5. Invalid Procedure call or argument [HTMLProject]

6. compile error "invalid procedure call or argument

7. Run-time error '5': Invalid procedure call or argument

8. Run-time error '5': Invalid procedure call or argument

9. Invalid procedure call or argument on Form.SetFocus

10. Invalid procedure call or argument: 'mid'

11. Function Error: Invalid procedure call or argument

12. Invalid Procedure Call or Argument

 

 
Powered by phpBB® Forum Software