Solved

Word, Visual Basic, using Save As...

Posted on 1998-08-01
7
359 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 2016 Install Options 11 56
Install MS Office 2016 and uninstall MS Office 2010 3 53
how to add and add vat in excel 9 46
Excel 2010 Sparklines - horizontal bar 2 129
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now