Solved

Writing to a Word Document from VB using Print # command

Posted on 2001-08-03
13
270 Views
Last Modified: 2012-05-07
I have designed a Word document that resembles an application form. It has been saved as a .doc file. I now need to be able to print information onto this document from within VB (data coming from SQL tables using stored procedures) in the correct positions but seem to be coming up against a brick wall. If I open the file (using the syntax Open "C:\TESTFILE.DOC" For Output As #1) as output, when I print something to the file it overwrites the contents of the file. If I open the file for append
(syntax Open "C:\TESTFILE.DOC" For Append As #1) it doesn't write anything to the file e.g. the labels, tables etc on the document are still there but I have noticed that the size of the file has actually increased.

Am I doing this the completely wrong way.
0
Comment
Question by:jkeogh
  • 4
  • 2
  • 2
  • +4
13 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6348925
i'd suggest using the word object library.

under project/references: ms word object library.

then do a mail merge (you have to set up the word document with the merge fields first)

here's some code to get you started (not tested)

Dim myDoc As Word.Document
Dim appWD As New Word.Application

appWD.Visible = True
appWD.WindowState = wdWindowStateMaximize
Set myDoc = appWD.Documents.Add
    ActiveDocument.MailMerge.OpenDataSource Name:= _
        "C:\test.txt", ConfirmConversions:=False, ReadOnly:=False _
        , LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", WritePasswordDocument:="", WritePasswordTemplate:= _
        "", Revert:=False, Format:=wdOpenFormatAuto, Connection:="", SQLStatement _
        :="", SQLStatement1:=""
etc....
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6348931
Yes.

You need to open the file with word and use the OLE functions to copy the data. What you are doing here, is to open it using the DOS filing system.

You now have start looking at the Word object model to see what you can do

To point you in the write direction(the Word object model is far to complex to describe in detail)...

Set a reference to Word in your VB project

Private WordApp As Word.Application
Private Sub Command1_Click()
   Set WordApp = New Word.Application
   WordApp.Visible = True
   WordApp.Documents.Add C:\TESTFILE.DOC
...
...
...
...
...
  WordApp.Close
  Set WordApp = Nothing
End Sub
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6348939
Note: word MUST be installed on the machine to use the above code.

Other alternatives include:
use a datareport.
use richtextbox control.

personally, i'd probably use the datareport.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6348955
Hi,

     Why don't u try the following command

     set word1=new Word.Application
     word1.Documents.Open("TESTFILE.DOC").Protect(.......)

     I hope this would work....

:-)

bye

Mohan
0
 

Author Comment

by:jkeogh
ID: 6349059
Thanks for your responses.
I really want to get away from using MailMerge at all.
My main area of concern is printing information to the word document.
The WordObject model may work, but I need to know the exact command for printing information to the Word Document.
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6349114
Hi again,

        Try the following code
Set word1 = Nothing
        Set word1 = New Word.Application
        word1.Documents.Add
        word1.Application.WindowState = wdWindowStateMaximize
        word1.ActiveDocument.Content.Font.Name = "Arial"
        word1.ActiveDocument.Content.Font.Size = "14"
        word1.ActiveDocument.Content.Font.Bold = True
        word1.ActiveDocument.Content.Paragraphs.Alignment = wdAlignParagraphCenter
        word1.ActiveDocument.Content = "TEXT YOU WANT TO ENTER"

bye
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6349120
A quicker solution may be to build a text file and import it into word when you are finished.  Let me know if you want an example
0
 

Author Comment

by:jkeogh
ID: 6349183
Thanks again for your help. This is for Dave Greene. Can you let me have an example please. Can the import into Word be automated from within the VB Application or does it require you to leave the app and open Word and import it that way?
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6349191
It can be automated.. one second
0
 
LVL 8

Accepted Solution

by:
Dave_Greene earned 200 total points
ID: 6349224
'Make sure you have a reference to Word Objects.

Private Sub Command1_Click()
  Dim objWord As New Word.Application
  objWord.Documents.Open ("C:\file.txt")
  objWord.Visible = True
  objWord.Documents.Save True, False
End Sub
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6349243
This is more complete...

It saves it as a word document

Private Sub Command1_Click()
  Dim objWord As New Word.Application
  Dim objDoc  As Word.Document
  objWord.Documents.Open ("C:\file.txt")
  Set objDoc = objWord.ActiveDocument
  objWord.Visible = True
  objDoc.SaveAs "C:\file.doc", wdFormatDocument
End Sub
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7198463
Hi jkeogh,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Dave_Greene's comment(s) as an answer.

jkeogh, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7213229
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now