Solved

Writing to a Word Document from VB using Print # command

Posted on 2001-08-03
13
266 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

861 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

23 Experts available now in Live!

Get 1:1 Help Now