Solved

Writing to a Word Document from VB using Print # command

Posted on 2001-08-03
13
262 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

746 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

12 Experts available now in Live!

Get 1:1 Help Now