2016-04-13 18 views
0

私は長い間、初めてVBAで作業しており、ドキュメントをテーブルに追加する際に助けが必要です。現時点ではドキュメントへのテーブルの追加エラー - オブジェクト変数またはブロック変数が設定されていません

問題の行が

.Cell(1, x).Range.Select 

であると私は取得エラーがオブジェクト変数またはブロック変数が設定されていないとされます。私の人生の間、私はこの場所でどこが間違っているのか分かりません。

私はテーブルに渡すために探しています3次元の文字列配列を渡していますし、これは、誰かがこれを確認することができれば、私は感謝される私は一瞬

Private Sub Create_Sized_Table_Multi_Column(i_Rows As Integer, i_Columns As Integer) 
'create a table 
Set t_newtable = ActiveDocument.Tables.Add(Selection.Range, i_Rows, i_Columns) 
End Sub 


Private Sub BuildTable(arr() As String, colCount As Integer, bookMark As String) 

Dim t_newtable As Table 
Dim i_Fund_Quantity As Integer 
Dim i_Rows_Required As Integer 
Dim i_Columns_Required As Integer 
      'Number of funds is the upperbound + 1 to allow for the zero relative 
      i_Fund_Quantity = UBound(arr) + 1 
      'header Row 
      i_Rows_Required = UBound(arr) + 1 

      'Number of columns 
      i_Columns_Required = colCount 

'Add a table - this table will contain moved funds 
'creates the table dimensioned by i_rows x i_column 
      Create_Sized_Table_Multi_Column i_Rows_Required, i_Columns_Required 

      'Now populate the header row 
      With t_newtable 
       For x = 0 To i_Columns_Required 
        .Cell(1, x).Range.Select 
        If x = 1 Then 
         Set_Table_Headers "Existing Fund" 
        ElseIf x = 2 Then 
         Set_Table_Headers "Customer Name" 
        ElseIf x = 3 Then 
         Set_Table_Headers "Switch To" 
         ActiveDocument.Bookmarks.Add ("bk_Switched_Table") 
        End If 
       Next 
      End With 


      'Populate the table with the fund details 
      ''//sp write to table here 

      With t_newtable 
       'Position cursor at first insertion point 
       'ActiveDocument.Bookmarks("bk_Switched_Table").Select 
       'Now create a loop 
       For i_Loop_Rows = 0 To UBound(arr) 

         Set_Table_Rows 
         Selection.TypeText arr(i, 0) 
         Selection.MoveRight UNIT:=wdCell 
         Selection.TypeText arr(i, 1) 
         Selection.MoveRight UNIT:=wdCell 
         Selection.TypeText arr(i, 2) 
         t_newtable.Columns(3).Select 
         t_newtable.Columns.AutoFit 
         Selection.Collapse Direction:=wdCollapseEnd 


       Next 
      End With 

      ActiveDocument.Bookmarks(bookMark).Select 
      Selection.TypeParagraph 
      Selection.TypeText s_Text 
      Selection.TypeParagraph 
      ActiveDocument.Bookmarks.Add (bookMark) 

End Sub 

にして働いていたコードです私がどこに間違っていて、どこを変える必要があるかを教えてください。あなたのBuildTableサブにt_newtableを設定することはありません

おかげ

サイモン

答えて

1

手順Create_Sized_Table_Multi_Columnにt_ newtableを宣言すると、そのスコープに限定されます。そのプロシージャを呼び出す場合は、テーブルを作成して呼び出したコードで使用できるようにするには、SubFunctionに変更して、テーブルを返す必要があります。例えば

Private Sub BuildTable(arr() As String, colCount As Integer, bookMark As String) 

    Dim t_newtable As Table 
    'Add a table - this table will contain moved funds 
    'creates the table dimensioned by i_rows x i_column 
    Set t_newtable = Create_Sized_Table_Multi_Column(i_Rows_Required, _ 
               i_Columns_Required) 
End Sub 

関数への呼び出しの周りに追加の括弧に注意してください。

Private Function Create_Sized_Table_Multi_Column(i_Rows As Integer, _ 
              i_Columns As Integer) As Table 

    'create a table 
    Set Create_Sized_Table_Multi_Column = ActiveDocument.Tables.Add(_ 
             Selection.Range, i_Rows, i_Columns) 
End Function 

はその後あなたがこの(コードはわかりやすくするために短縮)のようにそれを使用しています。これらは、何かがコールから返されるときに必要です。

+0

ありがとうございます!私が言っているようにVBAを使用してからしばらくしていて、家に生まれたばかりの新しいものを持っています... –

+0

はい、非常に気を散らす:-) –

1

。あなたがどこかにあなたのテーブルを作成する必要がある場合、あなたはあなたのt_newtableあなたがそれを可能にするオブジェクトをインスタンス化します

Set t_newtable = ActiveDocument.Tables(indexOfYourTable)

に持っています。 注:indexOfYourTableは、0に基づいていない1です。

OR

あなたのBuildTableサブ内側にあなたのCreate_Sized_Table_Multi_Columnサブでコードの行を入れて、あなたのBuildTableサブに必要な変数を渡すことができます。

関連する問題