NO EXPERIENCE ON MERGE recordset in a Word document
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
NO EXPERIENCE ON MERGE recordset in a Word document
I just have a filled recorset, with:
indirizzo
nome
città
i need to fill a template word, with a fixed filed with:
indirizzo: rs.indirizzo
nome: rs.nome
citta: rs.citta
liitle project?
tks.
note:
the word file is stored in c:\servizo\miodoc.doc.
indirizzo
nome
città
i need to fill a template word, with a fixed filed with:
indirizzo: rs.indirizzo
nome: rs.nome
citta: rs.citta
liitle project?
tks.
note:
the word file is stored in c:\servizo\miodoc.doc.
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
You can use a SQL string for mail merge, but not a recordset.
If you want to use a recordset, you'd have to do all the work in your code instead of letting Word do it. See for example Merging data from Queries and Recordsets to Word, Part 1
If you want to use a recordset, you'd have to do all the work in your code instead of letting Word do it. See for example Merging data from Queries and Recordsets to Word, Part 1
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Ok
But can you upload an example of Word doc with a girls, for a filling data?
In other case the link for download the example you have post, dont work.
But can you upload an example of Word doc with a girls, for a filling data?
In other case the link for download the example you have post, dont work.
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Does your recordset return more than one record?
If so, do you want to fill one document with all records, or do you want a separate document for each record?
If so, do you want to fill one document with all records, or do you want a separate document for each record?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
What should those documents be named?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
With a count....
I=i+1
I=i+1
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Here is an example:
Code: Select all
Sub Test()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim objWrd As Object
Dim objDoc As Object
Dim f As Boolean
Dim i As Long
On Error Resume Next
Set objWrd = GetObject(Class:="Excel.Application")
If objWrd Is Nothing Then
Set objWrd = CreateObject(Class:="Excel.Application")
f = True
End If
On Error GoTo ErrHandler
' Code to open cn and rs goes here
'...
'...
Do While Not rs.EOF
i = i + 1
Set objDoc = objWrd.Documents.Add
objDoc.Content.InsertAfter Text:="Indirizzo " & rs!Indirizzo
objDoc.Content.InsertAfter Text:="Nome " & rs!Nome
objDoc.Content.InsertAfter Text:="Città " & rs!Città
objDoc.SaveAs Filename:="Test" & i & ".docx"
objDoc.Close SaveChanges:=False
rs.MoveNext
Loop
ExitHandler:
rs.Close
cn.Close
On Error Resume Next
If f And Not objWrd Is Nothing Then
objWrd.Quit SaveChanges:=False
End If
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
TKS bro.HansV wrote: ↑09 Nov 2020, 21:11Here is an example:
Code: Select all
Sub Test() Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim objWrd As Object Dim objDoc As Object Dim f As Boolean Dim i As Long On Error Resume Next Set objWrd = GetObject(Class:="Excel.Application") If objWrd Is Nothing Then Set objWrd = CreateObject(Class:="Excel.Application") f = True End If On Error GoTo ErrHandler ' Code to open cn and rs goes here '... '... Do While Not rs.EOF i = i + 1 Set objDoc = objWrd.Documents.Add objDoc.Content.InsertAfter Text:="Indirizzo " & rs!Indirizzo objDoc.Content.InsertAfter Text:="Nome " & rs!Nome objDoc.Content.InsertAfter Text:="Città " & rs!Città objDoc.SaveAs Filename:="Test" & i & ".docx" objDoc.Close SaveChanges:=False rs.MoveNext Loop ExitHandler: rs.Close cn.Close On Error Resume Next If f And Not objWrd Is Nothing Then objWrd.Quit SaveChanges:=False End If Exit Sub ErrHandler: MsgBox Err.Description, vbExclamation Resume ExitHandler End Sub
but:
- why Excel object?
- and i just have a template in c:\servizio\TEST.DOC (see attached file)
- I'm on vb6
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Sorry, my mistake. It should be Class:="Word.Application" both times.
Where is Test.doc?
Where is Test.doc?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Is Indirizzo rs.doc (attached to the first post in this thread) the document that you mean?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
ops....
the real documento contain a different name.
I can send in private?
i need to fill after Sig. the rs.sig
i need to fill after di the rs.nome
i need to fill after Via the rs.indirizzo
the part are red in the doc.
the real documento contain a different name.
I can send in private?
i need to fill after Sig. the rs.sig
i need to fill after di the rs.nome
i need to fill after Via the rs.indirizzo
the part are red in the doc.
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
You know my email address...
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Try this:
Code: Select all
Sub Test()
Const strPath = "C:\Servizio\"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim objWrd As Object
Dim objDoc As Object
Dim f As Boolean
Dim i As Long
On Error Resume Next
Set objWrd = GetObject(Class:="Word.Application")
If objWrd Is Nothing Then
Set objWrd = CreateObject(Class:="Word.Application")
f = True
End If
On Error GoTo ErrHandler
' Code to open cn and rs goes here
'...
'...
Do While Not rs.EOF
i = i + 1
Set objDoc = objWrd.Documents.Open(strPath & "Test.doc")
With objWrd.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Execute FindText:="-{1,}", ReplaceWith:=rs!Sig, Replace:=1
objWrd.Selection.Collapse Direction:=0
.Execute FindText:="-{1,}", ReplaceWith:=rs!Nome, Replace:=1
objWrd.Selection.Collapse Direction:=0
.Execute FindText:="-{1,}", ReplaceWith:=rs!Indirizzo, Replace:=1
objWrd.Selection.Collapse Direction:=0
End With
objDoc.SaveAs Filename:=strPath & "Test" & i & ".docx"
objDoc.Close SaveChanges:=False
rs.MoveNext
Loop
ExitHandler:
rs.Close
cn.Close
On Error Resume Next
If f And Not objWrd Is Nothing Then
objWrd.Quit SaveChanges:=False
End If
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
I HAVE NO WORDS!HansV wrote: ↑10 Nov 2020, 11:35Try this:
Code: Select all
Sub Test() Const strPath = "C:\Servizio\" Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim objWrd As Object Dim objDoc As Object Dim f As Boolean Dim i As Long On Error Resume Next Set objWrd = GetObject(Class:="Word.Application") If objWrd Is Nothing Then Set objWrd = CreateObject(Class:="Word.Application") f = True End If On Error GoTo ErrHandler ' Code to open cn and rs goes here '... '... Do While Not rs.EOF i = i + 1 Set objDoc = objWrd.Documents.Open(strPath & "Test.doc") With objWrd.Selection.Find .ClearFormatting .Replacement.ClearFormatting .MatchWildcards = True .Execute FindText:="-{1,}", ReplaceWith:=rs!Sig, Replace:=1 objWrd.Selection.Collapse Direction:=0 .Execute FindText:="-{1,}", ReplaceWith:=rs!Nome, Replace:=1 objWrd.Selection.Collapse Direction:=0 .Execute FindText:="-{1,}", ReplaceWith:=rs!Indirizzo, Replace:=1 objWrd.Selection.Collapse Direction:=0 End With objDoc.SaveAs Filename:=strPath & "Test" & i & ".docx" objDoc.Close SaveChanges:=False rs.MoveNext Loop ExitHandler: rs.Close cn.Close On Error Resume Next If f And Not objWrd Is Nothing Then objWrd.Quit SaveChanges:=False End If Exit Sub ErrHandler: MsgBox Err.Description, vbExclamation Resume ExitHandler End Sub
tKS.
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Mummmm.
Before to save, Is possibile to set A4 Page and print the doc to the default printer?
Before to save, Is possibile to set A4 Page and print the doc to the default printer?
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: NO EXPERIENCE ON MERGE recordset in a Word document
Insert the following lines below the line End With:
Code: Select all
objDoc.PageSetup.PaperSize = 7
objDoc.PrintOut
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: NO EXPERIENCE ON MERGE recordset in a Word document
tKS!HansV wrote: ↑10 Nov 2020, 13:56Insert the following lines below the line End With:
Code: Select all
objDoc.PageSetup.PaperSize = 7 objDoc.PrintOut
But possible to everwrite the .doc with the same name, without windows allert?