AutoText and Image question (Report for Database)

R

Robertico

Let me explain what i'am doing.

I'd like to use Microsoft Word as a report generator for my database.
(I created a VB6 application that creates and has access to a Microsoft
Access database )
One of my fields contains links to photos (full path). I want to print/save
every record on a new page.
(One photo (with some text) per page in one single document)

I created a Ms Word template with bookmarks (within cells) for my fields and
has access from my VB application to this document.
I create a new page for every new record from my database and use AutoText
to create new Bookmarks.

First question:
If Ms Word is already active, the AutoText fails (Error numer 5825, The
object is removed (translated from Dutch))
What can/must i do to prevent this ? (This error only occurs when Ms Word is
already active)

Second question:
Is it possible to define the photo size in my template. (I has to resize the
original photo to approximately 13 x 17 cm)
When the original photo is 2272x1704 everything goes well. The photo fills
the cell. (about 13x17 cm)
But when the original photo is 800x600, it doesn't fill the cell)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Some code i'am using:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim WordRange As Word.Range
Set WordObj = CreateObject("Word.Application")
Set WordDoc = WordObj.Documents.Add(App.Path & "\report.dot")
WordObj.Visible = False

' The code below will place the text/photo in the bookmarked location
'DESCRIPTION
Set WordRange = WordDoc.GoTo(What:=wdGoToBookmark, Name:="description")
WordRange.InsertAfter rs!description

'PHOTO
Set WordRange = WordDoc.GoTo(What:=wdGoToBookmark, Name:="photo")
WordRange.InlineShapes.AddPicture Filename:=rs!filename,
LinkToFile:=False, SaveWithDocument:=True

' The code below will go to end of page, create a pagebreak and insert
the AutoText
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak

WordDoc.AttachedTemplate.AutoTextEntries("MyAutoText").Insert
Where:=Selection.Range, RichText:=True


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thanks in advance,

Robertico
 
J

Jonathan West

Robertico said:
Let me explain what i'am doing.

I'd like to use Microsoft Word as a report generator for my database.
(I created a VB6 application that creates and has access to a Microsoft
Access database )
One of my fields contains links to photos (full path). I want to
print/save every record on a new page.
(One photo (with some text) per page in one single document)

I created a Ms Word template with bookmarks (within cells) for my fields
and has access from my VB application to this document.
I create a new page for every new record from my database and use AutoText
to create new Bookmarks.

First question:
If Ms Word is already active, the AutoText fails (Error numer 5825, The
object is removed (translated from Dutch))
What can/must i do to prevent this ? (This error only occurs when Ms Word
is already active)

On which line does this error occur?
Second question:
Is it possible to define the photo size in my template. (I has to resize
the original photo to approximately 13 x 17 cm)
When the original photo is 2272x1704 everything goes well. The photo fills
the cell. (about 13x17 cm)
But when the original photo is 800x600, it doesn't fill the cell)

Yes. What you need to do is assign the InlineShape to an object variable,
like this


Dim iShape as InlineShape

Set oShape = WordRange.InlineShapes.AddPicture(Filename:=rs!filename, _
LinkToFile:=False, SaveWithDocument:=True

You can then check the Width property of oShape, and increase it if
necessary. The width property is expressed in points (1/72 of an inch) You
can convert between points and centimeters using the PointsToCentimeters and
CentimetersToPoints functions


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
R

Robertico

On which line does this error occur?

When executing this line:
WordDoc.AttachedTemplate.AutoTextEntries("MyAutoText").Insert
Where:=Selection.Range, RichText:=True

( Error message: Run-time error '5825':, The object is removed.)
(translated from Dutch)
This error only occurs when another instance of Ms Word is already active.
....What you need to do is assign the InlineShape to an object variable,
like this
Dim iShape as InlineShape

Set oShape = WordRange.InlineShapes.AddPicture(Filename:=rs!filename, _
LinkToFile:=False, SaveWithDocument:=True

I give it a try later this day. Let you know if it works.
(BTW you make a typo with iShape and oShape i think)

Thanks in advance,

Robertico
 
R

Robertico

In addition to my previous post:
- - - - - - - - - - - - - - - - - - - -
Dim iShape as InlineShape

Set oShape = WordRange.InlineShapes.AddPicture(Filename:=rs!filename, _
LinkToFile:=False, SaveWithDocument:=True

I was very curious, so i took some time to test your code.
I got the actual width and height from the image in centimeters.

Height = 12,88521 and Width = 17,17146 cm
Height = 8,466666 and Width = 11,29771 cm (600x800) photo)
Height = 12,88521 and Width = 17,17146 cm
Height = 12,88521 and Width = 17,17146 cm

But how can i increase the size of the smaller image ?

Regards,

Robertico
 
R

Robertico

But how can i increase the size of the smaller image ?

I found an example to do this. So this problem is solved.
Please feel free to correct me if i'am wrong or you have a better solution.

Set iShape = WordRange.InlineShapes.AddPicture
(Filename:=rs!filename,LinkToFile:=False, SaveWithDocument:=True)

With iShape
.Height = 365.25
.Width = 486.75
End With

In addition to my first question:
First question:
If Ms Word is already active, the AutoText fails (Error numer 5825, The
object is removed (translated from Dutch))
What can/must i do to prevent this ? (This error only occurs when Ms Word
is already active)

I discovered that after there's a new document created and the Bookmarks on
the templates first page are filled,
the already opened document apparently become the activedocument and then
the code;

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak

is executed on that document and not on my new created document. (Created a
new page at the already opened document !)

Although when i 'ask' for the active document 'WordObj.ActiveDocument', it
returns the name of my new created document.
I already tried to use: 'WordDoc.Activate' before this code, but that
doesn't solve the problem.
When i excute a macro with the same code within my new created document and
another document already opened, no error occurs.

I have no idea how to solve this problem. If i can fix this, i can finish my
application.

Regards,

Robertico
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top