none
Need some help about Word Macro RRS feed

  • Question

  • Hi all~ I met a problem and I need your help.

    I've written a macro to change my Word active document's name without closing it. It works very well in PC, but not in mac. when running in mac, it doesn't work. I'm not familiar with mac. Please help me. 

    Here's the macro:

    Sub Editname()
    '

    Dim strDocName As String, strDocPath As String
    Dim strNewDocName As String
    Dim KillFile As String
    Dim strDate As String


    ' Get the current doc name.
    strDocName = ActiveDocument.Name
    strDocFullName = ActiveDocument.FullName
    strDocPath = ActiveDocument.Path

    If strDocPath = "" Then
    MsgBox ("This document hasn't been saved. You can't rename it.")
    Exit Sub
    End If

    ' Pop up an input box for new name.
    strNewDocName = InputBox("Enter a new name for this document:", "Rename document", strDocName)

    ' Save the doc with newly entered name.
    ActiveDocument.SaveAs2 FileName:=strDocPath & "\" & strNewDocName (The VBA showed there's something wrong)

    ' Delete the doc with original name.
    KillFile = strDocFullName
    Kill KillFile
    End Sub

    Thursday, March 14, 2019 4:02 AM

All replies

  • The starting point is to change

    ActiveDocument.SaveAs2 FileName:=strDocPath & "\" & strNewDocName

    to 

    ActiveDocument.SaveAs2 FileName:=strDocPath & Application.PathSeparator & strNewDocName
    By default, Windows file systems use "\" as the Path Separator character, whereas Mac OS generally now uses "/".


    (That isn't the whole story but it should  be enough in a simple scenario).


    Peter Jamieson

    Friday, March 15, 2019 10:34 AM
  • Thanks a lot for your help. By the way, after I change it, 

    KillFile = strDocFullName
    Kill KillFile((The VBA showed there's something wrong))
    End Sub

    Could you please help me? Thanks in advance!

    Saturday, March 16, 2019 4:14 AM
  • The code works OK here in a simple scenario with Word 365 version 16.24 and Mac OS 10.14.3 so I wonder if you can provide

     a. more details on exactly what error VBA showed

     b. your Mac OS(X) version

     c. your Word version

     d. the original and new document full names (i.e. path name+document name).

    (NB, my 16.24 version is the "Office Insider Fast" version)


    Peter Jamieson

    Saturday, March 16, 2019 8:30 PM