Vba 在Microsoft Word 2011 for OSX中通过POST发送和接收数据

Vba 在Microsoft Word 2011 for OSX中通过POST发送和接收数据,vba,macos,ms-word,Vba,Macos,Ms Word,我正在尝试移植一个宏,我们在Windows上为MS Word工作,它使用一个网站生成一个等式图像,并返回该图像以插入到文档中。当前(在Windows上工作)调用如下。当我在OSX中使用相同的调用时,我收到一个错误429,说明“ActiveX组件无法创建对象” 该错误是在Set w_page=CreateObject(“Microsoft.XMLHTTP”)语句中生成的。我尝试了几种替代方法,例如: Set w_page = CreateObject("MSXML2.ServerXMLHTTP")

我正在尝试移植一个宏,我们在Windows上为MS Word工作,它使用一个网站生成一个等式图像,并返回该图像以插入到文档中。当前(在Windows上工作)调用如下。当我在OSX中使用相同的调用时,我收到一个错误429,说明“ActiveX组件无法创建对象”

该错误是在
Set w_page=CreateObject(“Microsoft.XMLHTTP”)
语句中生成的。我尝试了几种替代方法,例如:

Set w_page = CreateObject("MSXML2.ServerXMLHTTP")

但也产生了同样的错误。请帮助我找到在OSX Word 2011中发送帖子的正确方式

对任何感兴趣的人来说,该项目都可以在

非常感谢

编辑:我发现它提供了一个与OSX Excel一起使用的选项,但我找不到与OSX Word类似的选项。有人知道一个与Excel
ActiveSheet.QueryTables
相当的单词可以在OSX中发送帖子吗

编辑:我取得了一些进展。看起来您可以通过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)
当用户通过应用程序输入
Latex\u Str
时,
Font\u Size
同样由用户定义,
WebAdd
是两个地址之一,这取决于我们是生成等式还是清理临时文件

我无法弄清楚的是如何让VBA从Python脚本读取返回值(一个包含服务器返回值的字符串,我需要它以便以后检索图像文件)。
Shell
命令似乎只返回一个PID编号。有人能帮忙吗

解决方案:我想出来了!我能够编写一个Python脚本来处理对服务器的POST请求,并将其响应打印到stdout。在社区的帮助和大量在线文档的帮助下,我能够通过
result=VBA.MacScript(command)
方法使用VBA中的AppleScript调用这个Python脚本。这使我能够将Python脚本中的标准输出读入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()

最近,我开始使用
MacScript
命令调用我的函数并获取字符串返回,而@CuberChase让我开始编写外部函数来处理对服务器的调用。非常感谢

不幸的是,没有能力通过Mac Office直接从VBA执行HTTP Post请求

当前出现错误429,说明“ActiveX组件无法创建对象”,因为OS X中没有XMLHTTP对象模型(或MSXML2或WinHttp),这些对象模型仅适用于Windows。这意味着它们不适用于任何Office程序,不仅仅是Word


您必须找到解决方法,比如可能使用AppleScript(不确定是否可行)或在外部程序(如curl)上发出shell命令

哦,哇,这看起来是可行的,但我希望它会更直接一点。我将致力于实现这一点,如果我能让它发挥作用,我将相应地标记您的答案。谢谢好东西。如果你成功的话,一定要发布你的结果。老实说,我没有想过调用外部函数,但是既然你这么说了,我可能会调用Python,让它处理请求。我相信Python安装在OSX上,所以不应该存在依赖性问题。我会做的,并相应地更新,但你的答案似乎是要工作。谢谢!这肯定让我走得更远,但不是全部。我仍然无法将服务器的响应(现在由Python脚本查询)输入VBA。此外,Python脚本本身运行良好,但是当我尝试使用
Shell
命令调用它时,VBA抛出了一个“path not found”错误。如果我得不到更多的帮助,我会在一两天后选择这个作为答案。没问题,我也会看看运行Python。
sCmd = "python " & pyPath & "getURL.py --formula " & Latex_Str & " --fontsize "_
    & Font_Size & " " & WebAdd
sResult = Shell(sCmd, vbNormalFocus)
# 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()