문제

델파이 XE5를 사용하여 MS Word에서 2 개의 셀이있는 테이블을 삽입하려면 다음 코드를 사용하고 있습니다.테이블의 세포의 모든 글꼴은 꽤 똑바로 앞으로 나아갑니다.1 단어를 제외하고.나머지가 아닌 동안이 단어가 굵게 표시됩니다.

내 코드를 조정할 수 있도록 도와주세요. 그래서 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로 제한하므로 방금 한 번 이상이라고 부르며 한 번 전화하는 것만 큼 좋습니다.

마지막 호출 만 굵게해야합니다.나머지는 위에 정의 된 형식을 유지해야합니다.

어떤 도움 모두를 알 수 있습니다.고맙습니다

도움이 되었습니까?

해결책

나는 길을 찾을 수 있었다.그것은 조금 지저분하지만 그 일을합니다.

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');를 호출하고 문제를 해결합니다.

다른 답변은 여전히 환영합니다. 왜냐하면이 방법은 다른 무관 한 텍스트도 굵게 표시 될 수 있기 때문입니다.

다른 팁

멀리 노력하고 있습니다.:-) 단어 범위 (및 임시 변수의 사용)에 익숙해 져야합니다.델파이 10 시애틀에서 Word2010 단위를 사용하여 테스트했습니다.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top