SOLVED

Save as

Brass Contributor

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

5 Replies

@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.

@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
yes, it always start with "1. ", but it might have commas, periods, exclamation points and other punctuation, which i want to get rid of in the title line.
best response confirmed by Josh_Waldner (Brass Contributor)
Solution
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
1 best response

Accepted Solutions
best response confirmed by Josh_Waldner (Brass Contributor)
Solution
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

View solution in original post