Forum Discussion

Josh_Waldner's avatar
Josh_Waldner
Brass Contributor
Apr 16, 2022

Save as

Hi. I need  little help with a VBA code to save a document as a .doc(x) with the first line of the document as the title. The document is originally a .txt file with a number as a name. ei: 563.txt.

Thanks in advance, Josh Waldner

  • Use:

    Dim fname As String
    Selection.HomeKey wdStory
    With ActiveDocument
    fname = LTrim(Mid(.Bookmarks("\line").Range.Text, 3))
    fname = Replace(fname, ",", "")
    fname = Replace(fname, ".", "")
    fname = Replace(fname, "?", "")
    fname = Replace(fname, "!", "")
    .SaveAs2 fname & ".docx"
    End With
  • Josh_Waldner Use:

     

    Selection.HomeKey wdStory
    With ActiveDocument
           .SaveAs2 .Bookmarks("\line").Range.Text & ".docx"
    End With

     

    You will need to make sure that the first line of the document does not contain and characters that cannot be used in a filename.

    • Josh_Waldner's avatar
      Josh_Waldner
      Brass Contributor

      Doug_Robbins_Word_MVP 

      thanks. you did answer my question, but how about taking the first line of the first paragraph and copy it as an independent line at the beginning, and strip off all nonalphabetical characters. please see attached image.

      • Does the first line always start with a numeral, followed by a period as in the

        1. Den Hirten, die bei Nacht,

        and you want to reduce that to

        Den Hirten die bei Nacht

Resources