Solved

Word, Visual Basic, using Save As...

Posted on 1998-08-01
7
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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