Results 1 to 5 of 5

Thread: Name as to rename files

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    3

    Name as to rename files

    I'm a little new to VB and am mostly self-taught, as such my code is probably very inefficient and not done properly: Anyway, to my question, can someone tell me why the following code tells me that the "Name" line gives an Invalid procedure call or argument (Error 5). If I write a test code which does essentially the same thing (without all the search and automation) it works fine... why does the same command in two contexts not work the same?

    Code that won't work:
    VB Code:
    1. Sub PicRename()
    2.  
    3.     Dim day As String
    4.     day = InputBox("Please enter the folder name as it appears in my pics")
    5.     Dim hour As Integer
    6.     hour = InputBox("Please enter the latest hour (after midnight - on a 24h clock) for pictures to be converted")
    7.     Dim num As Integer
    8.     num = 0
    9.     Dim newnum As Integer
    10.     Dim changer As String
    11.     Dim newnam As String
    12.  
    13. alpha:
    14.     newnum = num + 24
    15.     With Application.FileSearch
    16.         .FileName = "0" & num & "????AA.JPG"
    17.         .LookIn = "C:\Documents and Settings\PCPC\My Documents\My Pictures\" & day & "\"
    18.         .Execute
    19.         If .FoundFiles.Count = 0 Then GoTo Omega
    20.         For i = 1 To 1
    21.             changer = .FoundFiles(i)
    22.             With Selection
    23.                 .TypeText (changer)
    24.                 .MoveLeft unit:=wdCharacter, Count:=10, Extend:=wdMove
    25.                 .MoveLeft unit:=wdCharacter, Count:=2, Extend:=wdExtend
    26.                 .TypeText (newnum)
    27.                 .HomeKey unit:=wdStory, Extend:=wdMove
    28.                 .EndKey unit:=wdStory, Extend:=wdExtend
    29.                 newnam = .Text
    30.                 .Delete
    31.             End With
    32.         Next i
    33.     End With
    34. 'line that doesn't work:    
    35. Name changer As newnam
    36.  
    37. GoTo alpha:
    38. Omega:
    39.     If num < hour Then
    40.         num = num + 1
    41.         GoTo alpha:
    42.     End If
    43.    
    44. End Sub

    Code that does work:
    VB Code:
    1. Sub Test()
    2.  
    3. Dim changer As String
    4. Dim newnam As String
    5. changer = "C:\Documents and Settings\PCPC\My Documents\My Pictures\2005_1224\005206AA.JPG"
    6. newnam = "C:\Documents and Settings\PCPC\My Documents\My Pictures\2005_1224\245206AA.JPG"
    7.  
    8. Name changer As newnam
    9.  
    10. End Sub

    If anyone could help me figure this out it would be greatly appreciated.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Name as to rename files

    What are the values of changer and newnam?

    It seems to me that changer will only contain the file name, and not the path (which is also needed).


    By the way, why are you using GoTo's the way you are? A "Do Loop" would be more appropriate, eg:
    VB Code:
    1. ..
    2. 'alpha:
    3. Do
    4. ...
    5.  
    6. ...
    7. 'GoTo alpha:
    8. 'Omega:
    9. '    If num < hour Then
    10. '        num = num + 1
    11. '        GoTo alpha:
    12. '    End If
    13.   num = num + 1
    14. Loop While num <= hour

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    3

    Re: Name as to rename files

    To be honest I didn't know about Do Loop or how to use it, thanks for that tip.

    When I debug the program 'changer' and 'newnam' both have the full path and filename. (In fact I copied and pasted the paths and filenames for my test program from the fields window when debugging the first program).

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Name as to rename files

    In that case the only think that comes to mind is that Application.FileSearch may still have a lock on the file.

    I haven't used it before, so I dont know if you need to close it somehow; it will probably be described in the help if you do.

    One thing I can suggest is to add a line containing "DoEvents" to the line after "End With". This should hopefully give Application.FileSearch time to release the file.

  5. #5

Posting Permissions

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



Click Here to Expand Forum to Full Width