使用VBA将Excel电子表格中的embedded式Word文档保存到磁盘

我们有一个Excel电子表格,当前使用存储在公司LAN上的Word模板生成报告。 这对于内部用户来说工作正常,但对于没有连接到局域网的任何人(例如笔记本电脑用户)来说都不是。

pipe理不希望将模板作为单独文件分发给外部用户,但希望将其embedded到隐藏工作表上的电子表格中。 然后提出在生成报告时,embedded的模板将被保存到用户的临时path以及从那里生成的报告。

我的问题是,如何使用VBA将embedded式Word模板保存到磁盘。 这似乎是一个简单的任务,但我还没有find一个解决scheme的任何地方在谷歌。 任何帮助,将不胜感激。

好的,我想我可能会有答案,但只能在Excel 2010中进行testing。

Sub SaveEmbedded() Dim sh As Shape Dim objWord As Object ''Word.Document Dim objOLE As OLEObject ''The shape holding the object from 'Create from file' ''Object 2 is the name of the shape Set sh = ActiveSheet.Shapes("Object 2") ''Activate the contents of the object sh.OLEFormat.Activate ''The OLE Object contained Set objOLE = sh.OLEFormat.Object ''This is the bit that took time Set objWord = objOLE.Object ''Easy enough objWord.SaveAs2 Filename:="c:\docs\template.dot", FileFormat:= _ wdFormatTemplate ''1=wdFormatTemplate End Sub 

检查MSDN文档。

我相信你正在寻找SaveAs方法。

这里有一个例子。 MSDN – VBA参考 – 另存为方法