使用VBA将Excel电子表格中的嵌入式Word文档保存到磁盘 [英] Save an embedded Word document in an Excel spreadsheet to disk using VBA
问题描述
我们有一个Excel电子表格,当前使用存储在公司LAN中的Word模板生成报告.这适用于内部用户,但不适用于未连接到LAN的任何人,例如笔记本电脑用户.
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.
然后我的问题是,如何使用VBA将嵌入式Word模板保存到磁盘.这似乎是一个简单的任务,但我还没有在Google的任何地方找到解决方案.任何帮助将不胜感激.
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.
推荐答案
好的,我想我可能会有答案,但是只能在Excel 2010中进行测试.
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
这篇关于使用VBA将Excel电子表格中的嵌入式Word文档保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!