Make copy of ThisDocument, save and close copy. RRS feed

  • Question

  • I'm trying to do something like the ExportAsFixedFormat method, where it creates a copy of ThisDocument and closes it, leaving ThisDocument open. However, I want to save it as a DOCX file. 
    Sunday, March 1, 2020 12:47 AM

All replies

  • Try something based on:

    Sub Demo()
    Dim RngSrc As Range, DocTgt As Document
    Set RngSrc = ActiveDocument.Range
    Set DocTgt = Documents.Add(Visible:=False)
    With DocTgt
      .Range.FormattedText = RngSrc.FormattedText
      .SaveAs FileName:=Split(.FullName, ".doc")(0) & " - " & Format(Now(), "YYYYMMDDhhmmss") & ".docx", _
        FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      .Close False
    End With
    End Sub

    Paul Edstein
    [MS MVP - Word]

    Sunday, March 1, 2020 6:15 AM
  • Unless you are also trying to avoid saving the existing document, to ensure you get everything in the document, how about something like:

    Option Explicit
    Sub Demo()
    Dim fullname As String
    Dim dotpos As Integer
    With ActiveDocument
      fullname = .fullname
      dotpos = InStrRev(fullname,".")
      .SaveAs2 Left(fullname, dotpos - 1) & Format(Now(), "YYYYMMDDHHmmss") & Mid(fullname, dotpos), .SaveFormat
      ' use SaveAs2 again to re-establish the original name
      .SaveAs2 fullname, .SaveFormat
    End With
    End Sub

    Peter Jamieson

    Sunday, March 1, 2020 2:06 PM