Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2008

Tip: Looking for answers? Try searching our database.

how to put default suggested filename in SaveAs FileName:?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim - 24 Feb 2008 17:56 GMT
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
Greg Maxey - 24 Feb 2008 18:30 GMT
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

Signature

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

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

> 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
[quoted text clipped - 24 lines]
>    ActiveWindow.Close
> End Sub
Jim - 24 Feb 2008 18:49 GMT
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?

> Maybe something like this:
> Sub ScratchMacro()
[quoted text clipped - 48 lines]
> >    ActiveWindow.Close
> > End Sub
Greg Maxey - 24 Feb 2008 18:56 GMT
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.

Signature

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

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

> Hi Greg,
> it seems to work great until the save line, get this error:
[quoted text clipped - 63 lines]
>>>    ActiveWindow.Close
>>> End Sub
Jim - 24 Feb 2008 20:26 GMT
Thanks Greg, I got my original one to work, by taking out the file name
variable. Thanks for your help
Jim

> Jim,
>
[quoted text clipped - 69 lines]
> >>>    ActiveWindow.Close
> >>> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.