簡體   English   中英

使用VBA將Excel電子表格中的嵌入式Word文檔保存到磁盤

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

我們有一個Excel電子表格,目前使用存儲在公司LAN上的Word模板生成報告。 這適用於內部用戶,但不適用於未連接到LAN的任何人,例如筆記本電腦用戶。

管理層不希望將模板作為單獨的文件分發給外部用戶,而是希望將其嵌入到隱藏工作表的電子表格中。 然后建議在生成報告時,然后將嵌入的模板保存到用戶臨時路徑並從那里生成報告。

那么我的問題是,如何使用VBA將嵌入式Word模板保存到磁盤。 這似乎是一個簡單的任務,但我還沒有在谷歌的任何地方找到解決方案。 任何幫助,將不勝感激。

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

查看MSDN文檔。

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

這里有一個例子; MSDN - VBA Ref - SaveAs方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM