HOW to insert data values in word document

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

HOW to insert data values in word document

Post by sal21 »

How to insert in this doc the value from Access recordset ?
No idea!
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

First, open the document in Word.
Insert a bookmark in each location of the document where you want to place a value from your recordset.
Give each bookmark a clear and meaningful name, for example Destinatario and DataFattura.
Save the document.

In your VB6 project, use Automation to start Word, open the document, set the values of the bookmarks, then save the document under another name. Quit Word when you have processed all records.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
24 Apr 2022, 08:28
First, open the document in Word.
Insert a bookmark in each location of the document where you want to place a value from your recordset.
Give each bookmark a clear and meaningful name, for example Destinatario and DataFattura.
Save the document.

In your VB6 project, use Automation to start Word, open the document, set the values of the bookmarks, then save the document under another name. Quit Word when you have processed all records.
FIRST TET, dont work!

Code: Select all

Private Sub FATTURA()

    Dim APP As Object
    Dim DOC As Object
    Set APP = CreateObject("WORD.Application")
    APP.Visible = True

    Set DOC = APP.documents.Open("C:\Lavori_Vb6\HOTEL\Fattura.doc")

    DOC.Bookmarks("IMMAGINE").Select

    DOC.Selection.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False
    
    DOC.booksmarks("NOME_AZIENDA").Range.Text = "TEST"
    DOC.printout
    DOC.Close
    Set DOC = Nothing

    APP.quit
    Set APP = Nothing

End Sub

i need t insert an image in bokkmark, is this correct?

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

Where does it fail? And what is the error message?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
24 Apr 2022, 20:59
Where does it fail? And what is the error message?
Error In:

DOC.Selection.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

Change DOC.Selection to APP.Selection
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

Or change

Code: Select all

   DOC.Bookmarks("IMMAGINE").Select

    DOC.Selection.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False
to

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False, Range:=DOC.Bookmarks("IMMAGINE").Range
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
25 Apr 2022, 07:25
Or change

Code: Select all

   DOC.Bookmarks("IMMAGINE").Select

    DOC.Selection.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False
to

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False, Range:=DOC.Bookmarks("IMMAGINE").Range
i test this after..

but see the code write TEST! instead in the bookmark NOME_AZIENDA!!!!

the code:
....

Dim APP As Object
Dim DOC As Object
Set APP = CreateObject("WORD.Application")

Set DOC = APP.documents.Open("C:\Lavori_Vb6\HOTEL\Fattura.doc")

With DOC.Bookmarks
.Item("NOME_AZIENDA").Range.Text = "TEST"
'.Item("ADDRESS").Range.Text = ADDRESS
'.Item("POSTCODE").Range.Text = POSTCODE
'.Item("DESCRIPTION").Range.Text = Description
'.Item("COST").Range.Text = COST
'.Item("GROSS").Range.Text = GROSS
'.Item("VAT").Range.Text = VAT
'.Item("DEPOSIT").Range.Text = DEPOSIT
'.Item("NET").Range.Text = NET
End With
.....

peraphs i need to select the bookmark before .Item("NOME_AZIENDA").Range.Text = "TEST"?
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

Are you sure that you created bookmarks? They look like hyperlinks to me...
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
25 Apr 2022, 08:03
Are you sure that you created bookmarks? They look like hyperlinks to me...
infact!

can you create for me (in the attached doc, in first post) two or tree correct bookmark, named TEST1, TEST2, ...
Tks

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

Here you go.

S1344.png
Fattura.doc
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
25 Apr 2022, 08:28
Here you go.


S1344.png


Fattura.doc
Tks bro!

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
25 Apr 2022, 07:25
Or change

Code: Select all

   DOC.Bookmarks("IMMAGINE").Select

    DOC.Selection.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False
to

HERE error 4120 parametro errato

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False, Range:=DOC.Bookmarks("IMMAGINE").Range

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

sal21 wrote:
25 Apr 2022, 20:34
HansV wrote:
25 Apr 2022, 07:25
Or change

Code: Select all

   DOC.Bookmarks("IMMAGINE").Select

    DOC.Selection.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False
to

HERE error 4120 parametro errato

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=False, Range:=DOC.Bookmarks("IMMAGINE").Range
In this case peraphas...
Other way are welcome to insert a Little image.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

I should have seen that. If LinkToFile is False, SaveWithDocument should be True:

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=True, Range:=DOC.Bookmarks("IMMAGINE").Range
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
25 Apr 2022, 20:44
I should have seen that. If LinkToFile is False, SaveWithDocument should be True:

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=True, Range:=DOC.Bookmarks("IMMAGINE").Range
super!
Work now!
Tks bro.

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: HOW to insert data values in word document

Post by sal21 »

HansV wrote:
25 Apr 2022, 20:44
I should have seen that. If LinkToFile is False, SaveWithDocument should be True:

Code: Select all

    DOC.InlineShapes.AddPicture FileName:="C:\Lavori_Vb6\HOTEL\IMG\RISTORANTE.bmp", LinkToFile:=False, SaveWithDocument:=True, Range:=DOC.Bookmarks("IMMAGINE").Range
OK.ALL TESTED.

Now, how to save as pdf file the doc filled with new value, and close the doc without save it.

Note:
save as the pdf in c:\mydir\ with a name based the string var named VALORE

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to insert data values in word document

Post by HansV »

Use

Code: Select all

    DOC.ExportAsFixedFormat OutputFileName:="c:\mydir\" & VALORE & ".pdf", ExportFormat:=17
    DOC.Close SaveChanges:=False
Best wishes,
Hans