vba将单元格写入文本文档
我写了一个宏来在word文档中写入单元格信息段落和表格,我遇到的问题是,当我运行宏时,word文档中首先出现的是一个表格,表格下面的文本,然后是另一个表格和表格下面的文本,以此类推,问题是每个表格下面的文本都需要在它上面。我不确定我做错了什么,但这是我到目前为止的代码vba将单元格写入文本文档,vba,excel,ms-word,Vba,Excel,Ms Word,我写了一个宏来在word文档中写入单元格信息段落和表格,我遇到的问题是,当我运行宏时,word文档中首先出现的是一个表格,表格下面的文本,然后是另一个表格和表格下面的文本,以此类推,问题是每个表格下面的文本都需要在它上面。我不确定我做错了什么,但这是我到目前为止的代码 Set objword = CreateObject("Word.Application") Set objdoc = objword.Documents.Add Set objselection = objword.Select
Set objword = CreateObject("Word.Application")
Set objdoc = objword.Documents.Add
Set objselection = objword.Selection
Set objrange = objdoc.Content
objword.Visible = True
Set objrange = objdoc.Content
objrange.Collapse Direction:=wdCollapseend
' write paragraph
If Cells(i, 9) <> "" Then
objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & "." & Mid(CStr(Cells(i, 1)), 3, 2) & "." & Mid(CStr(Cells(i, 1)), 5, 2) & "." & Mid(CStr(Cells(i, 1)), 7, 2) & " " & CStr(Cells(i, 9))
objselection.TypeParagraph
Else
If Cells(i, 8) <> "" Then
objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & "." & Mid(CStr(Cells(i, 1)), 3, 2) & "." & Mid(CStr(Cells(i, 1)), 5, 2) & " " & CStr(Cells(i, 8))
objselection.TypeParagraph
Else
If Cells(i, 7) <> "" Then
objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & "." & Mid(CStr(Cells(i, 1)), 3, 2) & " " & CStr(Cells(i, 7))
objselection.TypeParagraph
Else
If Cells(i, 6) <> "" Then
objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & " " & CStr(Cells(i, 6))
objselection.TypeParagraph
End If
End If
End If
End If
With objselection
.TypeText Text:=Cells(i, 6) & "|" & Cells(i, 7) & "|" & Cells(i, 8) & "|" & Cells(i, 9)
.TypeParagraph
.TypeText Text:=CStr(Worksheets("1").Cells(b, 11))
.TypeParagraph
.TypeText Text:="test"
.TypeParagraph
End With
'write table
Set objtable = objdoc.Tables.Add(objrange, cn, 2)
objtable.AutoFormat (16)
'populate table
For cpopulate = 0 To cn - 1
With objtable
.Cell(cpopulate + 1, 1).Range.Text = Cells(i + cpopulate, 1)
.Cell(cpopulate + 1, 2).Range.Text = CStr(Cells(i + cpoppulate, 10))
End With
Next cpopulate
Set objword=CreateObject(“Word.Application”)
设置objdoc=objword.Documents.Add
设置objselection=objword.Selection
设置objrange=objdoc.Content
objword.Visible=True
设置objrange=objdoc.Content
对象范围.折叠方向:=wdCollapseend
写一段
如果单元格(i,9)“,则
objselection.TypeText:=Mid(CStr(单元格(i,1)),1,2)&“&Mid(单元格(i,1)),3,2)&“&Mid(单元格(i,1)),5,2)&“&Mid(单元格(i,1)),7,2)&”&CStr(单元格(i,9))
objselection.typeparation
其他的
如果单元格(i,8)“,则
objselection.TypeText:=Mid(CStr(单元格(i,1)),1,2)&“&Mid(单元格(i,1)),3,2)&“&Mid(单元格(i,1)),5,2)&”&CStr(单元格(i,8))
objselection.typeparation
其他的
如果单元格(i,7)“,则
objselection.TypeText:=Mid(CStr(单元格(i,1)),1,2)&“&Mid(CStr(单元格(i,1)),3,2)&”&CStr(单元格(i,7))
objselection.typeparation
其他的
如果单元格(i,6)“,则
objselection.TypeText:=Mid(CStr(单元格(i,1)),1,2)&“&CStr(单元格(i,6))
objselection.typeparation
如果结束
如果结束
如果结束
如果结束
选举
.TypeText文本:=单元格(i,6)&“|”&单元格(i,7)&“|”&单元格(i,8)&“|”&单元格(i,9)
.打字段落
.TypeText:=CStr(工作表(“1”)。单元格(b,11))
.打字段落
.TypeText:=“测试”
.打字段落
以
"写表"
Set objtable=objdoc.Tables.Add(objrange,cn,2)
objtable.AutoFormat(16)
'填充表格
对于cpopulate=0到cn-1
可反对的
.Cell(cpopulate+1,1).Range.Text=单元格(i+cpopulate,1)
.Cell(cpopulate+1,2).Range.Text=CStr(Cells(i+cpopulate,10))
以
下一个
我想我上面的评论太短了,不能确保你真正理解我的意思。我想说的是,在Excel中创建表格比在Word中创建表格要容易得多(至少对我来说)
尤其是当您拥有所有的单元格()
和范围()
引用(在VBA中)以快速寻址每个单元格并用所需的数据填充它时。之后,您甚至可能会考虑将其列为<代码> ListObjs<代码>,并在该表上应用一个漂亮的小预置。
一旦表格完成并包含您想要的所有数据,并且按照您的意愿进行格式化,您就可以使用两行简单的代码轻松复制最终表格:
ws.Range("A1:G20").Copy
objDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
为了让事情变得更简单,您可以使用“临时”工作表,一旦您在表格上进行了复制,该工作表将立即被删除:
Option Explicit
Public Sub CopyTableToWord()
Dim ws As Worksheet
'Setup a temporary sheet
Set ws = ThisWorkbook.Worksheets.Add
'here should be your above code to fill the table with all the data that you want / need
'afterwards you may even want to format that table
'... and then you can easily copy over the final table to Word
ws.Range("A1:G20").Copy '... or whatever the resulting range will be
objDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
'... remove the temporary sheet afterwards if you wish or keep it
' so you don't have to recreate it each time
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End Sub
这种方法似乎更简单、更直接,而不是创建一个空表,然后用数据填充它:@Ralph问题是它的副本和整个范围,我开发的代码需要更多的特定单元格,所以我不能使用它approach@Ralph该程序还整理每个表中特定数量的单元格,基于一组条件,将表长度设为变量