Here is an excerpt of the actual method. Look for ShowMessage('Press enter
to continue.') in code if this is or Break Point not exists then the
following image does not get inserted in the selected range.
Code:
try
for lIndex := 1 to 6 do
begin
lMainDoc.Activate;
Selection.Find.ClearFormatting;
loleUnit := wdStory;
loleExtend := EmptyParam;
Selection.HomeKey(loleUnit, loleExtend);
lSearchValueFound := True;
while lSearchValueFound do
begin
loleSearchValue :=
MapIndexToDefinedConstantsDescription(lIndex);
loleSearchForward := True;
if Selection.Find.Execute(loleSearchValue,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam,
loleSearchForward,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam) then
begin
{get selection formatted information}
loleFontName := Selection.Range.FormattedText.Font.Name;
loleFontSize := Selection.Range.FormattedText.Font.Size;
loleColourIndex :=
Selection.Range.FormattedText.Font.ColorIndex;
loleBold := Selection.Range.FormattedText.Bold;
loleUnderLine:= Selection.Range.FormattedText.Underline;
lSearchValueFound := True;
{delete tag}
loleCount := 1;
loleUnit := wdCharacter;
Selection.Delete(loleUnit, loleCount);
loleTextColumnCount :=
Selection.PageSetup.TextColumns.Count;
if loleTextColumnCount = 1 then lMoreThanOneTextColumn
:= False;
if loleTextColumnCount > 1 then
begin
lMoreThanOneTextColumn := True;
loleTextColumnSpacing :=
Selection.PageSetup.TextColumns.Spacing;
end;
lTemp := rLogin.LocalDrive +
cMailMergeTemplateWorkingFolder + loleSearchValue;
{insert the description into temp doc}
lTempDoc.Activate;
Selection.InsertFile(lTemp,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam);
{back to beginng of doc}
loleUnit := wdStory;
loleExtend := EmptyParam;
Selection.HomeKey(loleUnit, loleExtend);
lContinue := True;
repeat
lTempDoc.Activate;
lParams := TParameterList.Create;
try
AnalyseDescription(lTemp + '.txt', lParams);
{set variables based on aParameterList values}
lPageBreak :=
Boolean(StrToIntDef(lParams.GetParamValue(cPageBreak), 0));
lTextToRight:=
Boolean(StrToIntDef(lParams.GetParamValue(cTextToRight), 0));
lXFOCode :=
lParams.GetParamValue(cXFOCode);
lPhotoTablePhotos :=
lParams.GetParamValue(cPhotoTablePhotos);
lContinue :=
Boolean(StrToIntDef(lParams.GetParamValue(cAnyTexToMerge), 0));
lTextBeforeTag :=
Boolean(StrToIntDef(lParams.GetParamValue(cTextBeforeTag), 0));
lTextAfterTag :=
Boolean(StrToIntDef(lParams.GetParamValue(cTextAfterTag), 0));
lTagAfterTag :=
Boolean(StrToIntDef(lParams.GetParamValue(cSpecialTagAfterTag), 0));
lSpecialTagFound :=
Boolean(StrToIntDef(lParams.GetParamValue(cSpecialTag), 0));
lPairedXFOCode :=
Boolean(StrToIntDef(lParams.GetParamValue(cPaired), 0));
lPairedXFOCodeFound
:=
Boolean(StrToIntDef(lParams.GetParamValue(cPairedXFOCodeFound), 0));
lPairCodeOnSameLine
:=
Boolean(StrToIntDef(lParams.GetParamValue(cPairCodeOnSameLine), 0));
finally
FreeAndNil(lParams);
end;
if not lContinue then
begin
lMainDoc.Activate;
Break;
end;
{now find the XFOCode (this code includes not only xfo
but all special codes) in the temp doc}
if lSpecialTagFound then
begin
{back to beginng of temp doc}
loleUnit := wdStory;
loleExtend := EmptyParam;
Selection.HomeKey(loleUnit, loleExtend);
Selection.Find.ClearFormatting;
loleSearchForward := True;
loleSearchValue := lXFOCode;
if Selection.Find.Execute(loleSearchValue,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam,
loleSearchForward,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam) then
begin
{Delete the tag in Temp doc
this delete, deletes any paired codes if its
occurance is on the same line}
loleUnit := wdLine;
Selection.EndKey(loleUnit, EmptyParam);
loleUnit := wdLine;
loleExtend := wdExtend;
Selection.HomeKey(loleUnit, loleExtend);
loleCount := 1;
Selection.MoveUp(loleUnit, loleCount, loleExtend);
Selection.EndKey(loleUnit, loleExtend);
loleUnit := wdCharacter;
Selection.Delete(loleUnit, loleCount);
{check also if XFOCode is paired then delete the
paired code as well (only if not on same line}
case lPairedXFOCodeFound of
True : begin
case lPairCodeOnSameLine of
False : begin
if not lTextToRight
then
begin
Selection.Find.ClearFormatting;
loleSearchForward :=
True;
loleSearchValue :=
cWaitPhotoCode;
if
Selection.Find.Execute(loleSearchValue,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam,
loleSearchForward,
EmptyParam,
EmptyParam,
EmptyParam,
EmptyParam) then
begin
{Delete the tag in
Temp doc}
loleUnit :=
wdLine;
Selection.EndKey(loleUnit, EmptyParam);
loleUnit :=
wdLine;
loleExtend :=
wdExtend;
Selection.HomeKey(loleUnit, loleExtend);
loleCount := 1;
Selection.MoveUp(loleUnit, loleCount, loleExtend);
Selection.EndKey(loleUnit, loleExtend);
loleUnit :=
wdCharacter;
Selection.Delete(loleUnit, loleCount);
end;
end;
end;
end;
end;
end;
{as there will be section already in the template,
therefore, no need
to insert a new section at this stage. Check if
text exists before XFOCode then paste
it here}
if lTextBeforeTag then
begin
loleUnit := wdLine;
Selection.MoveDown(loleUnit, loleCount,
EmptyParam);
Selection.HomeKey(loleUnit, EmptyParam);
loleUnit := wdStory;
loleExtend := wdExtend;
Selection.HomeKey(loleUnit, loleExtend);
{apply the format obtained from the merge field
from the main doc if applicable}
if loleFontName <> '' then
Selection.Range.Font.Name := loleFontName;
Selection.Font.Size := loleFontSize;
if loleUnderLine <> 0 then
Selection.Range.Underline := loleUnderLine;
if loleBold <> 0 then Selection.Range.Bold :=
loleBold;
if loleColourIndex <> 0 then
...
read more »