Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
rustyfishbones
Master II
Master II

Macro to Move File

Hi All,

I have taken a Macro that deletes a file and tried to modify it to Move a file, here is what I have, I get no errors, it does not move the file, it just takes me back into the Edit Module screen, any ideas?

FUNCTION MoveFile(mFile)

SET oFile = CREATEOBJECT("Scripting.FileSystemObject")

currentStatus = oFile.FileExists(mFile)

IF currentStatus = TRUE THEN

oFile.MoveFile(mFile)

END IF

SET oFile = Nothing

END FUNCTION

SUB MoveFiles

SET doc = ActiveDocument

    SET mySelections = doc.fields("Full Path").GetSelectedValues

   

    FOR i = 0 to mySelections.Count - 1

  

    MoveFile mySelections.Item(i).text, "C:\Users\alan\Desktop\"

     

       

    NEXT

  

END SUB

1 Solution

Accepted Solutions
marcus_sommer

I would rather try something like this:

SUB MoveFile(source, destination)

SET oFile = CREATEOBJECT("Scripting.FileSystemObject")

currentStatus = oFile.FileExists(source)

IF currentStatus = TRUE THEN

     oFile.MoveFile(source, destination)

END IF

SET oFile = Nothing

END SUB

SUB MoveFiles

SET doc = ActiveDocument

SET mySelections = doc.fields("Full Path").GetSelectedValues

    FOR i = 0 to mySelections.Count - 1

       file = mySelections.Item(i).text

       call MoveFile(file, "C:\Users\alan\Desktop\")

       doc.GetApplication.Sleep 1000

    NEXT

END SUB

- Marcus

View solution in original post

2 Replies
marcus_sommer

I would rather try something like this:

SUB MoveFile(source, destination)

SET oFile = CREATEOBJECT("Scripting.FileSystemObject")

currentStatus = oFile.FileExists(source)

IF currentStatus = TRUE THEN

     oFile.MoveFile(source, destination)

END IF

SET oFile = Nothing

END SUB

SUB MoveFiles

SET doc = ActiveDocument

SET mySelections = doc.fields("Full Path").GetSelectedValues

    FOR i = 0 to mySelections.Count - 1

       file = mySelections.Item(i).text

       call MoveFile(file, "C:\Users\alan\Desktop\")

       doc.GetApplication.Sleep 1000

    NEXT

END SUB

- Marcus

rustyfishbones
Master II
Master II
Author

Hi Marcus,

That worked perfectly, although I had to remove parentheses from

oFile.MoveFile(source, destination)