Creating macro/code to use selected text as file name

A

Anthony Dowd

Hi

I'm trying to create a macro that selects a given line of text in a word
document, then saves the document with this text as the file name.

The code below produces the following error:
Run Time Error 5487: Word can not complete the save due to a file permission
error.

******************************************************
Sub Macro1()

Dim stDocName As String

Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=11
Selection.MoveRight Unit:=wdCharacter, Count:=12
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
stDocName = Selection

ActiveDocument.SaveAs FileName:= _
"Operation" & stDocName & ".doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
****************************************************

I have tried a variation of the file name line above ("Operation" &
Selection.Paste & ".doc", FileFormat:= _) to paste the selected text as the
file name but got the following error:
"Compile Error: Expected Function or variable"
but I'm probably closer to the mark with the line of code as it is in the
above block of code.

Any suggestions?
Anthony
 
D

Doug Robbins

Hi Anthony,

The second method won't work that's for sure. The first one should, but all
you need really include after the
ActiveDocument.SaveAs

is

FileName:= "Operation" & stDocName

Put in a MsgBox stDocName to make sure that the string contains what you
think it does and not some characters that may be illegal in filenames.

Also, see if you can save the document manually with that filename.
--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Anthony Dowd

The code looks ok, whether it works as you intend I've no idea. Check whatever is in the
module before or after your Sub. I sounds like there's something there that VBA doesn't
like.

HTH + Cheers - Peter
 
A

Anthony Dowd

Thanks Doug & Peter

I sorted the problem. I was using an illegal character but have changed the
offending text.

Thanks again.

Anthony
 
A

Anthony Dowd

Hi again

Just a follow up question...

How can I modify my code to accommodate varying numbers of characters in a
string of text to be used as the file name. The current macro code can only
cope with strings of text 33+ characters long. The macro runs when the
number of characters is greater than 33 but only uses 33 characters in the
file name. The macro doesn't run when the number of characters is less than
33. I could use If...Then statements to accommodate all the possible numbers
of characters, but this seems inefficient. Is there some other command
besides "Count:=". I have tried using the EndKey command but the macro won't
run using this because it extends one space beyond the actual characters in
the string.
Sub SaveOpReport()
Dim stDocName As String
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=11
Selection.MoveRight Unit:=wdCharacter, Count:=12
Selection.MoveRight Unit:=wdCharacter, Count:=33, Extend:=wdExtend
Selection.Copy
stDocName = Selection

ActiveDocument.SaveAs FileName:=stDocName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

End SubThanks in advance
Anthony
 
A

Anthony Dowd

Solved problem of varying number of characters in string using the following
code.

With Selection
.MoveEndUntil Cset:=".", Count:=wdForward
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With

Works a treat.
Anthony
 

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