Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何在MS Word中使某些文本加粗_Delphi_Ms Word_Delphi Xe5 - Fatal编程技术网

Delphi 如何在MS Word中使某些文本加粗

Delphi 如何在MS Word中使某些文本加粗,delphi,ms-word,delphi-xe5,Delphi,Ms Word,Delphi Xe5,我使用下面的代码使用Delphi XE5在MS Word中插入一个包含2个单元格的表。表格单元格的所有字体都非常直接。除了一个字。我需要这个词是粗体的,而其余的不是 请帮我调整我的代码,这样我可以使1字加粗 wrdDoc.Tables.Add(wrdSelection.Range,3,2); wrdDoc.tables.Item(3).Rows.Alignment := wdAlignRowLeft; wrdDoc.Tables.Item(3).Columns.Item(1).SetWidt

我使用下面的代码使用Delphi XE5在MS Word中插入一个包含2个单元格的表。表格单元格的所有字体都非常直接。除了一个字。我需要这个词是粗体的,而其余的不是

请帮我调整我的代码,这样我可以使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;
正如您在代码的最后一部分所看到的,有3个调用
InsertAfter()
,我插入的句子非常长。Delphi将我的电话限制在255,所以我只需要多次给他们打电话,这和打一次电话一样好

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


任何和所有的帮助都将不胜感激。谢谢你

我确实找到了办法。虽然有点凌乱,但它确实起作用

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('这个文本必须是粗体的')
解决了这个问题


任何其他可能的答案仍然是受欢迎的,因为此方法可能会使其他不相关的文本也变为粗体。

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

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';

此解决方案使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;