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 / October 2003

Tip: Looking for answers? Try searching our database.

Macros

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doug - 31 Oct 2003 14:34 GMT
If this is the wrong forum, please help on re-direct.

I need to create a macro to save a file to a specific
location, but with a file name based on the first 2 words
in the document. (different name each time) Any takers?
dsc - 31 Oct 2003 16:52 GMT
One option would be to select the first two words, then run a macro that uses the Selection property to get those selected words in a string variable, add any other chars you want to the string, then SaveAs.

Sub SaveAsFirstTwoWords()
Dim NameString As String
Dim PathString As String
   If Selection.Type = wdSelectionNormal Then
       PathString = "C:\wherever\wherever\wherever"
       NameString = PathString & Selection & "WhateverElse"
       ActiveDocument.SaveAs FileName:=NameString
   Else
       MsgBox "Nothing is Selected"
       End
   End If
End Sub

You could put that macro in a drop-down menu, a toolbar button, or a hotkey. Select the first two words of the file, then run it.

> If this is the wrong forum, please help on re-direct.
>
> I need to create a macro to save a file to a specific
> location, but with a file name based on the first 2 words
> in the document. (different name each time) Any takers?
dsc - 31 Oct 2003 17:11 GMT
Oh, and to avoid overwriting existing files, you might want to add something
like this:

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

If fs.FileExists(NameString) Then
           ActiveDocument.SaveAs FileName:=NameString
       Else
           MsgBox "Honk off, Bozo. " & NameString & " already exists."
           End
       End If

> If this is the wrong forum, please help on re-direct.
>
> I need to create a macro to save a file to a specific
> location, but with a file name based on the first 2 words
> in the document. (different name each time) Any takers?
dsc - 31 Oct 2003 17:27 GMT
Well, that'll larn me to post when I'm groggy with fattygue.

I gooned up the check for the file. Try it this way:

Sub SaveAsFirstTwoWords()
Dim NameString As String
Dim PathString As String
Dim fs

Set fs = CreateObject("Scripting.FileSystemObject")

   If Selection.Type = wdSelectionNormal Then
       PathString = "C:\" 'wherever\wherever\wherever"
       NameString = PathString & Selection & ".doc"
       If Not fs.FileExists(NameString) Then
           ActiveDocument.SaveAs FileName:=NameString
       Else
           MsgBox "Honk off, Bozo. " & NameString & " already exists."
           End
       End If
   Else
       MsgBox "Nothing is Selected"
       End
   End If
End Sub

> If this is the wrong forum, please help on re-direct.
>
> I need to create a macro to save a file to a specific
> location, but with a file name based on the first 2 words
> in the document. (different name each time) Any takers?
 
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.