Results 1 to 17 of 17

Thread: Solved: MS Word - Filename argument of SaveAs Method of ActiveDocument

  1. #1

    Solved: MS Word - Filename argument of SaveAs Method of ActiveDocument

    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:

    [vba]
    Sub Rename_ActiveDocument()
    Dim NewFileName As String
    NewFileName = ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange.Text
    ActiveDocument.SaveAs FileName:=NewFileName
    End Sub
    [/vba]

    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.

    [vba]ActiveDocument.SaveAs FileName:= "Yabadabadoo"[/vba]

    What seems to be the problemo?

    Thanks.

    Last edited by GeoffreyB; 10-28-2008 at 02:02 PM.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    If you test for NewFileName, do you get a valid string?

    I am trying to get:[vba]

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

    [/vba] - but replacing "Text Box 2" with an index number, so...
    [vba]

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

    [/vba]and I get an error.

  3. #3
    Quote Originally Posted by fumei
    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.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    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.

  5. #5
    Quote Originally Posted by fumei
    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.
    Last edited by GeoffreyB; 10-28-2008 at 02:09 PM.

  6. #6
    VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    One way to quickly test it is to print out the value:

    [vba]
    NewFileName = ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange.Text
    Debug.Print NewFileName
    [/vba]

    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.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    " 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.

  8. #8
    Quote Originally Posted by geekgirlau
    One way to quickly test it is to print out the value:

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

    Debug.Print NewFileName



    [/vba]

    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.

  9. #9
    Problemo Solvedo!

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

    [vba]
    NewFileName = ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange.Text
    [/vba]

    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:

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

    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

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    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.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    Oh, and could you please mark the thread as Solved? Thanks.

  12. #12
    Quote Originally Posted by fumei
    Do you understand why that square was there, and what it actually IS?
    Formatting symbol.

    Quote Originally Posted by fumei
    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.

    Quote Originally Posted by fumei
    Oh, and could you please state in posts what version of Word is applicable?
    Will do.

    Quote Originally Posted by fumei
    Glad you got it worked out.
    Thanks.

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    "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.

  14. #14
    Quote Originally Posted by fumei
    "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.

  15. #15
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    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
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  16. #16
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  17. #17
    Hi Lucas,

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •