Edit Word document embedded in a workbook and save as copy

前端 未结 1 782
遇见更好的自我
遇见更好的自我 2021-01-25 11:43

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

1条回答
  •  一整个雨季
    2021-01-25 12:29

    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.

    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. 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.

    2. 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.

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

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

    5. 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.

    6. 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
    7. 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
    

    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
    

    0 讨论(0)
提交回复
热议问题