我制作了一个 Word 模板并将其作为对象插入到 Excel 中。我用代码打开它并将数据输入书签和主要部分。但是,在代码完成处理后,我的嵌入式模板包含所有数据。所以它不再是一个模板,而是我用代码创建的文件。
嵌入式 Word 模板应作为副本打开,因为我不想对原始嵌入式模板进行任何更改或始终使用代码将其清空(或者这是唯一可行的方法?)。是否可以使用代码将嵌入的 Word 文档作为副本打开、对其进行更改并另存为 Word 文档?我在互联网上找不到任何有用的东西。
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 中就地打开文档。
我已经调整了问题中的代码。为了更容易理解(更短),我删除了 Word 文档中的编辑,除了写入几个书签之外。当然,可以将其放回原处。
我强烈建议使用 VBA 为形状指定名称。 Office 应用程序可以随意更改它们分配的通用名称,因此依赖“对象 2”有时可能会导致问题。
请勿使用
Activate
这种情况下的方法(注释掉)。如果该对象已就地激活,则无法在 Word.Application 中打开文档。使用
OLEFormat.Object.Verb
参数为xlOpen
的方法在 Word 中打开文档。打开后,OLE 对象可以设置为 Word 文档对象。
来自您的评论:
'ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument '<--- This is for closing footer and header?
不。最好与相应的Range
一起工作对象。 “那里”有很多例子。如果您在使用时遇到问题,请提出新问题。在 Word 应用程序中打开的 Word 文档可以另存为文件(就地打开的文档则不能)。然而,关于不保存编辑的问题......有两种基本方法:
- 编辑前另存为,打开该文档,编辑并保存。那么原件应该保持不变
- 在对象中进行编辑,保存,然后撤消更改。代码示例中显示了此方法
Word 的对象模型能够将任意数量的操作分组到单个“撤消记录”中。
Set objUndo = objWord.Application.UndoRecord objUndo.StartCustomRecord "Edit In Word"
编辑完成后,返回“空”(未更改)文档:
objUndo.EndCustomRecord
Set objUndo = Nothing
objWord.Undo
最后,要关闭文档,请退出 Word 应用程序而不保存更改。
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
关于excel - 编辑嵌入工作簿中的 Word 文档并另存为副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419143/