how to put default suggested filename in SaveAs FileName:?

J

Jim

I have the following simple macro to take a selection from a long document
and create a new document from it, the problem is that every time I use it,
the new file is saved to the same file name and over writes the old one. What
I would like is to have the file saved to the default suggested file name
that word would normally generate from the first line of the selection.
Anyone know what the variable for FileName should be to make this so?

Thanks Jim

Sub NewDocFromSelection()
'
' NewDocFromSelection Macro
' Create new document from selection
'
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
ChangeFileOpenDirectory "D:\My websites\"
ActiveDocument.SaveAs FileName:="What causes this Syndrome.docx", _
FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False
ActiveWindow.Close
End Sub
 
G

Greg Maxey

Maybe something like this:
Sub ScratchMacro()
Dim oRng As Range
Dim pStr As String
Dim oDoc As Document
Set oRng = Selection.Range
oRng.Start = Selection.Range.Start
If Selection.Words.Count > 5 Then
oRng.End = Selection.Words(5).End
Else
MsgBox "Please select the range of text to save as new document."
Exit Sub
End If
If Not oRng.Characters.Last Like "[A-Za-z]" Then
oRng.MoveEnd wdCharacter, -1
End If
pStr = "D:\My websites\" & oRng.Text & ".docx"
Selection.Copy
Set oDoc = Documents.Add
Selection.PasteAndFormat (wdPasteDefault)
oDoc.SaveAs (pStr)
oDoc.Close
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~~~~~~~~~~~~
 
J

Jim

Hi Greg,
it seems to work great until the save line, get this error:
Run-time error '5487'
Word cannot complete the save due to file permissionerror

any ideas?


Greg Maxey said:
Maybe something like this:
Sub ScratchMacro()
Dim oRng As Range
Dim pStr As String
Dim oDoc As Document
Set oRng = Selection.Range
oRng.Start = Selection.Range.Start
If Selection.Words.Count > 5 Then
oRng.End = Selection.Words(5).End
Else
MsgBox "Please select the range of text to save as new document."
Exit Sub
End If
If Not oRng.Characters.Last Like "[A-Za-z]" Then
oRng.MoveEnd wdCharacter, -1
End If
pStr = "D:\My websites\" & oRng.Text & ".docx"
Selection.Copy
Set oDoc = Documents.Add
Selection.PasteAndFormat (wdPasteDefault)
oDoc.SaveAs (pStr)
oDoc.Close
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~...e ActiveWindow.Close End Sub[/QUOTE] [/QUOTE]
 
G

Greg Maxey

Jim,

It is working here but I am just using some jibberish text. pStr is just the
file path and string. You might try including some of the other SaveAs
attributes like in your original code.



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~...eWindow.Close End Sub[/QUOTE][/QUOTE][/QUOTE]
 
J

Jim

Thanks Greg, I got my original one to work, by taking out the file name
variable. Thanks for your help
Jim
Greg Maxey said:
Jim,

It is working here but I am just using some jibberish text. pStr is just the
file path and string. You might try including some of the other SaveAs
attributes like in your original code.



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~...Window.Close End Sub[/QUOTE][/QUOTE] [/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top