MS Word - Filename argument of SaveAs Method of ActiveDocument


  • Hallo Guys,


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


    Code Snippet

    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.


    Code Snippet

    ActiveDocument.SaveAs FileName:= "Yabadabadoo" 



    What seems to be the problemo?



    Monday, October 27, 2008 8:42 PM


  • Problemo Solvedo!

    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.
    Wednesday, October 29, 2008 10:13 PM