Discussion Locked

Word document copy

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a Word document.
I have a command button on the Word document.
I want to click on the command button and copy one Word document with another Word document.
Example: DocumentA in directoryB replaces
DocumentA in directoryA.
Thank you.

3 replies

by grybyx

I see two scenarios in your question. First, you want to replace an existing file with the active document. Second, you want the active document to manage the replacement of one file with another.

Here's my solution to the first scenario:

Private Sub CommandButton1_Click()
Const DestFileName As String = "C:\My Documents\dest.doc"
Dim temp As String

temp = ActiveDocument.FullName
ActiveDocument.SaveAs FileName:=DestFileName
Documents.Open FileName:=temp
Documents(DestFileName).Close
End Sub

The above routine will save the active document in another location, keeping the active document active afterward.

Here's my solution to the second scenario:

Private Sub CommandButton1_Click()
Const SrcFileName As String = "C:\My Documents\src.doc"
Const DestFileName As String = "C:\My Documents\dest.doc"
Dim temp As String

temp = ActiveDocument.FullName
Documents.Open FileName:=SrcFileName
Documents(SrcFileName).SaveAs FileName:=DestFileName
Documents.Open FileName:=temp
Documents(DestFileName).Close
End Sub

The above routine will replace C:\My Documents\dest.doc with C:\My Documents\src.doc.

Happy Programming!

by hurdk

This answer is a very professional response. The code has definitely helped me. Thank you.

This discussion is closed to new replies.