Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word - Filename argument of SaveAs Method of ActiveDocument

Status
Not open for further replies.

GeoffreyBernardo

Programmer
Oct 28, 2008
6
ZA
Hallo Guys,

In Word, I want to take the text in a textbox in the active document and use it as the filename for the active document by using the SaveAs method. Here is the code:

Sub Rename_ActiveDocument()
Dim NewFileName As String

NewFileName = ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange.Text
ActiveDocument.SaveAs FileName:=NewFileName
End Sub

Unfortunately, I get an error message: Word cannot complete the save due to a file permission error.

But if I just type the new filename in directly, it saves the file.

ActiveDocument.SaveAs FileName:= "Yabadabadoo"

What seems to be the problemo?

Thanks.
 
The code is in the document you are trying to save isn't it?

So you have this code running in the ActiveDocument, and all of a sudden the name of the ActiveDocument tries to change. The running code is using the file, so you can't go and change the file name because Word wouldn't no what to do with the code that is running.
 
Hi mintjulep,

If I type in a string literal for the "FileName"-argumanet of the SaveAs method, it does save, even though the document is open.
 
And that is correct.

mintjulep: "The running code is using the file, so you can't go and change the file name because Word wouldn't no what to do with the code that is running."

The running code is not using the file. You CAN execute a SaveAs instruction on the ActiveDocument.

As posted on VBAExpress, please verify the string NewFileName. There may be illegal characters for a filename.

faq219-2884

Gerry
My paintings and sculpture
 
Problemo Solvedo!

Guys, you were right all along. It is because of an illegal character. I stepped through the procedure and after this step:

NewFileName = ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange.Text

I hovered the mouse cursor over NewFileName and noticed a little square after the text. This square does not appear in the Immediate Window when you use Debug.Print NewFileName or even MsgBox NewFileName.

I got rid of the annoying little square with two string functions:

Dim TextBoxText As String
TextBoxText = ActiveDocument.Shapes("Text Box 3").TextFrame.TextRange.Text
NewFileName = Left(TextBoxText, Len(TextBoxText) - 1)

When I stepped through the code again and hovered the mouse cursor over the NewFileName variable, the little square was gone and the .SaveAs step executed like a dream.

I appreciate your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top