2014-04-14 50 views
0

我正在使用以下代码在MS Word中使用Delphi XE5插入一个包含2个单元格的表。表格单元格的所有字体都非常简单。除1字外。我需要这个词大胆,而其余的不是。如何在MS Word中使某些文本变为粗体

请帮我调整我的代码,这样我就可以让1个字加粗。

wrdDoc.Tables.Add(wrdSelection.Range,3,2); 
wrdDoc.tables.Item(3).Rows.Alignment := wdAlignRowLeft; 

wrdDoc.Tables.Item(3).Columns.Item(1).SetWidth(36,wdAdjustNone); 
wrdDoc.Tables.Item(3).Columns.Item(2).SetWidth(379,wdAdjustNone); 
wrdDoc.tables.Item(3).Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone; 
wrdDoc.tables.Item(3).Borders.Item(wdBorderRight).LineStyle := wdLineStyleNone; 
wrdDoc.tables.Item(3).Borders.Item(wdBorderVertical).LineStyle := wdLineStyleNone; 
wrdDoc.tables.Item(3).Borders.Item(wdBorderTop).LineStyle := wdLineStyleNone; 
wrdDoc.tables.Item(3).Borders.Item(wdBorderBottom).LineStyle := wdLineStyleNone; 
wrdDoc.tables.Item(3).Borders.Item(wdBorderHorizontal).LineStyle := wdLineStyleNone; 

wrdDoc.Tables.Item(3).Cell(1,1).Range.InsertAfter('8.1'); 
wrdDoc.Tables.Item(3).Cell(1,1).Range.Paragraphs.Alignment := wdAlignParagraphleft; 
wrdDoc.Tables.Item(3).Cell(1,1).Range.Font.Size := 12; 
wrdDoc.Tables.Item(3).Cell(1,1).Range.Font.Bold := false; 
wrdDoc.Tables.Item(3).Cell(1,1).Range.Font.underline := false; 

wrdDoc.Tables.Item(3).Cell(1,2).Range.InsertAfter('this will not be bold text'); 
wrdDoc.Tables.Item(3).Cell(1,2).Range.InsertAfter('this will not be bold text'); 
wrdDoc.Tables.Item(3).Cell(1,2).Range.InsertAfter('THIS TEXT MUST BE BOLD'); 
wrdDoc.Tables.Item(3).Cell(1,2).Range.Paragraphs.Alignment := wdAlignParagraphJustify; 
wrdDoc.Tables.Item(3).Cell(1,2).Range.Font.Size := 12; 
wrdDoc.Tables.Item(3).Cell(1,2).Range.Font.Bold := false; 
wrdDoc.Tables.Item(3).Cell(1,2).Range.Font.underline := false; 

正如你可以在代码的最后一部分看到,有一个InsertAfter() 3个电话,我插入句子很长。而德尔福限制我到255,所以我只是多次打电话给他们,这和一次打电话一样好。

只有最后一次调用,必须是粗体。其余的应该保持上面定义的格式。

任何和所有帮助,将不胜感激。谢谢

回答

1

我确实设法找到了一种方法。这有点麻烦,但做这项工作。

Procedure MakeBold(SearchStr:String); 
Begin 
WrdApp.Selection.Find.ClearFormatting; 
    WrdApp.Selection.Find.Text := SearchStr; 
    WrdApp.Selection.Find.Forward := True; 
    WrdApp.Selection.Find.Wrap := wdFindContinue; 
    WrdApp.Selection.Find.Format := False; 
    WrdApp.Selection.Find.MatchCase := true; 
    WrdApp.Selection.Find.MatchWholeWord := wrfMatchCase in Flags; 
    WrdApp.Selection.Find.MatchWildcards :=wrfMatchWildcards in Flags; 
    WrdApp.Selection.Find.MatchSoundsLike := False; 
    WrdApp.Selection.Find.MatchAllWordForms := False; 
    { Perform the search } 
    WrdApp.Selection.Find.Execute(); 
    WrdApp.Selection.Font.Bold:=True; 
End; 

然后我只是打电话MakeBold('THIS TEXT MUST BE BOLD');它解决了这个问题。

任何其他可能的答案仍然是受欢迎的,因为这种方法可能会使其他不相关的文本也大胆。

0

你工作太辛苦了。 :-)您需要更熟悉Word范围(以及使用临时变量)。使用Word2010中单元在Delphi西雅图10进行测试,使用Word 2013年

var 
    WordTbl: Table; 

// Grab a local reference to the table for ease of use 
Tbl := wrdDoc.Selection.Tables.Item(3); 
Tbl.Cell(1, 2).Range.Text := 'not bold'; 

// Make sure the cell is the current selection 
Tbl.Cell(1, 2).Select; 

// Move the selection to the end of the text we wrote before 
wrdDoc.Selection.EndKey(wdLine, wdMove); 

// Add the next section of the text and move to the end, so we 
// know for sure where Word thinks we are now. Notice the spaces 
// at both ends - for some reason they make a difference 
wrdDoc.Selection.Range.Text := ' BOLD TEXT '; 
wrdDoc.Selection.EndKey(wdLine, wdMove); 

// Move back two words to select the text we just added and bold 
wrdDoc.Selection.MoveLeft(wdWord, 2, wdExtend); 
wrdDoc.Selection.Font.Bold := 1; 

// End of the line again, and turn bold off, then more text 
wrdDoc.Selection.EndKey(wdLine, wdMove); 
wrdDoc.Selection.Font.Bold := 0; 
wrdDoc.Selection.Range.Text := 'not bold again'; 
0

该解决方案在所有的ActiveDocument匹配是大胆

function TfmMain.MakeBold(SearchStr: String; App: OLEVariant {Word application var}): Integer; 
var 
    Find: OLEVariant; 
begin 
    Find := App.Selection.Find; 
    Find.ClearFormatting; 
    Find.Forward := true; 
    Find.Wrap := wdFindStop; 
    Find.Format := false; 
    Find.MatchCase := false; 
    Find.MatchWholeWord := true; 
    Find.MatchWildcards := false; 
    Find.MatchSoundsLike := false; 
    Find.MatchAllWordForms := false; 

    Find.Text := SearchStr; 
    while Find.Execute() do 
    begin 
    App.Selection.Font.Bold := true; 
    App.Selection.Collapse(wdCollapseEnd); 
    end; 
    Find := UnAssigned; 
end; 
相关问题