Solved

Word, Visual Basic, using Save As...

Posted on 1998-08-01
7
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Accepted Solution

by:
shalbe earned 80 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok.
I have posted the same question here: http://www.experts-exchange.com/Applications/MS_Office/Q_21205380.html
Thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

771 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

11 Experts available now in Live!

Get 1:1 Help Now