MAC

时间:2015-12-15 14:29:53

标签: excel vba excel-vba excel-vba-mac

我有以下脚本在Windows环境中完美运行。保存excel文件后,它将打开指定的word文档,更新链接,然后将其另存为PDF文件,该文件夹名为C6单元格内容。不幸的是,我也需要它来处理MAC。我将路径从“c:\ Prop”更改为“/ Prop /”(我在mac HD的根目录上创建了一个名为Prop的文件夹)并且它可以工作到打开单词的位置,但后来我得到了不支持功能消息......任何人都知道MAC办公室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

2 个答案:

答案 0 :(得分:0)

CreateObject函数是Windows脚本库的一部分,它在Mac OSX上不存在。

您应该通过引用Microsoft Word Object (v.x.x) Library(工具 - &gt;参考)而不是在运行时创建对象来获得更多运气:

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

这样你就不依赖于windows库了。

答案 1 :(得分:0)

CreateObject在我的Mac上使用Office 2011正常工作......我认为问题是路径字符串。在mac路径上是不同的。我相信它应该是这样的:

&#34; OSX:本地:属性:Prop.docm&#34;或者如果你想要它分开:

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

告诉我它是否有效:)