Excel VBA 复制粘贴每个命名范围到单词
Excel VBA copy pasting each named range to word
我有动态命名的单元格范围。我需要将每个命名范围粘贴到一页单词中,然后移至下一页以获取下一个命名范围。我尝试了几个代码,我无法 do.Each 命名范围数据相互重叠。谁能帮帮我。
Set wbBook = ActiveWorkbook
Set rs = wbBook.Names(1).RefersToRange
For i = 2 To wbBook.Names.Count
Set rs = Union(rs, wbBook.Names(i).RefersToRange)
Next
rs.Copy
With wd.Range
.Collapse Direction:=0
.InsertParagraphAfter
.Collapse Direction:=0
.PasteSpecial False, False, True
Application.CutCopyMode = False
End With
听起来您想将每个范围复制到不同的页面上,所以我不确定您为什么要使用联合。这是将每个命名范围 'name' 复制到 word 文档中的新 sheet 的快速示例。注意:为了简单起见,我创建了一个新文档。
Edit - 我在最后添加了 copy/paste 数据功能。格式化等取决于您拥有或想要什么。
Sub main()
'Create new word document
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add()
Dim intCounter As Integer
Dim rtarget As Word.Range
Dim wbBook As Workbook
Set wbBook = ActiveWorkbook
'Loop Through names
For intCounter = 1 To wbBook.Names.Count
Debug.Print wbBook.Names(intCounter)
With objDoc
Set rtarget = .Range(.Content.End - 1, .Content.End - 1)
'Insert page break if not first page
If intCounter > 1 Then rtarget.insertbreak Type:=wdPageBreak
'Write name to new page of word document
rtarget.Text = wbBook.Names(intCounter).Name & vbCr
'Copy data from named range
Range(wbBook.Names(intCounter)).Copy
Set rtarget = .Range(.Content.End - 1, .Content.End - 1)
rtarget.Paste
End With
Next intCounter
End Sub
Excel
生成的 Word 文档
我认为这不是最好的解决方案(因为我通常不玩 Word VBA),但我已经尝试过了,它似乎确实有效:
Sub AddNamedRangesToWordDoc()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim intCount As Integer
Dim oRng As Range
Dim oSelection As Object
Set oWord = New Word.Application
Set oDoc = oWord.Documents.Add
oWord.Visible = True
For intCount = 1 To ActiveWorkbook.Names.Count
Set oRng = Range(ActiveWorkbook.Names(intCount).RefersToRange.Name.Name)
oRng.Copy
oDoc.ActiveWindow.Selection.PasteSpecial , , 0
Set oSelection = oWord.Selection
oSelection.InsertBreak (wdPageBreak)
Next
Set oSelection = Nothing
Set oRng = Nothing
Set oDoc = Nothing
Set oWord = Nothing
End Sub
注意: 我正在创建一个新的单词应用程序。您可能需要检查 word 是否已经打开以及您希望如何处理现有的 word 文档。另外,我不是在创建单词对象。我在项目中引用了 Microsoft Word xx.x Object Library
,因为我更喜欢使用内置库。此外,函数假定您只有 1 个工作表,并且所有范围都在该工作表中
我有动态命名的单元格范围。我需要将每个命名范围粘贴到一页单词中,然后移至下一页以获取下一个命名范围。我尝试了几个代码,我无法 do.Each 命名范围数据相互重叠。谁能帮帮我。
Set wbBook = ActiveWorkbook
Set rs = wbBook.Names(1).RefersToRange
For i = 2 To wbBook.Names.Count
Set rs = Union(rs, wbBook.Names(i).RefersToRange)
Next
rs.Copy
With wd.Range
.Collapse Direction:=0
.InsertParagraphAfter
.Collapse Direction:=0
.PasteSpecial False, False, True
Application.CutCopyMode = False
End With
听起来您想将每个范围复制到不同的页面上,所以我不确定您为什么要使用联合。这是将每个命名范围 'name' 复制到 word 文档中的新 sheet 的快速示例。注意:为了简单起见,我创建了一个新文档。
Edit - 我在最后添加了 copy/paste 数据功能。格式化等取决于您拥有或想要什么。
Sub main()
'Create new word document
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add()
Dim intCounter As Integer
Dim rtarget As Word.Range
Dim wbBook As Workbook
Set wbBook = ActiveWorkbook
'Loop Through names
For intCounter = 1 To wbBook.Names.Count
Debug.Print wbBook.Names(intCounter)
With objDoc
Set rtarget = .Range(.Content.End - 1, .Content.End - 1)
'Insert page break if not first page
If intCounter > 1 Then rtarget.insertbreak Type:=wdPageBreak
'Write name to new page of word document
rtarget.Text = wbBook.Names(intCounter).Name & vbCr
'Copy data from named range
Range(wbBook.Names(intCounter)).Copy
Set rtarget = .Range(.Content.End - 1, .Content.End - 1)
rtarget.Paste
End With
Next intCounter
End Sub
Excel
生成的 Word 文档
我认为这不是最好的解决方案(因为我通常不玩 Word VBA),但我已经尝试过了,它似乎确实有效:
Sub AddNamedRangesToWordDoc()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim intCount As Integer
Dim oRng As Range
Dim oSelection As Object
Set oWord = New Word.Application
Set oDoc = oWord.Documents.Add
oWord.Visible = True
For intCount = 1 To ActiveWorkbook.Names.Count
Set oRng = Range(ActiveWorkbook.Names(intCount).RefersToRange.Name.Name)
oRng.Copy
oDoc.ActiveWindow.Selection.PasteSpecial , , 0
Set oSelection = oWord.Selection
oSelection.InsertBreak (wdPageBreak)
Next
Set oSelection = Nothing
Set oRng = Nothing
Set oDoc = Nothing
Set oWord = Nothing
End Sub
注意: 我正在创建一个新的单词应用程序。您可能需要检查 word 是否已经打开以及您希望如何处理现有的 word 文档。另外,我不是在创建单词对象。我在项目中引用了 Microsoft Word xx.x Object Library
,因为我更喜欢使用内置库。此外,函数假定您只有 1 个工作表,并且所有范围都在该工作表中