2012-03-19 130 views
1

我想在Excel工作簿中嵌入單詞模板,以便用戶可以單擊生成報告按鈕並讓單詞使用單詞模板打開新文檔。如何在Excel VBA中使用Word.Documents.Add()中的嵌入式dotx?

下面的代碼直接編輯dotx並允許對模板進行更改,這是不受歡迎的,因爲模板包含支持自動生成報告的格式和標記。

Public Sub ExportReportEmbedded() 

Set curSheet = ActiveSheet 
Application.ScreenUpdating = False 

Dim wdApp As Word.Application, wdDoc As Word.Document 
Set ole = Sheets("Report").Shapes("Object 4").OLEFormat 
ole.Activate 
' rather than activating it, I want to use the dotx in a new Word.Documents.Add(). 
' But how? 
' wdApp.Documents.Add(ole.???) 
curSheet.Activate 
Set wdDoc = ole.Object.Object 

Set q = Sheets("Report") 
With wdDoc.ContentControls 
    For i = 1 To 62 Step 1 
     .Item(i).Range.Text = q.Range("b" & i) 
    Next 
End With 

Application.ScreenUpdating = True 

結束子

+0

傑克,你真的需要嵌入該模板?您可以隨時使用該文檔在更改完成後創建新文檔,然後使用另存爲將其另存爲另一個文件?或者你打算髮布嵌入模板的Excel文件? – 2012-03-19 08:42:24

+0

@SiddharthRout計劃分發excel文件,嵌入模板。或者,我可以通過網絡訪問它,但宏安全性和可信文檔警告正在阻礙... – Jake 2012-03-19 09:47:48

+0

在這種情況下請參閱下面的代碼。 – 2012-03-19 09:51:43

回答

1

在下面的代碼直接編輯DOTX並允許改變到模板,這是不可取的,因爲該模板包含格式和標記支持作出自動生成報告。

直接回答你的問題,你可以通過以下方式打開嵌入式DOTX使模板本身沒有被打開,但基於模板的另一個Word文檔。

希望這是你想要的嗎?

Sub Sample() 
    Dim shp As Shape 

    Set shp = Sheets("Report").Shapes.Range(Array("Object 4")) 
    shp.Select 
    Selection.Verb Verb:=xlPrimary 
End Sub 

隨訪

試試這個。我正在使用GetTempPath API來獲取用戶的臨時文件夾,然後將嵌入式文檔保存到該文件夾​​。保存文檔後,我使用.Add創建新文件。此外,我正在使用MS Word的Late Binding,因此您不需要設置任何對MS Word對象庫的引用。請讓我知道,如果您有任何疑問:)

Private Declare Function GetTempPath Lib "kernel32" _ 
Alias "GetTempPathA" (ByVal nBufferLength As Long, _ 
ByVal lpBuffer As String) As Long 

Public Sub ExportReportEmbedded() 
    Dim oWordApp As Object, oWordDoc As Object, objWord As Object 
    Dim FlName As String 
    Dim sh As Shape 
    Dim objOLE As OLEObject 

    '~~> Decide on a temporary file name which will be saved in the 
    '~~> users temporary folder 
    FlName = GetTempDirectory & "\Template.dotx" 

    Set sh = Sheets("Report").Shapes("Object 4") 

    sh.OLEFormat.Activate 

    Set objOLE = sh.OLEFormat.Object 

    Set objWord = objOLE.Object 

    '~~> Save the file to the relevant temp folder 
    objWord.SaveAs2 fileName:=FlName, FileFormat:=wdFormatXMLTemplate 

    '~~> Establish an Word application object 
    On Error Resume Next 
    Set oWordApp = GetObject(, "Word.Application") 

    If Err.Number <> 0 Then 
     Set oWordApp = CreateObject("Word.Application") 
    End If 
    Err.Clear 
    On Error GoTo 0 

    oWordApp.Visible = True 

    '~~> Create new document based on the template 
    Set oWordDoc = oWordApp.Documents.Add(Template:=FlName, NewTemplate:=False, DocumentType:=0) 

    '~~> Close the actual template that opened 
    objWord.Close savechanges:=False 

    '~~> Rest of the code 
    '~~> now you can work with oWordDoc. This will not save the actual template 

    '~~> In the end Clean Up (Delete the template saved in the temp directory) 
    Kill FlName 
End Sub 

'~~> Function to get the user's temp directory 
Function GetTempDirectory() As String 
    Dim buffer As String 
    Dim bufferLen As Long 
    buffer = Space$(256) 
    bufferLen = GetTempPath(Len(buffer), buffer) 
    If bufferLen > 0 And bufferLen < 256 Then 
     buffer = Left$(buffer, bufferLen) 
    End If 
    If InStr(buffer, Chr$(0)) <> 0 Then 
     GetTempDirectory = Left$(buffer, InStr(buffer, Chr$(0)) - 1) 
    Else 
     GetTempDirectory = buffer 
    End If 
End Function 
+0

「,以便模板本身不打開,但基於模板的另一個單詞文檔」 - 我沒有得到這個。 Word打開,我按CTRL + S並將文檔保存回Excel工作簿。有任何想法嗎? – Jake 2012-03-19 11:43:39

+0

你必須使用文件另存爲或在VBA中'.SaveAs' – 2012-03-19 11:46:59

+0

那麼在OP中如何使用與我的代碼不同的Verb?感謝您的時間......正如我所提到的,每次用戶點擊生成按鈕時,我都不想保存副本,這樣,用戶可以預覽報告而不保存它。而當他確實想要保存時,他不需要記得要另存爲 – Jake 2012-03-19 12:07:47