Sin embargo, si en vez de usar MailMerge, uso DOCVARIABLE, para el botón "IMPRIMIR DOCUMENTOS" el resultado es el contrario. Cuando quiero que mi fuente de datos tenga 100 registros, me genera un documento word con la primera página escrita (correcta) y 99 páginas en blanco. Uso este código:
Código Delphi
[-]
Word := CreateOleObject('Word.Application');
Word.Documents.Add(FormBuscaFicheros.FoundFiles.ItemFocused.Caption);
Documento := Word.Documents.Item(1);
Socios.First;
While not Socios.Eof do
begin
I := IndexOfName(Documento.Variables, 'Nombre');
if I = 0 then Documento.Variables.Add('Nombre', ImpresionWord.FieldByName('Nombre').AsString)
else Documento.Variables.Item(I).Value := ImpresionWord.FieldByName('Nombre').AsString;
I := IndexOfName(Documento.Variables, 'Direccion');
if I = 0 then Documento.Variables.Add('Direccion', ImpresionWord.FieldByName('Direccion').AsString)
else Documento.Variables.Item(I).Value := ImpresionWord.FieldByName('Direccion').AsString;
Documento.Fields.Update;
Salto := wdSectionBreakNextPage;
Word.ActiveDocument.Content.Paragraphs.Last.Range.InsertBreak(salto);
Socios.Next;
end;
Word.Visible := true;
Fecha := Copy(DateToStr(Date),1,2) + Copy(DateToStr(Date),4,2) + Copy(DateToStr(Date),7,4);
Hora := Copy(TimeToStr(Time),1,2) + Copy(TimeToStr(Time),4,2) + Copy(TimeToStr(Time),7,4);
Word.ActiveDocument.SaveAs(ExtractFilePath(Application.ExeName)+'Documentos\'+'Envio Documentacion Fecha ' + Fecha + ' Hora '+ Hora + '.doc');
Word.Quit(EmptyParam, EmptyParam, EmptyParam);
Para el botón "MANDAR CORREOS" utilizo el siguiente código que SI lo hace correctamente:
Código Delphi
[-]
Socios.First;
While not Socios.Eof do
begin
Word := CreateOleObject('Word.Application');
Word.Documents.Add(FormBuscaFicheros.FoundFiles.ItemFocused.Caption);
Documento := Word.Documents.Item(1);
Documento.Variables.Add('Nombre', ImpresionWord.FieldByName('Nombre').AsString);
Documento.Variables.Add('Direccion', ImpresionWord.FieldByName('Direccion').AsString);
Documento.Fields.Update;
Word.ActiveDocument.SaveAs(ExtractFilePath(Application.ExeName)+'Documentos\'+'Carta al Socio ' + ImpresionWord.FieldByName('Nombre').AsString + '.doc');
Word.Quit(EmptyParam, EmptyParam, EmptyParam);
Socios.Next;
end;