Save document from selected text

D

ddt357

I need to create a macro that will let me save a selected portion of text in
the document as the file name. When I record the macro it inserts the
document name, but I want to use a variable instead. The name will change
with each document. Does anyone know how to do this?
 
D

Doug Robbins - Word MVP

How would the macro identify the portion of text to be used as the filename?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

OK, how would the macro know what to copy? Think a bit more about my
original question.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BenJohnson

I am having the same problem. Here is the macro as I have it. It works in
step through mode but not when I run it. When I run the macro it goes to
NotText:

Sub deleteme()

Selection.HomeKey Unit:=wdStory
Selection.MoveRight Unit:=wdCharacter, count:=4, Extend:=wdExtend
Selection.Copy
Selection.MoveDown Unit:=wdLine, count:=1

ChangeFileOpenDirectory "C:\Emdeon997s\"

Dim MyData As DataObject
Set MyData = New DataObject
Dim sClipText As String
On Error GoTo NotText

' Get data from the clipboard.
MyData.GetFromClipboard

' Assign clipboard contents to string variable.
sClipText = MyData.GetText(1)
'MsgBox sClipText
NotText:
If Err <> 0 Then
MsgBox "Data on clipboard is not text."
End If
ActiveDocument.SaveAs FileName:=sClipText & ".doc"
End Sub
 
D

Doug Robbins - Word MVP

Use:

Dim myrange As Range
Set myrange = ActiveDocument.Range
myrange.End = myrange.Start + 4
ActiveDocument.SaveAs "C:\Emdeon997s\" & myrange.Text


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BenJohnson

Thanks Doug. That get's me really close. One more question: What if I
wanted to use a piece of text for the file name that isn't right at the
beginning of the document? For example, the data I am working with has a
control number somewhere in the text. I can find that control number with
the following script but then how do I use what is selected as the save as
file name?

Selection.Find.ClearFormatting
With Selection.Find
.text = "*P*>~"
.Replacement.text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Selection.MoveLeft Unit:=wdCharacter, count:=12
Selection.MoveRight Unit:=wdCharacter, count:=9, Extend:=wdExtend
 
G

Graham Mayor

Maybe something like

Dim rNum As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="P>~", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rNum = Selection.Range
rNum.MoveStart wdCharacter, -11
rNum.MoveEnd wdCharacter, -5
Loop
End With
End With
On Error GoTo NotFound
ActiveDocument.SaveAs "C:\Emdeon997s\" & rNum.Text
Exit Sub
NotFound:
MsgBox "Number not found. Document not saved!", vbCritical, "Error"


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top