Exportar para o word delphi 7
Boa tarde! tenho o seguinte o código:
procedure TFrmPrincipal.Button1Click(Sender: TObject);
var
MsWord : Variant;
linha, i : integer;
Campos : TStringList;
S1, S2, S3, S4, S5, S6, STitulo: string;
begin
inherited;
Campos := TStringList.Create; // cria a lista de campos
ADOQuery1.Filtered := False;
Linha := 2;
{ Abre o Word }
MsWord := CreateOleObject('Word.Application');
MsWord.caption:= 'Exportação de dados para o Word';
MsWord.visible:= true;
ADOQuery1.First;
try
{ Novo documento }
MsWord.Documents.Add;
with TFrmPrincipal.Create(nil) do //Para criar um form para mostrar o progresso
try
Bar2.Max := ADOQuery1.RecordCount;
Show; // show a splash screen contain ProgressBar control
Update; // force display of Form5
ADOQuery1.First;
STitulo:= STitulo + 'EFETIVO DO CGF';
MsWord.Selection.TypeText(Text:= STitulo);
MsWord.Selection.TypeParagraph;
while not ADOQuery1.Eof do
begin
MsWord.Selection.TypeParagraph;
S1:= 'GRAD: ' + ADOQuery1.fieldbyname('GRAD').AsString;
S2:= 'RG: ' + ADOQuery1.fieldbyname('RG').AsString;
S3:= 'NOME: ' + ADOQuery1.fieldbyname('NOME').AsString;
S4:= 'FUNCAO: ' + ADOQuery1.fieldbyname('FUNCAO').AsString;
S5:= 'SECAO: ' + ADOQuery1.fieldbyname('SECAO').AsString;
S6:= 'SITUACAO: ' + ADOQuery1.fieldbyname('SITUACAO').AsString;
MsWord.Selection.TypeText(Text:= S1);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S2);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S3);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S4);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S5);
MsWord.Selection.TypeParagraph;
ADOQuery1.Next;
Bar2.StepBy(1);
end;
finally
Free;
end;
finally
end;
end;
So que eu quero que saia no word a tabela em grade com os campos em cima.
Com o código acima o texto não sai com grade organizado por campos/colunas.
procedure TFrmPrincipal.Button1Click(Sender: TObject);
var
MsWord : Variant;
linha, i : integer;
Campos : TStringList;
S1, S2, S3, S4, S5, S6, STitulo: string;
begin
inherited;
Campos := TStringList.Create; // cria a lista de campos
ADOQuery1.Filtered := False;
Linha := 2;
{ Abre o Word }
MsWord := CreateOleObject('Word.Application');
MsWord.caption:= 'Exportação de dados para o Word';
MsWord.visible:= true;
ADOQuery1.First;
try
{ Novo documento }
MsWord.Documents.Add;
with TFrmPrincipal.Create(nil) do //Para criar um form para mostrar o progresso
try
Bar2.Max := ADOQuery1.RecordCount;
Show; // show a splash screen contain ProgressBar control
Update; // force display of Form5
ADOQuery1.First;
STitulo:= STitulo + 'EFETIVO DO CGF';
MsWord.Selection.TypeText(Text:= STitulo);
MsWord.Selection.TypeParagraph;
while not ADOQuery1.Eof do
begin
MsWord.Selection.TypeParagraph;
S1:= 'GRAD: ' + ADOQuery1.fieldbyname('GRAD').AsString;
S2:= 'RG: ' + ADOQuery1.fieldbyname('RG').AsString;
S3:= 'NOME: ' + ADOQuery1.fieldbyname('NOME').AsString;
S4:= 'FUNCAO: ' + ADOQuery1.fieldbyname('FUNCAO').AsString;
S5:= 'SECAO: ' + ADOQuery1.fieldbyname('SECAO').AsString;
S6:= 'SITUACAO: ' + ADOQuery1.fieldbyname('SITUACAO').AsString;
MsWord.Selection.TypeText(Text:= S1);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S2);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S3);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S4);
MsWord.Selection.TypeParagraph;
MsWord.Selection.TypeText(Text:= S5);
MsWord.Selection.TypeParagraph;
ADOQuery1.Next;
Bar2.StepBy(1);
end;
finally
Free;
end;
finally
end;
end;
So que eu quero que saia no word a tabela em grade com os campos em cima.
Com o código acima o texto não sai com grade organizado por campos/colunas.
Paulo
Curtidas 0
Respostas
Marcos Saffran
28/01/2015
Olá Paulo, tente com as mudanças feitas abaixo:
acrescente Word97 na uses.
try
{ Novo documento }
MsWord.Documents.Add;
with TFrmPrincipal.Create(nil) do //Para criar um form para mostrar o progresso
try
Bar2.Max := ADOQuery1.RecordCount;
Show; // show a splash screen contain ProgressBar control
Update; // force display of Form5
ADOQuery1.First;
STitulo:= STitulo + 'EFETIVO DO CGF';
MsWord.Selection.TypeText(Text:= STitulo);
MsWord.Selection.TypeParagraph;
{ Adiciona tabela de 1 linhas e 6 colunas }
MsWord.ActiveDocument.Tables.Add(
Range := Word.Selection.Range,
NumRows := 1,
NumColumns := 6);
while not ADOQuery1.Eof do
begin
S1:= 'GRAD: ' + ADOQuery1.fieldbyname('GRAD').AsString;
S2:= 'RG: ' + ADOQuery1.fieldbyname('RG').AsString;
S3:= 'NOME: ' + ADOQuery1.fieldbyname('NOME').AsString;
S4:= 'FUNCAO: ' + ADOQuery1.fieldbyname('FUNCAO').AsString;
S5:= 'SECAO: ' + ADOQuery1.fieldbyname('SECAO').AsString;
S6:= 'SITUACAO: ' + ADOQuery1.fieldbyname('SITUACAO').AsString;
MsWord.Selection.TypeText(Text:= S1);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S2);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S3);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S4);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S5);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S6);
MsWord.Selection.MoveRight(wdCell);
ADOQuery1.Next;
Bar2.StepBy(1);
end;
finally
Free;
end;
acrescente Word97 na uses.
try
{ Novo documento }
MsWord.Documents.Add;
with TFrmPrincipal.Create(nil) do //Para criar um form para mostrar o progresso
try
Bar2.Max := ADOQuery1.RecordCount;
Show; // show a splash screen contain ProgressBar control
Update; // force display of Form5
ADOQuery1.First;
STitulo:= STitulo + 'EFETIVO DO CGF';
MsWord.Selection.TypeText(Text:= STitulo);
MsWord.Selection.TypeParagraph;
{ Adiciona tabela de 1 linhas e 6 colunas }
MsWord.ActiveDocument.Tables.Add(
Range := Word.Selection.Range,
NumRows := 1,
NumColumns := 6);
while not ADOQuery1.Eof do
begin
S1:= 'GRAD: ' + ADOQuery1.fieldbyname('GRAD').AsString;
S2:= 'RG: ' + ADOQuery1.fieldbyname('RG').AsString;
S3:= 'NOME: ' + ADOQuery1.fieldbyname('NOME').AsString;
S4:= 'FUNCAO: ' + ADOQuery1.fieldbyname('FUNCAO').AsString;
S5:= 'SECAO: ' + ADOQuery1.fieldbyname('SECAO').AsString;
S6:= 'SITUACAO: ' + ADOQuery1.fieldbyname('SITUACAO').AsString;
MsWord.Selection.TypeText(Text:= S1);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S2);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S3);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S4);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S5);
MsWord.Selection.MoveRight(wdCell);
MsWord.Selection.TypeText(Text:= S6);
MsWord.Selection.MoveRight(wdCell);
ADOQuery1.Next;
Bar2.StepBy(1);
end;
finally
Free;
end;
GOSTEI 0
Paulo
28/01/2015
[img]http://arquivo.devmedia.com.br/forum/imagem/311129-20150210-121314.jpg[/img]
GOSTEI 0
Marcos Saffran
28/01/2015
Olá Paulo,
eu tenho um programa que insere os valores em uma tabela já existente no arquivo do word, no seu caso seria colocar na linha 2 da tabela o texto <%NOME%>.
então, minha dica abaixo só funcionará se o arquivo do word já existir, com a tabela com 6 colunas e 2 linhas.
Antes do loop (while) você coloca os comandos abaixo:
MSWord.Application.Selection.Find.ClearFormatting;
{ Adiciono no find o parametro que quero buscar dentro do word,
e abaixo esta selecionado as condições }
MSWord.Application.Selection.Find.Text := '<%NOME%>';
MSWord.Application.Selection.Find.Replacement.Text := '';
MSWord.Application.Selection.Find.Forward := true;
MSWord.Application.Selection.Find.Format := False;
MSWord.Application.Selection.Find.MatchCase := False;
MSWord.Application.Selection.Find.MatchWholeWord := False;
MSWord.Application.Selection.Find.MatchWildcards := False;
MSWord.Application.Selection.Find.MatchSoundsLike := False;
MSWord.Application.Selection.Find.MatchAllWordForms := False;
// Executo o pesquisa, o parametro se estiver no texto vai ficar selecionado
MSWord.Application.Selection.Find.Execute;
Controle := 1;
e dentro do while coloque isso, considerando que foi criada a tabela com 6 colunas:
while not ADOQuery1.Eof do
begin
S1:= ADOQuery1.fieldbyname('GRAD').AsString;
S2:= ADOQuery1.fieldbyname('RG').AsString;
S3:= ADOQuery1.fieldbyname('NOME').AsString;
S4:= ADOQuery1.fieldbyname('FUNCAO').AsString;
S5:= ADOQuery1.fieldbyname('SECAO').AsString;
S6:= ADOQuery1.fieldbyname('SITUACAO').AsString;
if controle := 1 then
begin
controle := controle + 1;
end
else
begin
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
end;
MSWord.Application.Selection.TypeText(S1);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S2);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S3);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S4);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S5);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S6);
ADOQuery1.Next;
Bar2.StepBy(1);
end
eu tenho um programa que insere os valores em uma tabela já existente no arquivo do word, no seu caso seria colocar na linha 2 da tabela o texto <%NOME%>.
então, minha dica abaixo só funcionará se o arquivo do word já existir, com a tabela com 6 colunas e 2 linhas.
Antes do loop (while) você coloca os comandos abaixo:
MSWord.Application.Selection.Find.ClearFormatting;
{ Adiciono no find o parametro que quero buscar dentro do word,
e abaixo esta selecionado as condições }
MSWord.Application.Selection.Find.Text := '<%NOME%>';
MSWord.Application.Selection.Find.Replacement.Text := '';
MSWord.Application.Selection.Find.Forward := true;
MSWord.Application.Selection.Find.Format := False;
MSWord.Application.Selection.Find.MatchCase := False;
MSWord.Application.Selection.Find.MatchWholeWord := False;
MSWord.Application.Selection.Find.MatchWildcards := False;
MSWord.Application.Selection.Find.MatchSoundsLike := False;
MSWord.Application.Selection.Find.MatchAllWordForms := False;
// Executo o pesquisa, o parametro se estiver no texto vai ficar selecionado
MSWord.Application.Selection.Find.Execute;
Controle := 1;
e dentro do while coloque isso, considerando que foi criada a tabela com 6 colunas:
while not ADOQuery1.Eof do
begin
S1:= ADOQuery1.fieldbyname('GRAD').AsString;
S2:= ADOQuery1.fieldbyname('RG').AsString;
S3:= ADOQuery1.fieldbyname('NOME').AsString;
S4:= ADOQuery1.fieldbyname('FUNCAO').AsString;
S5:= ADOQuery1.fieldbyname('SECAO').AsString;
S6:= ADOQuery1.fieldbyname('SITUACAO').AsString;
if controle := 1 then
begin
controle := controle + 1;
end
else
begin
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
end;
MSWord.Application.Selection.TypeText(S1);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S2);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S3);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S4);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S5);
MSWord.Application.Selection.SelectCell;
MSWord.Selection.MoveRight(wdCell);
MSWord.Application.Selection.TypeText(S6);
ADOQuery1.Next;
Bar2.StepBy(1);
end
GOSTEI 0
Paulo
28/01/2015
Deu o seguinte erro:
[img]http://arquivo.devmedia.com.br/forum/imagem/311129-20150212-141644.png[/img]
[img]http://arquivo.devmedia.com.br/forum/imagem/311129-20150212-141644.png[/img]
GOSTEI 0
Marcos Saffran
28/01/2015
Olá Paulo,
você deve declarar a variável controle, na declaração de variáveis (var) de seu procedimento inclua:
controle : Integer;
você deve declarar a variável controle, na declaração de variáveis (var) de seu procedimento inclua:
controle : Integer;
GOSTEI 0
Paulo
28/01/2015
Deu o seguinte erro:
construir
[ERRO] UPrincipal.pas (407): Tipo de expressão deve ser BOOLEAN
[Erro] UPrincipal.pas (438): Declaração esperado, mas «Processo» encontrado
[Fatal Error] Crh1.dpr (13): Não foi possível compilar unidade usada 'UPrincipal.pas'
construir
[ERRO] UPrincipal.pas (407): Tipo de expressão deve ser BOOLEAN
[Erro] UPrincipal.pas (438): Declaração esperado, mas «Processo» encontrado
[Fatal Error] Crh1.dpr (13): Não foi possível compilar unidade usada 'UPrincipal.pas'
GOSTEI 0
Marcos Saffran
28/01/2015
Olá Paulo,
vi no seu print screen que tem dois 'begin' depois do while, verifique se é isso.
vi no seu print screen que tem dois 'begin' depois do while, verifique se é isso.
GOSTEI 0