0

First, thank you in advance...everyone here has been incredible

So what I am trying to do is use two comboboxes (cb_RNNumberPayments and cb_CountCohorts) to insert a table into my document at a bookmark "FeeTable". Also I am trying to make sure to format the table correctly (but that is probably a future struggle). Now because the table has a header (so need to have it oRow+1) and has two additional columns (oCol +2) and based on what I have read, comboxes do not read as integers, I am running into a load of issues. I can get it to work if I just set oRow =4 and oCol =4 but since each the column and row numbers are different everytime, I want to write it to be based off the comboboxes...make senser? I am also looking to insert specific text into the column headers (Col1 Nursing Program Class, Col2 First Term, Col3 Second Term, Col4-6 are dependent on if the column exist based on the combobox cb_RNNumberPayments and the last col should be Total Fee

Sub FeeTable()
    Dim oRng As Word.Range, oTbl As Word.Table
    Dim RNPayment As Integer
    Dim nCohort As Integer
    Dim oRow As Integer
    Dim oCol As Integer

    Set oRng = ActiveDocument.Range.Bookmarks("FeeTable").Range
    Set oCol = cb_RNNumberPayments.Value + 2
    Set oRow = cb_CountCohorts.Value + 1
    Set oTbl = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=oRow, _
                                         NumColumns:=oCol)
    ActiveDocument.Bookmarks.Add "FeeTable", oTbl.Range
    oTbl.Rows.SetLeftIndent LeftIndent:=InchesToPoints(0.3), _
                            RulerStyle:=wdAdjustSameWidth
    With oTbl
        .Borders.InsideLineStyle = wdLineStyleSingle
        .Borders.InsideLineWidth = wdLineWidth025pt
        .Borders.InsideColor = wdColorBlack

        .Borders.OutsideLineStyle = wdLineStyleSingle
        .Borders.OutsideLineWidth = wdLineWidth025pt
        .Borders.OutsideColor = wdColorBlack
    End With

    On Error Resume Next
lbl_Exit:
    Exit Sub
End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • You only use `Set` for object-type variables. Try (eg) `oCol = CInt(RNPayment.Value) + 2` "I am running into a load of issues" - would be useful to be more explicit about exactly what those issues are: do you get an error message, and if so what is the error and on which line? – Tim Williams Jan 04 '19 at 23:20
  • I am getting Compile error: object required. I did change it Set oRng = ActiveDocument.Range.Bookmarks("FeeTable").Range Set oCol = CInt(cb_RNNumberPayments.Value) + 2 Set oRow = CInt(cb_CountCohorts.Value) + 1 Set oTbl = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=oRow, NumColumns:=oCol) – Philip Palmer Jan 04 '19 at 23:26
  • Ranges/Tables/etc are objects and require `Set` in assignments. `Integer/string/Long/etc` are not object types and so you don't use `Set` for those. https://stackoverflow.com/questions/349613/what-does-the-keyword-set-actually-do-in-vba – Tim Williams Jan 05 '19 at 00:01
  • Thank you, thank you, thank you!!!!!! worked like a charm – Philip Palmer Jan 05 '19 at 00:40

0 Answers0