macos – 使用VBA从Word 2011 for Mac OSX中的URL添加图像

我正在努力将
project in Windows移植到OSX.我已经克服了VBA for OSX Word 2011
not allowing you to send POSTs to a server的问题,并且已经找到了
how to return a string result from an external script.现在我必须从使用我的外部脚本返回构建的URL在我的Word文件中插入一个图像.

当前的尝试如下,并在Windows中工作但在OSX中崩溃Word:

Selection.InlineShapes.AddPicture FileName:=File_Name, _
    LinkToFile:=False, SaveWithDocument:=True

在做了一些研究后,看起来MS可能已经在OSX中禁用了这个功能作为“安全风险”.我仍然需要让它发挥作用.是否有人知道VBA中的Office 2011使这项工作成为一种方式,或者禁止这种解决方法?我试图尽可能避免将图像文件写入磁盘.

更新:我已经创建了一个Python脚本,用于从URL获取图像文件,但我仍然不知道如何将这个图像从Python脚本转换为VBA,并从那里到光标所在位置的Word文档中.脚本的重要部分如下.图像作为PIL对象读入,我可以使用img.show()显示它,但我不确定这是什么文件类型或如何让VBA接受它.

# Import the required libraries
from urllib2 import urlopen, URLError
from cStringIO import StringIO
from PIL import Image

# Send request to the server and receive response, with error handling!
try:
    # Read the response and print to a file
    result = StringIO(urlopen(args.webAddr + args.filename).read())
    img = Image.open(result)
    img.show()

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

请注意,在上面,使用argparse库将args.webAddr和args.filename传递给脚本.此脚本有效,并将显示我期望的图像文件.有关如何将该图像转换为Word 2011 for OSX并将其插入光标下的任何想法?

非常感谢!

编辑:自迁移到github后更新了项目的链接.

最佳答案 老问题,但没有答案,当图像在http URL时,我看到同样的崩溃.我认为您可以使用以下解决方法

Sub insertIncludePictureAndUnlink()
' Put your URL in here...
Const theImageURL As String = ""
Dim f As Word.Field
Dim r As Word.Range
Set f = Selection.Fields.Add(Range:=Selection.Range, Type:=wdFieldIncludePicture, Text:=Chr(34) & theImageURL & Chr(34), PreserveFormatting:=False)
Set r = f.Result
f.Unlink
Set f = Nothing
' should have an inlineshape in r
Debug.Print r.InlineShapes.Count
' so now you can do whatever you need, e.g....
r.Copy
Set r = Nothing
End Sub
点赞