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

发布于 2024-08-23 14:02:02 字数 271 浏览 3 评论 0原文

我们有一个 Excel 电子表格,当前使用存储在公司 LAN 上的 Word 模板生成报告。这对于内部用户来说效果很好,但对于没有连接到 LAN 的任何人(例如笔记本电脑用户)来说则不然。

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-08-30 14:02:02

好吧,我想我可能有了答案,但它只在 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

Okay, I think I may have an answer, but it is only tested in 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
红颜悴 2024-08-30 14:02:02

检查 MSDN 文档。

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

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

Check the MSDN Documentation.

I believe you are looking for the SaveAs Method.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文