Invalid Procedure Call

P

Pops Jackson

I have the following code I am trying to execute:

At w = Dir() near the end of the procedure, I get the "Invalid Procedure
Call or Argument" error message. I thought I knew what to do about this but
nothing works.

Any ideas welcomed.

Sub CopyAll()
Dim VBComp As VBIDE.VBComponent
Dim FName As String
Dim w As String
w = Dir("C:\Users\Jim\Documents\MsExcel\*.xls")

Do While w <> ""

Workbooks.Open (w)
With Workbooks(w)

FName = .Path & "\code.txt"
If Dir(FName) <> "" Then
Kill FName
End If
For Each VBComp In .VBProject.VBComponents
If VBComp.Type <> vbext_ct_Document Then
VBComp.Export FName
ThisWorkbook.VBProject.VBComponents.Import FName

Kill FName
End If
Next VBComp
End With
Workbooks(w).Close
w = Dir() ' Error Message "Invalid Procedure Call or Argument"

Loop
End Sub
 
D

Dave Peterson

I think it's because you're stacking calls to Dir().

Option Explicit
Sub CopyAll()
Dim VBComp As VBIDE.VBComponent
Dim FName As String
Dim w As String
Dim myFolder As String

myFolder = "C:\Users\Jim\Documents\MsExcel\"

w = Dir(myFolder & "*.xls")

Do While w <> ""
Workbooks.Open myFolder & w 'changed to include path
With Workbooks(w)
FName = .Path & "\code.txt"

'just delete it if it's there
'ignore any error if it's not
On Error Resume Next
Kill FName
On Error GoTo 0

For Each VBComp In .VBProject.VBComponents
If VBComp.Type <> vbext_ct_Document Then
VBComp.Export FName
ThisWorkbook.VBProject.VBComponents.Import FName
Kill FName
End If
Next VBComp

End With

Workbooks(w).Close Savechanges:=true 'don't you want to save it???
w = Dir()
Loop
End Sub
 
P

Pops Jackson

Many thanks to both of you for your responses and help. I do indeed wish to
save the file and added the code as suggested as well as the "myfolder"
lines.

I finally got past the error by adding "Kill Dir()" just above "w = Dir()".

Thanks again,

Jim
 
D

Dave Peterson

I don't understand how that worked, but glad you do!

Pops said:
Many thanks to both of you for your responses and help. I do indeed wish to
save the file and added the code as suggested as well as the "myfolder"
lines.

I finally got past the error by adding "Kill Dir()" just above "w = Dir()".

Thanks again,

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top