Eng*_*ero 3 macos ms-word word-vba word-vba-mac
我正在尝试移植我们在Windows上使用MS Word工作的宏,该宏使用网站生成方程图像并返回该图像以插入到文档中.当前(在Windows上工作)调用如下.当我在OSX中使用相同的调用时,我收到错误429,指出"ActiveX组件无法创建对象".
' Create an xmlhttp object.
Set w_page = CreateObject("Microsoft.XMLHTTP")
' Open the connection to the remote server.
w_page.Open "POST", WebAdd, False
' Indicate that the body of the request contains form data
w_page.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' Actually send the request and return the data:
Font_Size = ComboFontSize.Value
w_page.Send "formula=" & Font_Size & "." & Latex_Str
Run Code Online (Sandbox Code Playgroud)
在Set w_page = CreateObject("Microsoft.XMLHTTP")语句上生成错误.我尝试了几种替代方法,例如:
Set w_page = CreateObject("MSXML2.ServerXMLHTTP")
Run Code Online (Sandbox Code Playgroud)
和
Set w_page = CreateObject("WinHttp.WinHttpRequest.5.1")
Run Code Online (Sandbox Code Playgroud)
但是会产生相同的错误.请帮我找到在OSX Word 2011中发送POST的正确方法.
对于任何感兴趣的人,该项目可在GitHub上获得.
非常感谢!
编辑:我发现这篇文章详细介绍了与OSX Excel一起使用的选项,但我找不到它的OSX Word模拟.有没有人知道一个相当于Excel的Word ActiveSheet.QueryTables,它可以用于在OSX中发送POST?
编辑:我取得了一些进展.看起来你可以通过VBA调用外部程序,并且所有的Mac都安装了Python,所以我编写了一个Python脚本,它使用urllib并将urllib2请求发送到服务器.Python文件还用于argparse从命令行解析公式字符串,fontsize和Web地址.
现在我的宏可以调用我的python脚本,例如:
sCmd = "python " & pyPath & "getURL.py --formula " & Latex_Str & " --fontsize "_
& Font_Size & " " & WebAdd
sResult = Shell(sCmd, vbNormalFocus)
Run Code Online (Sandbox Code Playgroud)
其中Latex_Str输入是由通过应用程序用户,Font_Size同样由用户定义的,并且WebAdd是两个地址中的一个,这取决于我们是否生成方程或清理临时文件.
我无法弄清楚的是如何让VBA从我的Python脚本中读取返回值(一个带有我的服务器返回值的字符串,以便以后检索图像文件).该Shell命令似乎只返回一个PID号.有人可以帮忙吗?
解决方案:我明白了!我能够编写一个Python脚本来处理对服务器的POST请求并将其响应打印到stdout.在社区和大量在线文档的帮助下,我可以使用该result = vba.MacScript(command)方法使用VBA中的AppleScript调用此Python脚本.这使我能够从我的Python脚本中读取stdout到VBA中的字符串变量.我的Python脚本如下:
# Import the required libraries
from urllib import urlencode
from urllib2 import Request, urlopen, URLError, ProxyHandler, build_opener, install_opener
import argparse
# Set up our argument parser
parser = argparse.ArgumentParser(description='Sends LaTeX string to web server and returns meta data used by LaTeX in Word project')
parser.add_argument('webAddr', type=str, help='Web address of LaTeX in Word server')
parser.add_argument('--formula', metavar='FRML', type=str, help='A LaTeX formula string')
parser.add_argument('--fontsize', metavar='SIZE', type=int, default=10, help='Integer representing font size (can be 10, 11, or 12. Default 10)')
parser.add_argument('--proxServ', metavar='SERV', type=str, help='Web address of proxy server, i.e. http://proxy.server.com:80')
parser.add_argument('--proxType', metavar='TYPE', type=str, default='http', help='Type of proxy server, i.e. http')
# Get the arguments from the parser
args = parser.parse_args()
# Define formula string if input
if args.formula:
values = {'formula': str(args.fontsize) + '.' + args.formula} # generate formula from args
else:
values = {}
# Define proxy settings if proxy server is input.
if args.proxServ: # set up the proxy server support
proxySupport = ProxyHandler({args.proxType: args.proxServ})
opener = build_opener(proxySupport)
install_opener(opener)
# Set up the data object
data = urlencode(values)
data = data.encode('utf-8')
# Send request to the server and receive response, with error handling!
try:
req = Request(args.webAddr, data)
# Read the response and print to a file
response = urlopen(req)
print response.read()
except URLError, e:
if hasattr(e, 'reason'): # URL error case
# a tuple containing error code and text error message
print 'Error: Failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'): # HTTP error case
# HTTP error code, see section 10 of RFC 2616 for details
print 'Error: The server could not fulfill the request.'
print 'Error code: ', e.code
# print e.read()
Run Code Online (Sandbox Code Playgroud)
我最近开始的这个相关线程指向MacScript了调用我的函数并获取字符串返回的命令的方向,而@CuberChase让我开始编写外部函数来处理对服务器的调用.非常感谢!
遗憾的是,没有能力通过Mac Office 直接从VBA 执行HTTP Post请求.
您当前收到错误429,指出"ActiveX组件无法创建对象",因为OS X中没有XMLHTTP对象模型(或MSXML2或WinHttp),这些对象模型仅适用于Windows.这意味着它们不适用于任何Office程序,而不仅仅是Word.
你必须找到一个可能使用AppleScript的工作(不确定是否可能)或在外部程序(例如curl)上发出shell命令.这个SO答案使用Curl作为HTTP Get请求,可能是最好的起点.
| 归档时间: |
|
| 查看次数: |
2817 次 |
| 最近记录: |