Excel 编辑嵌入工作簿中的Word文档并另存为副本
我制作了一个Word模板,并将其作为对象插入Excel。我打开它的代码和输入数据的书签和主要部分。然而,在代码完成处理后,我的嵌入模板中包含了所有数据。所以它不再是一个模板,而是我用代码创建的一个文件 嵌入式Word模板应该作为副本打开,因为我不想对原始嵌入式模板进行任何更改,也不想一直使用代码将其置为空(或者这是唯一可行的方法?)。该代码是否可以打开嵌入的Word文档作为副本,对其进行更改并另存为Word文档?我在网上找不到任何有用的东西Excel 编辑嵌入工作簿中的Word文档并另存为副本,excel,vba,ms-word,Excel,Vba,Ms Word,我制作了一个Word模板,并将其作为对象插入Excel。我打开它的代码和输入数据的书签和主要部分。然而,在代码完成处理后,我的嵌入模板中包含了所有数据。所以它不再是一个模板,而是我用代码创建的一个文件 嵌入式Word模板应该作为副本打开,因为我不想对原始嵌入式模板进行任何更改,也不想一直使用代码将其置为空(或者这是唯一可行的方法?)。该代码是否可以打开嵌入的Word文档作为副本,对其进行更改并另存为Word文档?我在网上找不到任何有用的东西 Sub opentemplateWord() Dim
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
子opentemplateWord()
像形状一样暗淡
将objWord设置为对象“”Word.Document
暗淡的物体
将系统设置为工作表
暗淡单元格作为范围
设置wSystem=工作表(“模板”)
''从“从文件创建”保存对象的形状
''对象2是形状的名称
设置sh=wSystem.Shapes(“对象2”)
''激活对象的内容
sh.OLEFormat.Activate
''包含的OLE对象
设置objOLE=sh.OLEFormat.Object
“这是需要时间的一点
设置objWord=objOLE.Object
'>----此部分输入书签
objWord.Bookmarks.Item(“ProjectName1”).Range.Text=此工作簿.Sheets(“MAIN”).Range(“D15”).Value
objWord.Bookmarks.Item(“ProjectName2”).Range.Text=此工作簿.Sheets(“MAIN”).Range(“D16”).Value
'>----此部分输入文本
'ActiveWindow.ActivePane.View.SeekView=wdSeekIndocument'这是一项有趣的任务,我已经好几年没有看过了。。。诀窍是在Word应用程序界面中打开文档,而不是在Excel中就地打开
我已经修改了问题中的代码。为了让它更容易理解(更简短),我删除了Word文档中的编辑功能,除了写入几个书签之外。当然,这可以放回去
我非常推荐使用VBA为形状指定名称。Office应用程序可以随意更改它们指定的通用名称,因此依赖“Object 2”有时可能会导致问题
在这种情况下,不要使用Activate
方法(注释掉)。如果对象已就地激活,则无法在Word.Application中打开文档
使用参数为xlOpen
的OLEFormat.Object.Verb
方法在Word中打开文档
打开OLE对象后,可以将其设置为Word文档对象
根据您的评论:“ActiveWindow.ActivePane.View.SeekView=WdSeekIndocument”我已经尝试从同一个Excel中运行此文件两次,第二次运行时也出现错误。它打开、运行Word文件并将其保存到目标,但在第二次运行时,您会收到“Microsoft Word已停止工作”,“Windows可以尝试恢复您的信息”。“关闭程序”@user7202022再次,这需要是一个新问题,带有,因为这不是原始问题/要求的一部分。堆栈溢出就是这样设计的——一个问答网站,而不是一个讨论论坛。显然,您要做的事情(将Word模板作为Excel工作簿的一部分分发)并不简单,也不是Office设计的一部分。正如您所发现的,没有现成的或简单的解决方案。在这一点上,我能提供的唯一即兴建议是将嵌入的文档作为模板保存到磁盘,然后从中进行“会话”。
Set objUndo = objWord.Application.UndoRecord
objUndo.StartCustomRecord "Edit In Word"
objUndo.EndCustomRecord
Set objUndo = Nothing
objWord.Undo
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