나는 셀 문서의 단락과 테이블을 워드 문서 내에 작성하기 위해 매크로를 작성했다. 문제는 매크로를 실행할 때 워드 문서에 나타나는 첫 번째 것이 테이블이다. , 그 아래의 텍스트, 그리고 그 아래의 다른 테이블과 텍스트 등등, 각 테이블 밑에있는 텍스트는 그 위에 있어야합니다. 무엇이 잘못하고 있는지 모르겠지만 여기까지 내 코드가있다vba 텍스트 문서에 셀을 쓰는 것
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
이 방법은 빈 테이블을 만든 다음 데이터로 채우는 것보다 약간 간단하고 간단합니다. http://www.thespreadsheetguru.com/blog/20 14/5/22/copy-paste-an-excel-table-into-microsoft-word-with-vba – Ralph
@Ralph 문제는 복사 및 전체 범위이며, 특정 셀을 사용하는 코드 그래서 나는 그 접근법을 사용하지 않는다. – AlexNotTheLion
@Ralph 프로그램은 테이블의 길이를 변수로 만들어 기준에 따라 테이블 당 셀의 특정 수를 대조한다. – AlexNotTheLion