簡體   English   中英

將文本和書簽寫入工作簿中嵌入的Word模板

[英]Write text and bookmarks to a Word template embedded in a workbook

(此問題是有關如何使用 Word應用程序界面(而不是就地)中嵌入Excel工作簿中的文檔的后續操作。之所以這樣做,是因為能夠將結果另存為獨立文檔。)

此代碼正在執行所需的操作。 打開嵌入式Word模板,填寫並保存為新副本。 唯一不起作用的部分是.Application.Quit False 收到Word錯誤:“ Microsoft Word停止工作”。 失敗的可能原因是什么?

Sub opentemplateWord()
Dim sh As Shape
Dim objOLE As OLEObject
Dim objWord As Object 'Word.Document
Dim objRng As Object 'Word.Range
Dim objUndo As Object 'Word.UndoRecord
Dim cell As Excel.Range
Dim xlRng As Excel.Range
Dim xlSht As Worksheet


Set xlSht = Sheets("Main")

With xlSht
  Set xlRng = .Range("E1", .Range("E" & Rows.Count).End(xlUp))
End With

''The shape holding the object from 'Create from file'
''Object 2 is the name of the shape
Set sh = Worksheets("Templates").Shapes("WordFile")
''Activate the contents of the object
sh.OLEFormat.Activate
Set objOLE = sh.OLEFormat.Object
Set objWord = objOLE.Object

With objWord
  Set objRng = .Range.Characters.Last
  Set objUndo = .Application.UndoRecord
  objUndo.StartCustomRecord ("Doc Data")
  Set xlSht = Sheets("Main")
  .Bookmarks("ProjectName1").Range.Text = xlSht.Range("B10").Value
  .Bookmarks("ProjectName2").Range.Text = xlSht.Range("B11").Value
  .Bookmarks("ProjectName3").Range.Text = xlSht.Range("B12").Value
  .Bookmarks("ProjectName4").Range.Text = xlSht.Range("B13").Value
  .Bookmarks("ProjectName5").Range.Text = xlSht.Range("B14").Value

  For Each cell In xlRng
    objRng.InsertAfter vbCr & cell.Offset(0, -1).Text
     Select Case LCase(cell.Value)
        Case "title"
          objRng.Paragraphs.Last.Style = .Styles("Heading 1")
        Case "main"
          objRng.Paragraphs.Last.Style = .Styles("Heading 2")
        Case "sub"
          objRng.Paragraphs.Last.Style = .Styles("Heading 3")
        Case "sub-sub"
          objRng.Paragraphs.Last.Style = .Styles("Heading 4")
        Case "par"
          objRng.Paragraphs.Last.Style = .Styles("Normal")
    End Select
  Next cell
  Set xlSht = Sheets("Main")
  .SaveAs2 ActiveWorkbook.Path & "\" & _
    xlSht.Range("B2").Value & ", " & _
    xlSht.Range("B3").Value & "_" & _
    xlSht.Range("B4").Value & "_" & _
    xlSht.Range("B5").Value & ".docx"
  objUndo.EndCustomRecord
  .Undo
  .Application.Quit False '<---- Please close Word application
End With
Set objWord = Nothing '<---- Please free up objWord
End Sub

自動化時,由於各種原因,另一個Office應用程序內存可能會“阻塞”。 一個重要因素是管理代碼已使用的對象 在某些時候,需要釋放這些內存以釋放內存。 如果未正確處理它們,則它們可以使應用程序保持打開狀態(即使看不見)也可能會引起問題。

一個可能的問題可能是 xlSheet的多重實例化。 由於它總是被分配相同的工作表,因此只需要執行一次。 如果要重新使用一個對象(用於另一個對象),並且代碼有問題,請先將該對象設置為Nothing然后再為其分配其他對象。 (這通常不是必需的,但有時會有所幫助。)

問題中的代碼在Excel中運行,並使用許多Word對象。 這些Word對象可能會成為問題(過程結束時,Excel對象將“超出范圍”)。 因此,這是很好的編碼習慣這樣設置的對象Nothing ,只要他們不再需要。

問題中的示例代碼來說明:

  With objWord  'a Word document
     objUndo.EndCustomRecord
    .Undo
    Set objUndo = Nothing '<---- Release Word objects in Excel code
    Set objRng = Nothing
    .Application.Quit False '<---- Please close Word application
  End With
  Set objWord = Nothing '<---- Please free up objWord
End Sub

暫無
暫無

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

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