2016-05-04 43 views
0

マルチカラムリストボックスの内容に基づいて複数のテーブルとテキストをドキュメントに追加しようとしています。複数のブックマークとテーブルを追加

テーブルと他のすべてのブックマークを追加できますが、何らかの理由で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 
+0

を解決する方法、これはある範囲を選択波平今これを考え出しましたブックマークを挿入するときに範囲を指定してみてください。現在の方法では、すべてのブックマークが現在の選択(最初の表の直後)に挿入され、ブックマークの後にテキストが挿入されます。つまり、最後に挿入されたテキストが上部に表示されます。次のテーブルは、最初のテーブルの直後に挿入され、マージされます。 – arcadeprecinct

答えて

0

は私がそれを

encashment.Select 
Selection.MoveRight Unit:=wdCharacter, Count:=1 
Selection.TypeParagraph 
ActiveDocument.Bookmarks.Add 
.... 
2

私はあなたの問題はあなたが文書でが新しいブックマーク(複数可)を追加する必要がありますを指定していないということであると信じています。 Bookmarks.Addには、ブックマークを作成する場所を指定するためのオプションのパラメータRangeがあります。あなたがその情報を提供しなければ、Wordは望むところにそれを置くでしょう - あなたはコントロールできません。 rの終わりに従うべきであることを仮定し

は、このような何か:

Dim bm As Range 
Set bm = r.Duplicate 
ActiveDocument.Bookmarks.Add ("table_" & k, bm) 
' Do things... 
Dim reason As Range 
Set reason = bm.Duplicate 
ActiveDocument.Bookmarks.Add ("reason_" & k, reason) 
+0

コメントをいただきありがとうございます。私はこれをテストしますが、解決の前に解決します。 –

+1

@SimonPriceうまくいきましたが、可能であればSelectionオブジェクトを使わないでください。 Rangeオブジェクトを使用する方が良いでしょう:より予測可能です(コード実行中にユーザーが文書をクリックしても位置を変更することはできません)。コードはより読みやすい。複数の範囲で作業できますが、選択範囲は1つだけです。 –

関連する問題