Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Richmond, Virginia, USA
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating a Macro to Save a Document

    Many of the documents in my office become corrupt and need to be repaired (The documents have come through many word processing applications and have all kinds of issues). The easiest way to fix these documents is to strip all the text out of the documents and paste the text into a new document. I've gotten the macro to do that much.

    Here's where my problem begins: I can't make the macro save that new document back to the same path and filename. I would prefer that the new document replace the old document for 2 reasons. I don't want anyone to be able to open the old, corrupt document and copy it elsewhere (causing more problems), and I don't want the users to get confused by 2 documents on our system with very similar names.

    I've tried inserting the document name and path in the original document then pasting it into the Save As dialog box of the new document, but the name and path isn't a static field. It changes with the document, so I can't use that method. Any suggestions?

  2. #2
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a Macro to Save a Document

    Quick late addition - I'd experiment on a practice doc first!

    As none of the usual experts have replied, try this homecooking... (I've tried this on Word2K and it works OK - you may need to have ONLY the document to copy open when running it.)

    Sub Test()
    Dim sFileName As String
    sFileName = ActiveDocument.Name
    Selection.WholeStory
    Selection.Copy
    Documents.Add DocumentType:=wdNewBlankDocument
    Selection.Paste
    Windows(2).Activate
    ActiveDocument.Saved = True
    ActiveDocument.Close
    ActiveDocument.SaveAs sFileName
    End Sub

    Are you closing the original document first?

  3. #3
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Richmond, Virginia, USA
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a Macro to Save a Document

    Yes, I have been closing the original document first. Thank you so much for your help. The code you gave me works almost perfectly: there was only one problem that I was able to get around easily. The main problem with the documents is that the Compatibility (Tools-Options-Compatibility) in most of our corrupt documents is Custom instead of Word 2000. The code you gave me didn't fix that problem, but it fixed the saving as the original file name/path wonderfully.

    Just in case anyone else needs to know, here's what worked for me:

    Sub DocFixer()

    Dim sFileName As String
    sFileName = ActiveDocument.Name
    Selection.EndKey Unit:=wdStory
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    Selection.Copy
    ActiveDocument.Close
    Documents.Add DocumentType:=wdNewBlankDocument
    Selection.Paste
    ActiveDocument.SaveAs sFileName
    Selection.HomeKey
    End Sub

  4. #4
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a Macro to Save a Document

    Glad to see you sorted it - and thanks for posting a working solution!

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a Macro to Save a Document

    Does it work any better if you use "sFileName = ActiveDocument.FullName" instead of "sFileName = ActiveDocument.Name"?

  6. #6
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Richmond, Virginia, USA
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating a Macro to Save a Document

    I didn't have any problems running the macro with either command, so I guess they both work the same. Thanks for the suggestion, though. I'll keep it in mind should I have any problems.

Posting Permissions

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