Solved

Word, Visual Basic, using Save As...

Posted on 1998-08-01
7
361 Views
Last Modified: 2012-06-22
I need to save a document with a name that will be copied out of the document.
The code is below.
selection.copy will get the name for me.
ActiveDocument.SaveAs will save the file with this copied text as filename, how to get
the value or text out of selection.copy and be accepted here as filename? I wrote
filenamehere were it should go.
Remember, each time, the name will be different. It will be copied out of the text.

code:
_______

Selection.Copy
        ActiveDocument.SaveAs FileName:=filenamehere, _
        FileFormat:=wdFormatText, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False
0
Comment
Question by:Javier070798
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:shalbe
ID: 1612976
Javier,

You need to set the selection text equal to a string variable and the use the variable where filenamehere is. Something like below. If you need help getting it to work, let me know.

Try this.

Dim thisFileName as String

thisFileName = Selection.Text

ActiveDocument.SaveAs FileName:=thisFileName, _
         FileFormat:=wdFormatText, LockComments:=False, Password:="", _
         AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
         EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
         :=False, SaveAsAOCELetter:=False

Steve
0
 

Author Comment

by:Javier070798
ID: 1612977
I have tried that.
here's the code.
I get an error..."Not a valid filename", please help!

Dim thisFileName As String
    Selection.HomeKey Unit:=wdStory
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Copy
    thisFileName = Selection.Text
   
    ActiveDocument.SaveAs FileName:=thisFileName, _
        FileFormat:=wdFormatText, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False
0
 
LVL 3

Expert Comment

by:shalbe
ID: 1612978
Javier,

I am on my way out and don't have time to debug it right now. Reject my answer (Reopen the question) so other experts may answer. If you still don't have an answer when I get back this evening I'll work on it.

Steve
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Accepted Solution

by:
shalbe earned 80 total points
ID: 1612979
Javier,

The problem was that the Selection.EndKey command was including the carriage return in the selection. I added a line that backspaces to eliminate the return from the selection.

The following code now works for me. Let me know if you have any problems.


Sub NameThisDoc()
     Dim thisFileName As String

         Selection.HomeKey Unit:=wdStory
         Selection.EndKey Unit:=wdLine, Extend:=wdExtend
         Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

         thisFileName = Selection.Text
         
         ActiveDocument.SaveAs FileName:=thisFileName, _
         FileFormat:=wdFormatText, LockComments:=False, Password:="", _
         AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
         EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
         :=False, SaveAsAOCELetter:=False


End Sub

Steve
0
 

Author Comment

by:Javier070798
ID: 1612980
Great job!
Works...
I new some Pascal, but that wont help when you don't know the new stuff.
I got another one....
will post inmmediately.
0
 

Expert Comment

by:rb79
ID: 12571312
Hi guys,

I have a related question and probably I should start a new thread.
How do you set the property "Do not embed common system fonts" to true using VBA? This option can be found under EmbedTrueTypeFonts property in Word (Tools>>Options>>Save tab)

Raj
0
 

Expert Comment

by:rb79
ID: 12571341
Ok.
I have posted the same question here: http://www.experts-exchange.com/Applications/MS_Office/Q_21205380.html
Thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question