Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-17-2017, 12:12 PM
mike0215 mike0215 is offline Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Windows 10 Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Office 2016
Novice
Creating a macro from a non-saved Word doc that duplicates the current open document and saves it
 
Join Date: Nov 2017
Posts: 4
mike0215 is on a distinguished road
Post 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!
Reply With Quote
  #2  
Old 11-17-2017, 01:20 PM
macropod's Avatar
macropod macropod is offline Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Windows 7 64bit Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 11-17-2017, 01:28 PM
mike0215 mike0215 is offline Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Windows 10 Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Office 2016
Novice
Creating a macro from a non-saved Word doc that duplicates the current open document and saves it
 
Join Date: Nov 2017
Posts: 4
mike0215 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 11-17-2017, 01:40 PM
macropod's Avatar
macropod macropod is offline Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Windows 7 64bit Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

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
Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Word 2003 document saves as Wordpad docx Rick K Word 2 05-06-2015 08:25 AM
Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Cannot open saved Word document otherlunds Word 1 09-01-2014 05:41 AM
Creating a macro from a non-saved Word doc that duplicates the current open document and saves it Run-time error '1004': Document not saved. The document may be open... doctor_who12 Excel Programming 1 01-22-2014 04:47 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft