locked
Pasting text into table loses character formatting and changes font size RRS feed

  • Question

  • Word 2010:

    I have a macro that cuts some text from a document and then places it in a cell of a table. The first two words of the text always have bold character formatting.

    When the macro pastes the text into the table, about half the time, the bold character formatting is lost. The other half of the time, the formatting is retained.
    This font size is also changed, from 11 to 10.


    I'm pasting using Selection.PasteAndFormat (wdFormatOriginalFormatting)

    Any clues as to why this might happen?

    Sub PhotoTable()
    With Selection
    .Characters(1).Case = wdTitleSentence
    .Expand Unit:=wdSentence
    .Expand Unit:=wdParagraph
    .Paragraphs.Outdent
    .Cut
    '.Style = ActiveDocument.Styles("Normal")
    End With
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
    2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed

    With Selection.Tables(1)
    .Columns.PreferredWidth = InchesToPoints(3.5)
    .AutoFormat Format:=wdTableFormatGrid1, ApplyBorders:=False, _
    ApplyShading:=False, ApplyFont:=False, ApplyColor:=False, _
    ApplyHeadingRows:=False, ApplyLastRow:=False, ApplyFirstColumn:= _
    False, ApplyLastColumn:=False, AutoFit:=False
    End With


    With Selection.Tables(1)
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    'set width of row, cell
    .Cell(1, 1).SetWidth _
    ColumnWidth:=InchesToPoints(3.5), _
    RulerStyle:=wdAdjustNone
    .Cell(1, 2).SetWidth _
    ColumnWidth:=InchesToPoints(3.5), _
    RulerStyle:=wdAdjustNone


    End With
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    End Sub
    Saturday, August 21, 2010 8:23 PM

Answers

  • Try using the following instead

    Dim tbl As Table
    With Selection
        .Characters(1).Case = wdTitleSentence
        .Paragraphs(1).Outdent
        Set tbl = .ConvertToTable(, 1, 2)
    End With
    tbl.Columns.PreferredWidth = InchesToPoints(3.5)


    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "mcramer" wrote in message news:4f10baa4-fe0c-4f11-bf37-f580641552a3@communitybridge.codeplex.com...


    Word 2010:

    I have a macro that cuts some text from a document and then places it in a cell of a table. The first two words of the text always have bold character formatting.

    When the macro pastes the text into the table, about half the time, the bold character formatting is lost. The other half of the time, the formatting is retained.
    This font size is also changed, from 11 to 10.


    I'm pasting using Selection.PasteAndFormat (wdFormatOriginalFormatting)

    Any clues as to why this might happen?

    Sub PhotoTable()
    With Selection
    Characters(1).Case = wdTitleSentence
    Expand Unit:=wdSentence
    Expand Unit:=wdParagraph
    Paragraphs.Outdent
    Cut
    '.Style = ActiveDocument.Styles("Normal")
    End With
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
    2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed

    With Selection.Tables(1)
    Columns.PreferredWidth = InchesToPoints(3.5)
    AutoFormat Format:=wdTableFormatGrid1, ApplyBorders:=False, _
    ApplyShading:=False, ApplyFont:=False, ApplyColor:=False, _
    ApplyHeadingRows:=False, ApplyLastRow:=False, ApplyFirstColumn:= _
    False, ApplyLastColumn:=False, AutoFit:=False
    End With


    With Selection.Tables(1)
    Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    Borders(wdBorderRight).LineStyle = wdLineStyleNone
    Borders(wdBorderTop).LineStyle = wdLineStyleNone
    Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    Borders.Shadow = False
    'set width of row, cell
    Cell(1, 1).SetWidth _
    ColumnWidth:=InchesToPoints(3.5), _
    RulerStyle:=wdAdjustNone
    Cell(1, 2).SetWidth _
    ColumnWidth:=InchesToPoints(3.5), _
    RulerStyle:=wdAdjustNone


    End With
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    End Sub


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Jennifer Zhan Monday, August 23, 2010 5:42 AM
    Saturday, August 21, 2010 9:02 PM

All replies

  • I've figured out that the font size change is coming from the table style, which is a different font and size than the text I'm pasting into the table.

    Still not sure why the Character Formatting (Bold) is lost.

    Saturday, August 21, 2010 8:51 PM
  • Try using the following instead

    Dim tbl As Table
    With Selection
        .Characters(1).Case = wdTitleSentence
        .Paragraphs(1).Outdent
        Set tbl = .ConvertToTable(, 1, 2)
    End With
    tbl.Columns.PreferredWidth = InchesToPoints(3.5)


    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "mcramer" wrote in message news:4f10baa4-fe0c-4f11-bf37-f580641552a3@communitybridge.codeplex.com...


    Word 2010:

    I have a macro that cuts some text from a document and then places it in a cell of a table. The first two words of the text always have bold character formatting.

    When the macro pastes the text into the table, about half the time, the bold character formatting is lost. The other half of the time, the formatting is retained.
    This font size is also changed, from 11 to 10.


    I'm pasting using Selection.PasteAndFormat (wdFormatOriginalFormatting)

    Any clues as to why this might happen?

    Sub PhotoTable()
    With Selection
    Characters(1).Case = wdTitleSentence
    Expand Unit:=wdSentence
    Expand Unit:=wdParagraph
    Paragraphs.Outdent
    Cut
    '.Style = ActiveDocument.Styles("Normal")
    End With
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
    2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed

    With Selection.Tables(1)
    Columns.PreferredWidth = InchesToPoints(3.5)
    AutoFormat Format:=wdTableFormatGrid1, ApplyBorders:=False, _
    ApplyShading:=False, ApplyFont:=False, ApplyColor:=False, _
    ApplyHeadingRows:=False, ApplyLastRow:=False, ApplyFirstColumn:= _
    False, ApplyLastColumn:=False, AutoFit:=False
    End With


    With Selection.Tables(1)
    Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    Borders(wdBorderRight).LineStyle = wdLineStyleNone
    Borders(wdBorderTop).LineStyle = wdLineStyleNone
    Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    Borders.Shadow = False
    'set width of row, cell
    Cell(1, 1).SetWidth _
    ColumnWidth:=InchesToPoints(3.5), _
    RulerStyle:=wdAdjustNone
    Cell(1, 2).SetWidth _
    ColumnWidth:=InchesToPoints(3.5), _
    RulerStyle:=wdAdjustNone


    End With
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    End Sub


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Jennifer Zhan Monday, August 23, 2010 5:42 AM
    Saturday, August 21, 2010 9:02 PM