Solved

Word, Visual Basic, using Save As...

Posted on 1998-08-01
7
368 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

SendBlaster Pro 4 - Bulk Email Sending Software

SendBlaster 4 Pro - Best Bulk Emailing Sending Software
Automatic Subscribe / Unsubscribe Processing
Great for Newsletters & Mass Mailings
Optional HTML & Text Composition
Integration with Google Features
Built in Spam Score Checking
Free Professional Templates - Feature Packed!

Question has a verified solution.

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

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 …
My experience with Windows 10 over a one year period and suggestions for smooth operation
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

752 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