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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2369981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:23:11  来源:igfitidea点击:

Save an embedded Word document in an Excel spreadsheet to disk using VBA

excelvbaobjectole

提问by Paul

We have a Excel spreadsheet that currently generates a report using a Word template stored on the company LAN. This works fine for internal users but not for anyone not connected to the LAN e.g. laptop users.

我们有一个 Excel 电子表格,它当前使用存储在公司 LAN 上的 Word 模板生成报告。这适用于内部用户,但不适用于未连接到 LAN 的任何人,例如笔记本电脑用户。

Management does not want to distribute the template as a seperate file to external users but would prefer to embed this into the spreadsheet on a hidden worksheet. It is then proposed that when generating a report that the embedded template would then be saved to the users temp path and the report generated from there.

管理层不想将模板作为单独的文件分发给外部用户,而是希望将其嵌入到隐藏工作表上的电子表格中。然后建议在生成报告时将嵌入的模板保存到用户临时路径并从那里生成报告。

My question then, is how can i save the embedded Word template to disk using VBA. It would seem like a simple task but i haven't found a solution anywhere on google. Any help would be appreciated.

我的问题是,如何使用 VBA 将嵌入的 Word 模板保存到磁盘。这似乎是一项简单的任务,但我在谷歌上的任何地方都没有找到解决方案。任何帮助,将不胜感激。

回答by Fionnuala

Okay, I think I may have an answer, but it is only tested in Excel 2010.

好的,我想我可能有答案,但它仅在 Excel 2010 中进行了测试。

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

回答by CaRDiaK

Check the MSDN Documentation.

检查 MSDN 文档。

I believe you are looking for the SaveAs Method.

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

There is an example of this here; MSDN - VBA Ref - SaveAs Method

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