VB Macro problem in Word (simple code - I just suck)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • derelict
    New Member
    • Sep 2006
    • 1

    VB Macro problem in Word (simple code - I just suck)

    Hey all, im getting desperate now.
    I have a macro running in Word 2003, when I run the macro it *should* put a 'bottom' cell border in each cell that has the style used - this included a border at the top of the table and one at the bottom, like a set of container border for a table with no border within it. i.e

    table heading
    _______________ _______________ _ << top border
    text in the table spanning multiple rows and columns
    _______________ _______________ __<< bottom border

    So far, the macro will run, only put the border into a cell that has text!, I cant figure out how to make it go in all cells that have the style, regardless of text.
    Also, the very last table in the document end up with all table cells getting the border put in, regardless of style.

    This is driving me nuts, im not much of a programmer, but this makes me feel stupid. Can someone plz help me.
    Thanks alot!

    ==========Macro code===========

    Sub AfterPublish()

    AddCellFormatti ng "Table border"

    End Sub

    Private Sub AddCellFormatti ng(StyleName)
    ' Searches through the document looking for every
    ' instance of the style "Table Border" and ensures
    ' that the borders are turned on for the cells
    On Error Resume Next
    Dim cellInst As Cell

    If Not ActiveDocument. Styles(StyleNam e).InUse Then
    ' Gets to here if:
    ' a) the style exists in the document but is not in use.
    ' b) the style does not exist in the document.
    'Err.Clear
    Exit Sub
    End If
    ' Set the selection to the beginning of the document.
    Selection.Start Of wdStory
    With Selection.Find
    ' Search direction is forward.
    .Forward = True
    ' Clear any other previous search formatting.
    .ClearFormattin g
    .Replacement.Cl earFormatting
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLik e = False
    .MatchAllWordFo rms = False
    ' Search for the style indicating that the cell should have borders.

    .Style = ActiveDocument. Styles(StyleNam e)

    ' Search for every instance of this style.
    Do While .Execute
    ' Ensure that the selection is in a table.
    If Selection.Infor mation(wdWithIn Table) Then
    'For each cell within the current selection.
    For Each cellInst In Selection.Cells
    ' Apply custom formatting here:
    ' To turn off any of these lines, remove or
    ' prefix with a single quote (') character.

    ' The following line adds a border around the selected cells...
    cellInst.Border s(wdBorderBotto m).LineStyle = wdLineStyleSing le


    Next
    End If
    ' Move to the next paragraph
    Selection.Move wdParagraph, 1
    Loop
    End With
    End Sub
Working...