将文本和书签写入工作簿中嵌入的Word模板 [英] Write text and bookmarks to a Word template embedded in a workbook

查看:34
本文介绍了将文本和书签写入工作簿中嵌入的Word模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(此问题是如何使用Word中嵌入Excel工作簿中的文档的后续版本应用程序界面(而不是就地).之所以必须这样做是因为能够将结果保存为独立的文档.)

(This question is a follow-up on how to work with a document embedded in an Excel workbook in the Word application interface (instead of in-place). The reason this is necessary is to be able to save the result as an independent document.)

此代码正在执行所需的操作.打开嵌入式Word模板,将其填充并另存为新副本.唯一不起作用的部分是 .Application.Quit False .收到Word错误:"Microsoft Word停止工作".失败的可能原因是什么?

This code is doing what is needed to do. Opening embedded Word template, filling it in and saving it as new copy. The only part that is not working is .Application.Quit False. Getting Word error: "Microsoft Word has stopped working". What can be the possible reason of this failure?

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应用程序的内存可能会被绑住".一个重要因素是管理代码已使用的对象.在某些时候,需要释放这些内存以释放内存.如果未正确处理它们,则可以使应用程序保持打开状态(即使看不见),也可能会引起问题.

When automating another Office application memory can get "tied up" for various reasons. One important factor is managing objects the code has been using. At some point, these need to be "released" in order to free up memory. If they aren't properly disposed of, they can keep the application open (even if it's not visible) and cause problems.

一个可能的问题可能是 xlSheet 的多个实例化.由于始终为它分配相同的工作表,因此只需执行一次.如果要重复使用一个对象(用于另一个对象),并且代码有问题,请先将该对象设置为 Nothing ,然后再为其分配其他对象.(这通常不是必需的,但有时会有所帮助.)

One possible problem could be the multiple instantiation of xlSheet. Since it's always being assigned the same worksheet, this only needs to be done once. If you want to re-use an object (for a different object) and the code has problems, first set the object to Nothing before assigning a different object to it. (This is usually not necessary but sometimes it helps.)

问题中的代码在Excel中运行,并使用许多Word对象.这些Word对象可能会成为问题(过程结束时,Excel对象将超出范围").因此,良好的编码习惯是在不再需要时将对象设置为 Nothing .

The code in the question is running in Excel and uses a number of Word objects. These Word objects can become a problem (the Excel objects will "go out of scope" when the procedure ends). Therefore, it's good coding practice to set the object so Nothing as soon as they are no longer needed.

从问题中抽取示例代码来说明:

Sample code from the question to illustrate:

  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

这篇关于将文本和书签写入工作簿中嵌入的Word模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆