Solved

How to save a copy of Word document with VBA

Posted on 2009-07-13
3
603 Views
Last Modified: 2013-11-27
The steps for the VBA code are currently

1. Get the current word document as object
2. Save the document in a special folder
3. Give this path to a database so that the database can upload the file.
4. Ultimately delete the copy.

I am just using "myDoc.SaveAs CurrentProject.Path & "\Temp\" & str_Save_Name & ".rtf", wdFormatRTF"

At the end of this procedure the currently open document then has this path as its saved path, maybe the user had it saved somewhere else.

What should I do here?
Can I somehow take a copy of an open Word doc and save that without intefereing with the current document?
Or can I reliably just get the current path of the word document (without saving it at all) and use that path to upload?
0
Comment
Question by:darbid73
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
geneus earned 500 total points
ID: 24846845
Below is some Standard Code that I use to make a Copy of a Document in the Current Directory in order to modify the copied document and keep the original intact.

To get the Current Path of a Document:
MsgBox ActiveDocument.Path

To get the Full Document Name and Path:
MsgBox ActiveDocument.Path & Application.PathSeparator & _
    ActiveDocument.Name
'**************************************************************************************************
'*Step #1:  Preserve original document by making a copy of the document and working with the copy.*
'**************************************************************************************************
 
'Get the Current Document name
varOriginalDoc = ActiveDocument.Name
 
'Set the New Document Name
 
If Right(varOriginalDoc, 4) = ".doc" Then
varStringSelection = Len(varOriginalDoc) - 4
varNewDoc = Left(varOriginalDoc, varStringSelection) & "_Copy.doc"
ElseIf Right(varOriginalDoc, 4) = "docx" Then
varStringSelection = Len(varOriginalDoc) - 5
varNewDoc = Left(varOriginalDoc, varStringSelection) & "_Copy.docx"
End If
 
'Create and Save the new Document copy
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
 
ActiveDocument.SaveAs FileName:=varNewDoc, FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False
        
'Switch to the Original Document
Documents(varOriginalDoc).Activate
 
'Select the entire Document and Copy the Contents
Selection.HomeKey Unit:=wdStory
Selection.WholeStory
Selection.Copy
 
ActiveDocument.Close
 
'Switch to the new Document
Documents(varNewDoc).Activate

Open in new window

0
 
LVL 19

Author Comment

by:darbid73
ID: 24847133
Hey thanks for the great code - One thing I am concerned about is that the OriginalDoc could have any type of things on it, tables, pictures, special formating really anything.

In your experience is a Selection.Copy and I imagine (but you did not put it in the code) a Selection.Paste reliable?

I am not that experienced in Word - so please excuse the dumb what if questions - but your new document will be based on Normal.dot - what if they used another template for the original document, will a copy and paste work ok here?
0
 
LVL 19

Author Comment

by:darbid73
ID: 24847832
testing does not seem to show any problems with the copy/paste method.

Thanks
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vba sql wild card passing in code 3 22
Restoring a form to its default settings 3 27
Trying to open FORM in specific record !! 6 44
Word Perfect Question 11 17
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question