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

查看:197
本文介绍了编辑嵌入在工作簿中的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为形状分配名称. 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.

不要 使用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 '<--- This is for closing footer and header?否.最好使用相应的Range对象.为此有很多示例.问一个新问题,如果您在使用它们时遇到问题.

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天全站免登陆