先给各位看效果提示词效果最终效果内容有删减不仅实现了 ChatGPT for Word而且支持了基本的 Markdown 格式我都忍不住给自己点个赞。markdownText### 重要信息vbCrLf_1. 第一项vbCrLf_2. 第二项vbCrLf_- 无序项 1vbCrLf_- 无序项 2vbCrLf_[链接文本](http://example.com)vbCrLf_这是普通文本。vbCrLf_| 列 1 | 列 2 |vbCrLf_|-------|-------|vbCrLf_| 数据 1 | 数据 2 |vbCrLf_| 数据 3 | 数据 4 |输出效果如图是不是省了很多事情不再需要切换 APP不再需要粘贴不再需要调整样式我终于感觉到做这个事情的意义了。下面把我这个开发经历和大家分享一下开发环境OSMacOS 14.4.1WordOffice Word 2016IDECursorjq 可以用 brew 安装curlapplescript大致介绍一下我的开发水平 20年 研发经验VBA10年前写过 Visual Basic Applescript没用过感觉特别语义化以后有时间了想多了解了解简单概括一下就是我有丰富的编程经验但是 VBA 比较生疏以前开发过用 VB 生成 Word 文档转 PDFApplescript 没用过。Windows 版的 VBA 自带访问 HTTP 的库其实比这个简单很多。不过我的电脑是 Mac本着优先对自己负责的态度 就先开发 Mac 版等有时间了再看看 Windows 版。先贴一下VBA的核心代码 因为 Mac 现在安全限制越来越多起初用AppleScript直接启动 shell 脚本的方式因为系统权限一直不成功成功了故障多后来查阅官方文档发现2016以后有个AppleScriptTask的函数后来就没有出现过问题。Cursor 编程第一坑它一直使用 AppleScript 这个方法虽然我一直提示它有 bugSub AI()Dim myScriptResult As String Dim selectedText As Range Dim prompt As String Prompt the user to select text in the document If Selection.Type wdSelectionIP Then prompt Trim(Selection.text) Set selectedText Selection.Range Else MsgBox Please select some text before running this macro. Exit Sub End If Cleaning textReplace(prompt, Chr(34), Chr(39))textReplace(text, vbLf,)textReplace(text, vbCr,)textReplace(text, vbCrLf,) Remove selection Selection.Collapse myScriptResult AppleScriptTask(hello.scpt, runAppleScriptTask, text) Insert response text into Word document ConvertMarkdownToWord(myScriptResult)selectedText.InsertAfter vbNewLinemyScriptResult End Sub零基础的可以直接下载我提供的文档然后选择启用宏基本用法就是先选中文本然后按fn Option F8调出宏操作, 选择AI即可。我们这里涉及两个文件一个是这个 Word 文档扩展名是docm表示这个文档包含了宏。另外一个文档是hello.scpt我测试了一下这个后缀必需是.scpt或.applescript下面是hello.scpt内容, 需要注意的是先要在智谱开放平台https://open.bigmodel.cn/注册获取自己的 Token 然后替换脚本中相应的 Tokenon runAppleScriptTask(prompt)-- Define the API URLsetapiURL tohttps://open.bigmodel.cn/api/paas/v4/chat/completions-- Define the Authorization tokensetauthToken to你自己的Authorization token-- Define the JSON payload with the escaped prompt parametersetjsonPayload to{\model\:\glm-4-plus\,\messages\:[{\content\:\prompt\,\role\:\user\}],\temperature\:0.7,\top_p\:1}-- display dialog jsonPayload -- Calculate the length of the JSON payloadsetcontentLength to length of jsonPayload -- Define the path to jq(adjustifnecessary)setjqPath to/usr/local/bin/jq-- Construct thecurlcommandsetcurlCommand tocurl -s --connect-timeout 60 --max-time 180 --location ¬-H Authorization: Bearer authToken ¬-H Accept: application/json; ¬-H Content-Type: application/json; ¬-X POST ¬-d quoted form of jsonPayload ¬ quoted form of apiURL -- display dialog curlCommand try -- Executecurlinthe backgrounddoshell script curlCommand /tmp/curl_output.txt repeat delay1-- Waitfor1second trysetcurlStatus todoshell scriptpgrep -c curl-- Count runningcurlprocessesifcurlStatus is0thenexitrepeat on errorexitrepeat -- Exitiftheres an error (e.g., no curl process) end try end repeat -- Now read the output set extractedContent to do shell script cat /tmp/curl_output.txt | jqPath -r .choices[0].message.content -- Return only the extracted content -- display dialog extractedContent return extractedContent on error errMsg -- Display the error message in a dialog box display dialog Error: errMsg end try end runAppleScriptTask on urlEncode(input) set allowedChars to abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~ set output to repeat with charininputifallowedChars contains charthensetoutput to outputcharelsesetasciiNum to ASCII number charsethexList to{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}setoutput to output%(item((asciiNum div16)1)of hexList)(item((asciiNum mod16)1)of hexList)end if end repeat return output end urlEncode on escapeForJSON(theString)set resultString to repeat with theCharacter in theString set asciiValue to ASCII number of theCharacter if asciiValue is34then--double quote set resultString to resultString\\\ else if asciiValue is92then--backslash set resultString to resultString\\\\ else if asciiValue is47then--forward slash set resultString to resultString\\/ else if asciiValue is8then--backspace set resultString to resultString\\b else if asciiValue is12then--form feed set resultString to resultString\\f else if asciiValue is10then--line feed set resultString to resultString\\n else if asciiValue is13then--carriage return set resultString to resultString\\r else if asciiValue is9then--tab set resultString to resultString\\t else if asciiValue32then--other control characters set resultString to resultString\\umy padLeft(my toHex(asciiValue),4,0)else set resultString to resultStringtheCharacter end if end repeat return resultString end escapeForJSON on toHex(theNumber)set hexChars to 0123456789ABCDEF set hexString to repeat while theNumber0set hexString to(character((theNumber mod16)1)of hexChars)hexString set theNumber to theNumber div16end repeat if hexString is then set hexString to 0 return hexString end toHex on padLeft(theString,totalLength,padChar)if length of theString ≥ totalLength then return theString return text(totalLength-(length of theString))through-1of((totalLength-(length of theString))* padChar)theString end padLeft --runAppleScriptTask(check script)这个脚本绝大部分是 Cursor 自动生成的 生成的时间10秒调教的时间好多天。这个脚本有个固定的位置下载后需要把这个文件放到相应的位置才行/Users/i/Library/Application Scripts/com.microsoft.Word记得要先安装curl和jq。一个用来调用HTTP接口一个负责解析返回的json。如果一切 Lucky 的话应该可以正常使用了。Markdown 文档解析我也是花了老大一股劲用了3-4个晚上才弄好。目前支持标题、加粗、斜体字、列表、无序列表、表格。表格花了很多时间刚开始的时候生成的表格老是跑到最开头。总结最后简单总结一下 Cursor 开发的坑Cursor 修 Bug摁下葫芦起了漂。后来我优化提示词把所有的需要的问题都列出来Cursor 或者 ChatGPT 的水平有时候也有限。VBA 处理表格的时候无论怎么修改提示词出来的表格都有问题后来我还是用老办法逐行调试解决了一下。新建的表格刚开始老是放在最前面。刚开始想着忍忍算了后来觉得实在不能让。无奈 Cursor 虽然总是承认自己修改好了但是一调试一点用都没有。我说你可以自己跑跑看有没有问题结果他耍起赖来了“不好意思我没有环境”。一副死猪不怕开水烫的样子。后来我还是用最古老的办法看看官方 VBA 开发的文档参数的意思修改了一下终于好了。Cursor 或者说 ChatGPT 可以在不熟悉的领域帮我们快速的完成原型但是目前来说并不太靠谱。同样的提示词返回的代码质量千差万别。我觉得算是老司机的一个导航吧连助理还差可靠性和理解力。福利我把代码和文档打包放在百度网盘上回复“Word”可以下载。Windows 版本暂时没有计划。目前计划的是把 Excel、PPT 都出一版方便自己工作。