Mac版功能在Office for Mac 2016中运行不佳!有任何想法吗?

时间:2015-06-20 01:03:44

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

我的宏大量使用MacScript,但它似乎无法在最新的Office for Mac 2016预览版本中使用

2 个答案:

答案 0 :(得分:4)

用于支持Office for Mac 2011中的内联Apple脚本的MacScript命令已被弃用。由于沙箱的限制,MacScript命令无法再调用其他应用程序,例如Finder。因此,我们不鼓励使用此命令。 对于需要更改现有代码以使其不使用MacScript的情况,您可以使用AppleScriptTask命令(参见下文)。

新的AppleScriptTask命令执行AppleScript脚本。这类似于MacScript命令,除了它运行位于沙盒应用程序之外的AppleScript文件。 请按以下方式致电AppleScriptTask

 Dim myScriptResult as String

 myScriptResult = AppleScriptTask ("MyAppleScriptFile.applescript", "myapplescripthandler", "my parameter string") 

其中:

  • “MyAppleScript.applescript”文件必须位于〜/ Library / Application Scripts / [bundle id] /中,扩展名 applescript 不是必需的,也可以使用.scpt
  • “myapplescripthandler”是MyAppleScript.applescript文件中脚本处理程序的名称
  • “my parameter string”是“myapplescripthandler”脚本处理程序的单个输入参数。
  • 相应的AppleScript for Excel将位于〜/ Library / Application Scripts / com.microsoft.Excel /
  • 中名为“MyAppleScriptFile.applescript”的文件中

注意:Mac Word,Excel和PowerPoint的[bundle id]是:

  • com.microsoft.Word
  • com.microsoft.Excel
  • com.microsoft.Powerpoint

处理程序的示例如下:

on myapplescripthandler(paramString) 

    #do something with paramString 
    return "You told me " & paramString 

end myapplescripthandler

答案 1 :(得分:3)

我对Office for Mac 2016中对MacScript的支持感到非常失望。也许最令人沮丧的部分是创建文件夹结构和脚本文件的“解决方法” - 对于最终用户而言,它不易重现那些技术含量较低的客户。

为了解决这个问题,我创建了一个像安装程序一样运行的AppleScript,用于设置AppleScript文件夹以及需要与VBA应用程序一起传递以便AppleScriptTask工作的文件。我使用了Ron De Bruin网站(http://www.rondebruin.nl/mac/applescripttask.htm)中的“FileExists”和“FolderExists”示例。这两个函数在下面,用于确定文件或文件夹是否存在:

on ExistsFile(filePath)
    tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
end ExistsFile
on ExistsFolder(folderPath)
    tell application "System Events" to return (exists disk item folderPath) and class of disk item folderPath = folder
end ExistsFolder

您可以通过将以下脚本保存到名为“InstallFileFolderScript.scpt”的文件来运行该脚本。它做了两件事:

  1. 为Office MS for Mac MS Word脚本创建文件夹结构:“〜/ Library / Application Scripts / com.microsoft.Word”。
  2. 在当前工作目录中创建一个包含两个函数“FileExists”和“FolderExists”的脚本文件。
  3. 将脚本文件复制到com.microsoft.Word文件夹中。
  4. 文件复制后从工作目录中删除临时脚本。
  5. 随意修改它以根据应用程序的需要添加其他功能。脚本文件的每一行都是使用此脚本编写的。它也可以修改为与Excel和其他办公应用程序一起使用:

    property theFolders : {"~/Library/'Application Scripts'/com.microsoft.Word"}
    
    try
        tell application "Finder" to set targetFolder to (target of the front window) as alias
    on error -- no window
        set targetFolder to (choose folder)
    end try
    
    # build a parameter string from the folder list
    set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
    set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}
    
    do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders
    
    --Write the Script file if it does not exist
    if ExistsFile("~/Library/'Application Scripts'/com.microsoft.Word/FileFolderScript.scpt") is false then
        tell application "Finder"
            --GET THE WORKING DIRECTORY FOR FILE COPY OF SCRIPT
            get folder of (path to me) as Unicode text
            set workingDir to POSIX path of result
    
            --Write the new script in the current working directory
            set textFile to workingDir & "FileFolderScript.scpt"
    
            --Delete script if it exists
            set posixPath to POSIX path of textFile as string
            do shell script "rm -rf \"" & posixPath & "\""
    
            --Create File and Folder Script Interface for Microsoft Word VBA Applications
            set fd to open for access textFile with write permission
            write "on ExistsFile(filePath)" & linefeed to fd as «class utf8» starting at eof
            write "tell application \"System Events\" to return (exists disk item filePath) and class of disk item filePath = file" & linefeed to fd as «class utf8» starting at eof
            write "end ExistsFile" & linefeed to fd as «class utf8» starting at eof
            write "on ExistsFolder(folderPath)" & linefeed to fd as «class utf8» starting at eof
            write "tell application \"System Events\" to return (exists disk item folderPath) and class of disk item folderPath = folder" & linefeed to fd as «class utf8» starting at eof
            write "end ExistsFolder" & linefeed to fd as «class utf8» starting at eof
            close access fd
    
            --Copy the script file into the MACOS-Specific 'safe' folder
            set fromPath to quoted form of POSIX path of (workingDir) & "FileFolderScript.scpt"
            set toPath to quoted form of "~/Library/'Application Scripts'/com.microsoft.Word"
            do shell script "cp -R " & fromPath & space & "~/Library/'Application Scripts'/com.microsoft.Word" with administrator privileges
        end tell
    end if
    
    --Delete the temp script file from the working directory
    set posixPath to POSIX path of textFile as string
    do shell script "rm -rf \"" & posixPath & "\""
    
    --Provide confirmation
    display dialog "The File and Folder script necessary for Mac OS and Microsoft Office 2016 VBA integration has been successfully installed."
    
    --For use when checking if a file exists
    on ExistsFile(filePath)
        tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
    end ExistsFile
    

    最后,在VBA应用程序中,我使用它来调用AppleScript函数:

    Function Mac2016_FileOrFolderExists(FileOrFolder As Long, FilePathName As String)
        Dim RunMyScript As Boolean
    
        If (FileOrFolder = 1) Then
            RunMyScript = AppleScriptTask("FileFolderScript.scpt", "ExistsFile", FilePathName)
        Else
            RunMyScript = AppleScriptTask("FileFolderScript.scpt", "ExistsFolder", FilePathName)
        End If
    
        Mac2016_FileExists = RunMyScript
    End Function
    

    我还发现这篇Microsoft文章非常有用且易于理解:https://dev.office.com/blogs/VBA-improvements-in-Office-2016。它详细介绍了AppleScriptTask的用法,还介绍了在处理文件/文件夹时通常需要与AppleScriptTask一起实现的文件夹权限解决方法。

    祝你好运实施解决方案!希望这将有助于其他人提供“简单地为他们的客户工作”的宏,而不是让他们跳过解决方法步骤。欢迎提出问题,意见和建议!