Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Dir Function 1

Status
Not open for further replies.

tyhand

Programmer
Jul 3, 2002
186
US
Hi all,

The following subs opens several files in one folder
(File Storage) and then saves the files to another
folder called 'BackUp' under different names.

The sub OpenDaFiles() works fine by itself,
but I get an "invalid procedure call or argument"
on *GetFile = Dir* when attempting to call
the sub SaveAsBackUp()

I don't know exactly what the cause is, but I
suspect that it's because the sub being called
also uses the Dir Function.

I've tried reffering back to the returned value
of the Dir Function *GetFile = Dir(MyPath)* in the
sub OpenDaFiles, but it doesn't work as it doesn't
loop properly.

Is there a way to programmatically get around this?
Any alternatives? I'm using WORD 97.
Sample code below.

Thanks. Peace!


Sub OpenDaFiles()
'
Dim MyPath, GetFile
ChangeFileOpenDirectory "C:\My Documents\File Storage\"
MyPath = "C:\My Documents\File Storage\"
GetFile = Dir(MyPath)
Do While GetFile <> &quot;&quot;
Documents.Open FileName:=GetFile, Format:=wdOpenFormatAuto
Call SaveAsBackUp ' possible error here!
GetFile = Dir ' possible error here!
Loop
End Sub

Public Sub SaveAsBackUp()
ChangeFileOpenDirectory &quot;C:\My Documents\BackUp\&quot;
Dim DestinationPath, DestPathFile
DestinationPath = &quot;C:\My Documents\BackUp\&quot;
' --> possible cause of error here? <--
DestPathFile = Dir(DestinationPath & MyFile)
If DestPathFile <> &quot;&quot; Then
msg = &quot;File &quot; & MyFile & &quot; already exists in &quot; & DestinationPath
msg = msg & vbCrLf & vbCrLf & &quot; Overwrite?&quot;
PromptMe = MsgBox(msg, vbExclamation + vbYesNo, &quot;Save As&quot;)
End If
If PromptMe = vbNo Then
Exit Sub
Else
ActiveDocument.SaveAs FileName:=Whatever, FileFormat:= _
wdFormatText
End If
ActiveDocument.Close
End Sub
 
>>I don't know exactly what the cause is, but I
>>suspect that it's because the sub being called
>>also uses the Dir Function

yes, that's it
this is what I do

Code:
Sub OpenDaFiles()
  Dim MyPath, GetFile
  Dim MyColl As Collection, i As Long
  
  ChangeFileOpenDirectory &quot;C:\My Documents\File Storage\&quot;
  
  MyPath = &quot;C:\My Documents\File Storage\&quot;
  
  Set MyColl = New Collection
  GetFile = Dir(MyPath)
  Do While GetFile <> &quot;&quot;
    MyColl.Add GetFile
    GetFile = Dir
  Loop
  For i = 1 To MyColl.Count
    GetFile = MyColl(i)
    Documents.Open FileName:=GetFile, Format:=wdOpenFormatAuto
    Call SaveAsBackUp
  Next i
  Set MyColl = Nothing
End Sub
 
Thanks Justin!

the alternative worked just fine!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top