编辑嵌入在工作簿中的 Word 文档并另存为副本 [英] Edit Word document embedded in a workbook and save as copy

查看:20
本文介绍了编辑嵌入在工作簿中的 Word 文档并另存为副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 Word 模板并将其作为对象插入到 Excel 中.我用代码打开它并将数据输入到书签和主要部分.然而,在代码完成处理后,我的嵌入式模板里面包含了所有数据.所以它不再是一个模板,而是一个我用代码创建的文件.

I have made a Word template and inserted it to Excel as an object. I am opening it with the code and inputting data to bookmarks and main part. However after code is done doing processes my embedded template has all the data inside. So it is not a template anymore but a file I have created with the code.

嵌入式 Word 模板应作为副本打开,因为我不想对原始嵌入式模板进行任何更改或始终使用代码将其置空(或者这是唯一可能的方法吗?).无论如何,代码是否可以将嵌入的 Word 文档作为副本打开,对其进行更改并另存为 Word 文档?我在互联网上找不到任何有用的东西.

Embedded Word template should be opened as a copy, as I do not want to make any changes to original embedded template or null it with the code all the time (or is it the only way it possible to do?). Is it anyhow possible with the code to open embedded Word document as a copy, make changes to it and save as a Word document? I can't find anything useful in the internet.

Sub opentemplateWord()
Dim sh As Shape
Dim objWord As Object ''Word.Document
Dim objOLE As OLEObject
Dim wSystem As Worksheet
Dim cell As Range


    Set wSystem = Worksheets("Templates")
''The shape holding the object from 'Create from file'
''Object 2 is the name of the shape
Set sh = wSystem.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


'>------- This Part Inputs Bookmarks

objWord.Bookmarks.Item("ProjectName1").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D15").Value
objWord.Bookmarks.Item("ProjectName2").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D16").Value


'>------- This Part Inputs Text


  'ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument '<--- This is for closing footer and header?


    With objWord '<--| reference 'Selection' object


For Each cell In ThisWorkbook.Worksheets("Offer Letter").Range("C1", ThisWorkbook.Worksheets("Offer Letter").Range("C" & Rows.Count).End(xlUp))
     Select Case LCase(cell.Value)
    Case "title"
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Heading 1")
                .TypeText Text:=cell.Offset(0, -1).Text
    Case "main"
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Heading 2")
                .TypeText Text:=cell.Offset(0, -1).Text


    Case "sub"
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Heading 3")
                .TypeText Text:=cell.Offset(0, -1).Text


    Case "sub-sub"
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Heading 4")
                .TypeText Text:=cell.Offset(0, -1).Text



    End Select
   Next cell
    End With


objWord.Application.Visible = False

''Easy enough
    objWord.SaveAs2 ActiveWorkbook.Path & "" & Sheets("Other Data").Range("AN2").Value & ", " & Sheets("Other Data").Range("AN7").Value & "_" & Sheets("Other Data").Range("AN8").Value & "_" & Sheets("Other Data").Range("AX2").Value & ".docx"


End Sub

推荐答案

这是一个有趣的任务,我已经几年没看过了...诀窍是在 Word 应用程序中打开文档界面,而不是在 Excel 中就地.

This is an interesting task which I haven't looked at in a few years... The trick is to open the document in the Word application interface, instead of in-place in Excel.

我已经修改了问题中的代码.为了更容易理解(更短),除了写入几个书签之外,我删除了 Word 文档中的编辑.当然,这可以放回去.

I've adapted the code in the question. In order to make it easier to follow (shorter) I've removed the editing in the Word document except for writing to a couple of bookmarks. That can, of course, be put back in.

  1. 我非常推荐使用 VBA 为 Shape 分配名称.Office 应用程序可以随意更改它们分配的通用名称,因此依赖对象 2"有时可能会导致问题.

  1. I very much recommend using VBA to assign a name to the Shape. Office applications feel free to change a generic name they assign, so relying on "Object 2" could, sometime down the line, lead to problems.

不要NOT在这个场景中使用Activate方法(注释掉).如果对象已就地激活,则无法在 Word.Application 中打开文档.

Do NOT use the Activate method in this scenario (commented out). If the object is already activated in-place the document cannot be opened in the Word.Application.

使用带有参数 xlOpenOLEFormat.Object.Verb 方法在 Word 中打开文档.

Use the OLEFormat.Object.Verb method with the parameter xlOpen to open the document in Word.

打开后,可以将 OLE 对象设置为 Word 文档对象.

Once it's open, the OLE object can be set to a Word document object.

来自您的评论:'ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument '<--- 这是用于关闭页脚和页眉? 不.最好使用相应的 <代码>范围对象.有很多外面"的例子.如果您在使用它们时遇到问题,请提出一个新问题.

From your comments: 'ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument '<--- This is for closing footer and header? No. Better to work with the corresponding Range objects. There are lots of examples "out there" for that. Ask a new question if you run into problems using them.

在 Word 应用程序中打开的 Word 文档可以另存为文件(就地打开的文档不能).然而,关于不保存编辑的问题......有两种基本方法:

A Word document opened in the Word application can be saved as a file (a document opened in-place cannot). The question about not saving edits, however... there are two basic approaches:

  • 在编辑之前另存为,打开该文档,编辑并保存.原件应该保持不变
  • 在对象中进行编辑,保存然后撤消更改.代码示例中显示了这种方法
  • SaveAs before editing, open that document, edit and save. The original should then be untouched
  • Do the editing in the object, save then undo the changes. This approach is shown in the code sample

Word 的对象模型能够将任意数量的操作组合到单个撤消记录"中.

Word's object model is able to group any number of actions into a single "undo record".

Set objUndo = objWord.Application.UndoRecord
objUndo.StartCustomRecord "Edit In Word"

编辑完成后,回到空"(未更改)文档:

After the editing has been done, to get back to an "empty" (unchanged) document:

    objUndo.EndCustomRecord
    Set objUndo = Nothing
    objWord.Undo

最后,要关闭文档,请退出 Word 应用程序而不保存更改.

Finally, to close the document quit the Word application without saving changes.

Sub opentemplateWord()
    Dim sh As Shape
    Dim objWord As Object, objNewDoc As Object ''Word.Document
    Dim objOLE As OLEObject
    Dim wSystem As Worksheet
    Dim cell As Range       

    Set wSystem = Worksheets("Templates")
    ''The shape holding the object from 'Create from file'
    ''Object 2 is the name of the shape
    Set sh = wSystem.Shapes("WordFile")
    ''The OLE Object contained
    Set objOLE = sh.OLEFormat.Object
    'Instead of activating in-place, open in Word
    objOLE.Verb xlOpen
    Set objWord = objOLE.Object 'The Word document    

    Dim objUndo As Object 'Word.UndoRecord        
   'Be able to undo all editing performed by the macro in one step
    Set objUndo = objWord.Application.UndoRecord
    objUndo.StartCustomRecord "Edit In Word"

    With objWord
        .Bookmarks.Item("ProjectName1").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D15").Value
        .Bookmarks.Item("ProjectName2").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D16").Value

        objWord.SaveAs2 ActiveWorkbook.Path & "" & Sheets("Other Data").Range("AN2").Value & _
           ", " & Sheets("Other Data").Range("AN7").Value & "_" & _
           Sheets("Other Data").Range("AN8").Value & "_" & _
           Sheets("Other Data").Range("AX2").Value & ".docx"

        objUndo.EndCustomRecord
        Set objUndo = Nothing
        objWord.Undo
        .Application.Quit False

    End With
    Set objWord = Nothing
End Sub

这篇关于编辑嵌入在工作簿中的 Word 文档并另存为副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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