Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Move Files (XP SP3)

    I need to move files from the C:AscendPrint folder to the C:AscendArchive folder as part of a Word macro. I found the FileSystemObject.MoveFile method, but I am not sure how to use it or if it is what I should use.

    Thanks!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Move Files (XP SP3)

    You could use code like this:

    Sub MoveEmUp()
    Const strSource = "C:AscendPrint*.doc"
    Const strTarget = "C:AscendArchive"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile strSource, strTarget
    Set fso = Nothing
    End Sub

  3. #3
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Files (XP SP3)

    I got a permission denied error, probably because I am printing tthe documents first. Here is what I have:

    Dim sMyDir As String
    Dim sDocName As String
    ' The path to obtain the files.
    Documents.Add
    sMyDir = "C:AscendPrint"
    sDocName = Dir(sMyDir & "*.DOC")
    While sDocName <> ""
    ' Print the file.
    Application.PrintOut FileName:=sMyDir & sDocName
    ' Get next file name.
    sDocName = Dir()
    Wend
    Const strSource = "C:AscendPrint*.doc"
    Const strTarget = "C:AscendArchive"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile strSource, strTarget
    Set fso = Nothing
    Application.Quit

    I assume I need to have it wait until the printing is finished to move the files, but not sure how to do that. Sorry I didn't post this first...

    Thanks again

  4. #4
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Files (XP SP3)

    Found your solution in another post and added the Background:=False to the PrintOut. (I will look longer next time.)

    Thanks, Hans!

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Move Files (XP SP3)

    Word normally prints in the background, i.e. the macro continues while the documents are still being printed. You can turn off background printing temporarily:

    Dim fBackgroundPrinting As Boolean
    ' Save status
    fBackgroundPrinting = Options.PrintBackground
    ' Turn off background printing
    Options.PrintBackground = False
    ' Code to print documents goes here
    ...
    ...
    ' Restore status
    Options.PrintBackground = fBackgroundPrinting
    ' Code to move documents goes here
    ...
    ...

  6. #6
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Files (XP SP3)

    This is working well, except if the file already exists which does happen sometimes when we make edits. Is there an option on the MoveFile to overwrite? I couldn't see one.

    Thanks!

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Move Files (XP SP3)

    Instead of MoveFile, you coulde use CopyFile, followed by DeleteFile. CopyFile will overwrite existing files by default, while MoveFile will cause an error.

  8. #8
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Files (XP SP3)

    Thank you again, that worked like a charm.

  9. #9
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Files (XP SP3)

    I have one more step I need to add to the following macro. If there are no files found to print, it gives an error. I need to add an "If" there ar eno files, go to the quit function. This is a new step for me, so help is appreciated.

    Here is the code:

    Dim sMyDir As String
    Dim sDocName As String
    ' The path to obtain the files.
    Documents.Add
    sMyDir = "C:InetpubwwwrootPrint"
    sDocName = Dir(sMyDir & "*.DOC")
    While sDocName <> ""
    ' Print the file.
    Application.PrintOut Background:=False, FileName:=sMyDir & sDocName
    ' Get next file name.
    sDocName = Dir()
    Wend
    Const strSource = "C:InetpubwwwrootPrint*.doc"
    Const strTarget = "C:InetpubwwwrootSmartSEARCHFileArchive"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile strSource, strTarget
    fso.DeleteFile strSource
    Set fso = Nothing
    Application.Quit
    End Sub

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Move Files (XP SP3)

    One possibility would be to create a Boolean variable that ends up reflecting whether any files were found using Dir, like this:
    <pre> Dim sMyDir As String
    Dim sDocName As String
    ' The path to obtain the files.
    Dim fso As Object
    Const strSource = "C:InetpubwwwrootPrint*.doc"
    Const strTarget = "C:InetpubwwwrootSmartSEARCHFileArchive"
    Dim fFileFound As Boolean
    Documents.Add
    sMyDir = "C:InetpubwwwrootPrint"
    sDocName = Dir(sMyDir & "*.DOC")
    If sDocName <> "" Then
    fFileFound = True
    End If
    While sDocName <> ""
    ' Print the file.
    Application.PrintOut Background:=False, FileName:=sMyDir & sDocName
    ' Get next file name.
    sDocName = Dir()
    Wend
    If fFileFound = True Then
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile strSource, strTarget
    fso.DeleteFile strSource
    Set fso = Nothing
    End If
    Application.Quit
    End Sub</pre>

    Another possibility would be to put the file-moving process within the While/Wend loop, moving one file at a time, like this:
    <pre> Dim sMyDir As String
    Dim sDocName As String
    ' The path to obtain the files.
    Dim sFileName As String
    Dim fso As Object
    Const strSource = "C:InetpubwwwrootPrint*.doc"
    Const strTarget = "C:InetpubwwwrootSmartSEARCHFileArchive"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Documents.Add
    sMyDir = "C:InetpubwwwrootPrint"
    sDocName = Dir(sMyDir & "*.DOC")
    While sDocName <> ""
    sFileName = sMyDir & sDocName
    ' Print the file.
    Application.PrintOut Background:=False, FileName:=sFileName
    fso.CopyFile sFileName, strTarget
    fso.DeleteFile sFileName
    ' Get next file name.
    sDocName = Dir()
    Wend
    Set fso = Nothing
    Application.Quit
    End Sub</pre>


  11. #11
    New Lounger
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Files (XP SP3)

    Thank you for your help. In the interest of simplicity and after a couple of hours of investigation and testing (and because I really didn't understand your solution since I am such a neophyte at programming) , I just put in the If sDocName = "" Then GoTo LastLine and the LastLine label. Let me know if anyone can see a problem with this, it seems to work fine:

    Dim sMyDir As String
    Dim sDocName As String
    ' The path to obtain the files.
    Documents.Add
    sMyDir = "C:InetpubwwwrootPrint"
    sDocName = Dir(sMyDir & "*.DOC")
    If sDocName = "" Then GoTo LastLine
    While sDocName <> ""
    ' Print the file.
    Application.PrintOut Background:=False, FileName:=sMyDir & sDocName
    ' Get next file name.
    sDocName = Dir()
    Wend
    Const strSource = "C:InetpubwwwrootPrint*.doc"
    Const strTarget = "C:InetpubwwwrootSmartSEARCHFileArchive"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile strSource, strTarget
    fso.DeleteFile strSource
    Set fso = Nothing
    LastLine:
    Application.Quit

  12. #12
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Move Files (XP SP3)

    That looks fine, and is substantively equivalent to my first suggested alternative. Certain programming purists might frown at the "GoTo" approach, but it definitely gets the job done. Most of my procedures have error-handlers, and so are set up with the conventional pair of labels (named ExitLabel: and ErrorHandler: in my case), and I often use "GoTo ExitLabel" as a way of aborting a procedure early.

Posting Permissions

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