|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Creating a macro from a non-saved Word doc that duplicates the current open document and saves it
Hi, I'm very, very new to VBA and am trying to create a macro that will duplicate all of the contents (including comments, markup, etc.) of an open document (that will most likely not have been saved on the computer as it will be opened directly from Sharepoint) and save it to the local computer (i.e. in Documents), and then close it, leaving the original document open. This is one macro that I want to occur before a set of other macros that clean up the document--I'm essentially trying to save the original one for backup purposes, so the additional clean-up macros should only be applied to the original opened document, not the newly duplicated one. I've searched around and have tried out a bunch of different codes with no luck. The current code that's gotten my the closest prompts the 'Save' dialogue, and I'm able to save it but then when I try to open it I get an error telling me it cannot be found. This is the current code I'm using: Sub Macro1() ' Macro1 Macro Const lCancelled_c As Long = 0 Dim sSaveAsPath As String sSaveAsPath = GetSaveAsPath If VBA.LenB(sSaveAsPath) = lCancelled_c Then Exit Sub 'Save changes to original document ActiveDocument.Save 'the next line copies the active document Application.Documents.Add ActiveDocument.FullName 'the next line saves the copy to your location and name ActiveDocument.saveAs "C:\Users\name\Documents" 'next line closes the copy leaving you with the original document ActiveDocument.Close End Sub I've also tried this one (via the Record function) and there seems to be an error in the bold line.This is copied from a larger macro so the sub/end macros aren't included: 'CopyDoc Macro Selection.WholeStory Selection.Copy Documents.Add DocumentType:=wdNewBlankDocument Selection.PasteAndFormat (wdUseDestinationStylesRecovery) ActiveDocument.Save ActiveDocument.SaveAs2 FileName:="StoryPackCopy123.docx", FileFormat:= _ wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _ :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _ :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _ SaveAsAOCELetter:=False, CompatibilityMode:=15 Windows("CRM Story Pack Phase 2").Activate Any and all help would be appreciated! |
#2
|
||||
|
||||
So why not simply have the macro save the current document to wherever you want it, close it, then re-open the original one?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Well, I need the macro to create a duplicate of the current document and save it, as I'm applying additional macros to the original after creating a copy to preserve the original content.
Does that make sense? I tried recording a macro where I opened the file from Sharepoint in Word, copied and saved everything into a new document to ensure everything was preserved, and then ran a bunch of other macros on the original to create a finalized version. But that code didn't seem to work. |
#4
|
||||
|
||||
Either way, you don't need all this copy/paste stuff; simply save the document you opened with a new name. Whether you do that before modifying it, or after (or even before and after), is immaterial. For example:
Code:
Sub Demo() Dim strDoc As String With ActiveDocument 'get the source document's name Set strDoc = .FullName 'modify the document 'save the document via the SaveAs dialogue .Application.Dialogs(wdDialogFileSaveAs).Show 'modify the document 'close & save any changes to the document .Close True End With 'reopen the source document Documents.Open (strDoc) End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
copy, save document, word vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
My Word 10 won't open it's own saved files because it says they are corrupted | Dave Young | Word | 5 | 11-05-2016 09:13 PM |
Some images not saved when creating PDF from Word with Acrobat | WaltR | Word | 5 | 09-26-2015 02:58 PM |
Word 2003 document saves as Wordpad docx | Rick K | Word | 2 | 05-06-2015 08:25 AM |
Cannot open saved Word document | otherlunds | Word | 1 | 09-01-2014 05:41 AM |
Run-time error '1004': Document not saved. The document may be open... | doctor_who12 | Excel Programming | 1 | 01-22-2014 04:47 PM |