PDA

View Full Version : Solved: MS Word - Filename argument of SaveAs Method of ActiveDocument



GeoffreyB
10-27-2008, 02:13 PM
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.


EDIT: I also asked this question on these forums:
http://www.xtremevbtalk.com/showthread.php?t=301722
http://www.tek-tips.com/viewthread.cfm?qid=1509816&page=1

fumei
10-28-2008, 11:33 AM
If you test for NewFileName, do you get a valid string?

I am trying to get:

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

- but replacing "Text Box 2" with an index number, so...


ActiveDocument.Shapes(2).TextFrame.TextRange.Text

and I get an error.

GeoffreyB
10-28-2008, 12:17 PM
If you test for NewFileName, do you get a valid string?

Hi Fumei,

Thanks for your response. I think the .Text property definitely returns a string. I even used the CStr-function to convert the NewFileName variable, which already is defined as a string, to a string.

fumei
10-28-2008, 01:41 PM
What kind of textbox is this? Perhaps that may be a good place to start. As I stated, I get an error using that syntax.

Run-time error 5917
This object does not support attached text.

GeoffreyB
10-28-2008, 01:53 PM
What kind of textbox is this? Perhaps that may be a good place to start. As I stated, I get an error using that syntax.

Run-time error 5917
This object does not support attached text.

It's the one in Word you get on the ribbon under the Insert-tab and in the Text-section. In that section, there is a Text Box-dropdown list from which you can choose different types of text boxes. The one I'm using is the Simple Text Box.

I appreciate your effort.

geekgirlau
10-28-2008, 06:31 PM
One way to quickly test it is to print out the value:



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

Debug.Print NewFileName



If you step through your code and display the immediate window, you'll be able to see what value is being stored in that variable.

Keep in mind that you're going to have to look for illegal characters that won't be permitted as part of a filename. I'm guessing that if your syntax is correct and you are correctly picking up the text in the shape, there may be additional characters at the end of your text string that is causing the error.

fumei
10-29-2008, 08:46 AM
" I think the .Text property definitely returns a string. "

That is why I suggested - as does geekgirlau - to actually test it. What, exactly, is that string? Because she is correct, if the string contained illegal characters then it will not be a valid string for a filename.

GeoffreyB
10-29-2008, 01:40 PM
One way to quickly test it is to print out the value:



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


Debug.Print NewFileName





If you step through your code and display the immediate window, you'll be able to see what value is being stored in that variable.

Keep in mind that you're going to have to look for illegal characters that won't be permitted as part of a filename. I'm guessing that if your syntax is correct and you are correctly picking up the text in the shape, there may be additional characters at the end of your text string that is causing the error.

Hi GeekGirl,

I dd as you said and the variable does not contain any illegal charaters.

Thanks vey much for your input.

GeoffreyB
10-29-2008, 02:52 PM
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:clap: :thumb

fumei
10-30-2008, 10:29 AM
Do you understand why that square was there, and what it actually IS? I ask because the answer is the same if you, say, tried to use the Range.Text of the contents of a table cell. Using the Range.Text of a table cell as a filename in a SaveAs would also return the same file permission error.

BTW: the solution is the same as your solution to the textbox.

Oh, and could you please state in posts what version of Word is applicable? It helps. You are using 2007, as you mention "ribbon".

"It's the one in Word you get on the ribbon under the Insert-tab and in the Text-section. "

No, it is the one in Word 2007. This does not exist in previous versions. I do not use 2007 (and will not). I ask this because if someone posts questions on something that is only in 2007, I can just skip trying to do anything.

Thanks.

Glad you got it worked out.

fumei
10-30-2008, 10:30 AM
Oh, and could you please mark the thread as Solved? Thanks.

GeoffreyB
10-30-2008, 10:55 AM
Do you understand why that square was there, and what it actually IS?
Formatting symbol.


I ask because the answer is the same if you, say, tried to use the Range.Text of the contents of a table cell. Using the Range.Text of a table cell as a filename in a SaveAs would also return the same file permission error.

I know. I tried a table instead of a text box at one point.


Oh, and could you please state in posts what version of Word is applicable?

Will do.


Glad you got it worked out.

Thanks.

fumei
10-30-2008, 01:55 PM
"Formatting symbol."

Nope. Wrong. Well, I suppose it is sort of a formatting symbol, although I am not sure what you actually mean by that. It is a very very specific thing - and IMO, the most important concept/object in Word.

A table would be better, IMO. I hate graphical textboxes in VBA. Again, you fix the string exactly the same way, stripping off the last character.

GeoffreyB
11-01-2008, 12:13 PM
"Formatting symbol."

Nope. Wrong. Well, I suppose it is sort of a formatting symbol, although I am not sure what you actually mean by that. It is a very very specific thing - and IMO, the most important concept/object in Word.

What is it, then? Pray tell.:think:

lucas
11-01-2008, 12:57 PM
Coming in on the end of this but I think the answser to your question Geoffrey is that it is a space or a carriage return.

Re-read Anne's post #6

lucas
11-01-2008, 01:24 PM
You probably know how to do this but if you click the button to show your formatting it will reveal whether there are spaces in the textbox. They are dots.

GeoffreyB
11-01-2008, 02:05 PM
Hi Lucas,

It was actually a formatting mark. There were no spaces.