简体   繁体   中英

Excel VBA alteration for MAC

I have the following Script which works perfectly in a windows environment. Upon saving the excel file it will open the assigned word document, update the links and then save it as a PDF in the same folder named off the C6 cell contents. Unfortunately, I need it to work on a MAC as well. I changed the path from "c:\\Prop" to "/Prop/" (I created a folder on the root of the mac HD called Prop) and it works up to the point where is opens word, but then I get an unsupported function message... anyone know what the different function is for MAC office 16???

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim objWord, objWordDoc, objField As Object
    Dim boolSuccess, boolUpdated As Boolean
    Dim strFolderPath, strWordFileName, strPdfFileName, strOutput As String

    strFolderPath = "c:\Prop\"
    strWordFileName = "Prop.docm"
    strPdfFileName = ActiveWorkbook.Sheets(1).Cells(6, 3).Value & ".pdf"
    strOutput = "There are problems with updating the next fields:" & vbCrLf
    boolSuccess = True

    On Error GoTo Error
        Err.Clear

        Set objWord = CreateObject("Word.Application")
        Set objWordDoc = objWord.Documents.Open(strFolderPath & strWordFileName)

        If Not objWordDoc Is Nothing Then
            For Each objField In objWordDoc.Fields
                boolUpdated = objField.Update
               If Not boolUpdated Then
                    boolSuccess = False
                    strOutput = strOutput & "Field" & CStr(objField.Index) & vbCrLf
                End If
            Next
            objWordDoc.Save
            objWordDoc.ExportAsFixedFormat strFolderPath & strPdfFileName, 17
            objWordDoc.Close
            If boolSuccess Then
                MsgBox strWordFileName & " was updated successfully and " & strPdfFileName & " was saved in " & strFolderPath
            Else
                MsgBox strOutput
                MsgBox strWordFileName & " was updated with problems and " & strPdfFileName & " was saved in " & strFolderPath
            End If
        End If
Error:
    If Err.Description <> "" Then
        MsgBox "Error: " & Err.Description, , "Error"
    End If

    objWord.Quit

    Set objWordDoc = Nothing
    Set objWord = Nothing
End Sub

The CreateObject function is part of the Windows scripting library, it doesn't exist on Mac OSX.

You should have more luck by referencing the Microsoft Word Object (vxx) Library (tools -> References) and not creating the object at runtime:

dim objWord as New Word.Application
dim objWordDoc as Word.Document
Set objWordDoc = objWord.Documents.Open(strFolderPath & strWordFileName)

This way you are not relying on the windows libraries.

CreateObject works fine with me on mac with office 2011... I think the problem is the path string. On mac paths are different. I believe it should be something like this:

"OSX:Local:Prop:Prop.docm" or if you want it separated:

strFolderPath = "OSX:Local:Prop:"
strWordFileName = "Prop.docm"

Tell me if it worked :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM