Help with a Winword Macro RRS feed

  • Question

  • Hi,

    I am a novice but I have made this macro with Googling. I create a document in a folder and give it a name.  I then have to upload this document into a folder where I require the document name.  I want the document name in the end without doc extension for me to copy-paste in a folder.

    Dim NewPath As String
        NewPath = "C:\****\"
        ActiveDocument.SaveAs FileName:=NewPath & ActiveDocument.Name

        Dim myRange As Range

        Set myRange = ActiveDocument.Range
        myRange.Collapse Direction:=wdCollapseEnd
        myRange.InsertAfter ActiveDocument.Name
        MsgBox "This document's name is " & myRange.Text

    Friday, December 21, 2018 5:53 PM

All replies

  • What I think you require is

    Sub SaveAndCopyName()
    Const NewPath As String = "C:\Path\"
    Dim myRange As Range
    Dim strName As String

        If ActiveDocument.path = "" Then
            MsgBox "Save the document before using this macro!"
            Exit Sub
        End If
        strName = ActiveDocument.Name
        ActiveDocument.SaveAs FileName:=NewPath & strName

        Set myRange = ActiveDocument.Range
        myRange.Collapse DIRECTION:=wdCollapseEnd
        strName = Left(strName, InStrRev(strName, Chr(46)) - 1)
        myRange.Text = strName
        MsgBox "This document's name is '" & myRange.Text & "'."
        ActiveDocument.Close SaveChanges:=wdSaveChanges
        Set myRange = Nothing
    End Sub

    Graham Mayor - Word MVP

    Saturday, December 22, 2018 4:47 AM