マルチカラムリストボックスの内容に基づいて複数のテーブルとテキストをドキュメントに追加しようとしています。複数のブックマークとテーブルを追加
テーブルと他のすべてのブックマークを追加できますが、何らかの理由で2番目のテーブルを追加すると、最初のテーブルを上書きするなどのようになります。
誰かが私にこれについて間違っていると私に伝えて、それを正しく置くのを助けることができたら、私は感謝します。
Private Sub Glossaries()
Dim r As Range
Set r = ActiveDocument.Bookmarks("NewRecommendationText").Range
r.Text = "text here"
With r
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
End With
'for each item in the listbox
If lbGlossaries.ListCount > 0 Then
For k = 0 To lbGlossaries.ListCount - 1
Dim tblGloss As Table
ActiveDocument.Bookmarks.Add ("table_" & k)
Dim bm As Range
Set bm = ActiveDocument.Bookmarks("table_" & k).Range
Set tblGloss = ActiveDocument.Tables.Add(bm, lbGlossaries.ListCount + 1, 5)
'Now populate the header row
With tblGloss
For x = 0 To 4
.Cell(1, x + 1).Range.Select
If x = 0 Then
Set_Table_Headers "Customer Name"
ElseIf x = 1 Then
Set_Table_Headers "Product"
ElseIf x = 2 Then
Set_Table_Headers "Fund"
ElseIf x = 3 Then
Set_Table_Headers "Risk Profile"
ElseIf x = 4 Then
Set_Table_Headers "Lump Sum Amount"
End If
Next
End With
With tblGloss
.Cell(i + 2, 0).Range.Select
Set_Table_Rows
Selection.TypeText Text:=lbGlossaries.Column(0, k) ' customer
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=lbGlossaries.Column(1, k) ' selected product
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=lbGlossaries.Column(2, k) ' selected fund
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=lbGlossaries.Column(3, k) ' risk profile
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=lbGlossaries.Column(4, k) ' amount
Selection.MoveRight Unit:=wdCell
'Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
'Selection.Cells.Merge
'Selection.MoveLeft Unit:=wdCharacter, Count:=1
'Selection.TypeText Text:=lbGlossaries.Column(5, i) ' reason
tblGloss.Select
tblGloss.Columns.AutoFit
Selection.Collapse Direction:=wdCollapseEnd
.AutoFitBehavior (wdAutoFitWindow)
End With
With bm
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
End With
ActiveDocument.Bookmarks.Add ("reason_" & k)
Dim reason As Range
Set reason = ActiveDocument.Bookmarks("reason_" & k).Range
reason.Text = lbGlossaries.Column(5, k) ' reason
''add the glossary text under here
activeBookmark = activeBookmark & "_glossary" & k
ActiveDocument.Bookmarks.Add (activeBookmark)
Dim glossary As Range
Set glossary = ActiveDocument.Bookmarks(activeBookmark).Range
glossary.Text = lbGlossaries.Column(6, i) & Chr(13) & Chr(13)
''add the tax glossary text under here
activeBookmark = activeBookmark & "_Tax_glossary" & k
ActiveDocument.Bookmarks.Add (activeBookmark)
Dim TaxGlossary As Range
Set TaxGlossary = ActiveDocument.Bookmarks(activeBookmark).Range
TaxGlossary.Text = lbGlossaries.Column(7, i) & Chr(13) & Chr(13)
''add the encashment glossary text under here
activeBookmark = activeBookmark & "_Encashment_glossary" & k
ActiveDocument.Bookmarks.Add (activeBookmark)
Dim encashment As Range
Set encashment = ActiveDocument.Bookmarks(activeBookmark).Range
encashment.Text = lbGlossaries.Column(8, i) & Chr(13) & Chr(13)
''add the encashment designation text under here
activeBookmark = activeBookmark & "_designation" & k
ActiveDocument.Bookmarks.Add (activeBookmark)
Dim designation As Range
Set designation = ActiveDocument.Bookmarks(activeBookmark).Range
If lbCgt.Column(9, k) <> "" Then
designation.Text = lbGlossaries.Column(10, i)
Else
ActiveDocument.Bookmarks(activeBookmark).Delete
End If
Next
End If
を解決する方法、これはある範囲を選択波平今これを考え出しましたブックマークを挿入するときに範囲を指定してみてください。現在の方法では、すべてのブックマークが現在の選択(最初の表の直後)に挿入され、ブックマークの後にテキストが挿入されます。つまり、最後に挿入されたテキストが上部に表示されます。次のテーブルは、最初のテーブルの直後に挿入され、マージされます。 – arcadeprecinct