VB 6 and MS-Word question

R

Robert

Hi,

I need some hints for the following problem.

I have a VB 6 application that needs to write data (text and image) to a
Ms-Word document (template)

So I created a Ms-Word document with some bookmarks. The VB App is able to
write the data to the Ms-Word document and save it with a new doc-name.

But.this works only for one page. How to deal with a multiple page document.
Beforehand I don't know how much pages I need. One page contains a few text
lines and one image.

Can I define the image size ?

A piece of my (test)code.

' Project References = Microsoft Word 11.0 Object Library

Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim WordRange As Word.Range
mydate$ = Date
Set WordObj = CreateObject("Word.Application")
Set WordDoc = WordObj.Documents.Open(App.Path & "\report.doc")
WordObj.Visible = True

'Date

Set WordRange = WordDoc.GoTo(What:=wdGoToBookmark, Name:="Date")
WordRange.InsertAfter mydate$ 'inserts the contents

'Photo

Set WordRange = WordDoc.GoTo(What:=wdGoToBookmark, Name:="photo")
WordRange.InlineShapes.AddPicture FileName:= myfile$, LinkToFile _
:=False, SaveWithDocument:=True
 
J

Jezebel

The number of pages is irrelevant to the code you've posted. What's the
problem?

To insert data (other than pictures) it is easier to use DocumentProperties
and DocProperty fields than to use bookmarks.

Avoid selecting anything when you are working with a document remotely. Work
directly with the ranges involved --

WordDoc.Bookmarks("photo").Range.InlineShapes.AddPicture FileName:= myfile$,
LinkToFile:=False, SaveWithDocument:=True

Yes, you can resize the picture --

With WordDoc.Bookmarks("photo").Range.InlineShapes.AddPicture (...)
.Height = ...
.WIdth = ...
etc
End With


If you have a reference to a specific Word library in your app (such as Word
11), don't use CreateObject to instantiate your reference -- that will use
the registered version of Word on the computer, which is not necessarily the
same as your library version. Use --

Set WordObj = new Word.Application
 
R

Robert

Jezebel,
To insert data (other than pictures) it is easier to use
DocumentProperties and DocProperty fields than to use bookmarks.

I'am not familiar with DocumentProperties and DocProperty fields. How can i
use them, like bookmarks ?.
The user needs to create his own template (according to the rules e.g.
bookmarks) so i must be flexible.
Can you explain this a little more for me ?
WordDoc.Bookmarks("photo").Range.InlineShapes.AddPicture FileName:=
myfile$, LinkToFile:=False, SaveWithDocument:=True
With WordDoc.Bookmarks("photo").Range.InlineShapes.AddPicture (...)
.Height = ...
.WIdth = ...
etc
End With
If you have a reference to a specific Word library in your app (such as
Word 11), don't use CreateObject to instantiate your reference -- that
will use the registered version of Word on the computer, which is not
necessarily the same as your library version. Use --

Set WordObj = new Word.Application

Very useful hints ! thank you.

Robert
 
J

Jezebel

Document properties can be created set by the user on the File > Properties
dialog. The Summary tab shows Built-in properties; the Custom tab shows any
Custom properties. You can also create and set these through code --

ActiveDocument.CustomDocumentProperties("DocDate") = "28 May 2005"

You display the value in the document by insert a field, as for any other
kind of field (ie, press Ctrl-F9) --

{ DocProperty DocDate }
 
R

Robert

Jezebel,

I now understand what you mean with DocumentProperties and DocProperty
fields.
But i need a template to add text (not only a date and it's not the
documents date) and an image.
Beforehand the user can create it's own template lay-out.
So i (my application) need a reference to the precise location within the
document to write the text and image.
So why using DocumentProperties and DocProperty and insert a field in the
document.
Using bookmarks works fine, but my problem is that when i create a second or
third or ... page i have no bookmarks for that page.
How can i get new bookmarks with the same lay-out as the first page. I was
thinking about using AutoText.

Robert
 
J

Jezebel

I used document date only as an example. Property fields will work with any
text or data. And unlike Bookmarks, you can use the same property more than
once, while your bookmark names have to be unique.

Beyond that, I actually don't understand what you're trying to do. Who is
creating the additional pages? -- Your VB application, the document user, or
the template creator?
 
R

Robert

Beyond that, I actually don't understand what you're trying to do. Who is
creating the additional pages? -- Your VB application, the document user,
or the template creator?

There is a VB application and there is a Microsoft Word document. The VB
application uses the Word document as a report.
So the user of my application can create his/her own Word document
(template) and can choose every lay-out they wish as long as they insert the
obligatory fields.
My application uses this Word document (template), fills in the fields and
saves it with a new documentname.
But beforehand it's unknown how much pages the application needs. The
Microsoft Word document (template) contains only one page with the
obligatory fields.
So if the fields are filled, how to create the next page with the same
lay-out, .... read obligatory fields.

I hope you understand what i intend. My excuses but English is not my native
language.

Robert
 
I

Ian B

Where would we all be without these groups and the contributors?

I have worked programmatically with Word from Word 2 macros through to today
with Word 2003 VBA and VB.
I never knew until this post from "Jezebel" that there was a field which I
could use to insert data into a document from Document properties.
The hours I could have saved.
Thanks Jezebel and your fellow contributors!

Ian B
 

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