Embed a file into a Word doc using VBA

我与影子孤独终老i 提交于 2019-12-25 03:49:19

问题


I am attempting to do this from Access 2010 using Word 2010. I have a WordDoc object and cannot find a way to embed a file.

I tried starting from nothing using a bookmark:

bmFile.Range.InsertFile "C:\Users\Me\Desktop\TestFile.xlsx"

and that trew an error about the File being corrupted.

I tried editing an existing embeded file using WordDoc.InlineShapes(1) but no properties were changable or relevant.

Any ideas would be greatly appreciated.

Thanks


回答1:


From an existing file (as per your example) you should be able to do this

bmFile.Range.InlineShapes.AddOLEObject ClassType:="Excel.Sheet.12", _
  FileName:="C:\Users\Me\Desktop\TestFile.xlsx", _
  LinkToFile:=False, _
  DisplayAsIcon:=False

It's actually nastier to insert an object without using a file. You can do it by setting the FileName parameter to "", but then the OLE server will be started and display its UI (which doesn't happen when you embed from a file).

As for modifying anything in the embedded object, it isn't particularly straightforward because the object's UI tends to get in the way, but the starting point is the OLEFormat member of the Shape (or InlineShape). Difficult to find because "OLEFormat" is not a particularly informative name.




回答2:


theWordDocObject.InlineShapes.AddOLEObject _
    FileName:="pathtofile", _
    LinkToFile:=False, DisplayAsIcon:=False

(works at least with Excel files)



来源:https://stackoverflow.com/questions/26510895/embed-a-file-into-a-word-doc-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!