• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Writing to a Word Document from VB using Print # command

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
jkeogh
Asked:
jkeogh
  • 4
  • 2
  • 2
  • +4
1 Solution
 
bobbit31Commented:
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
 
nigelroweCommented:
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
 
bobbit31Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mohan_sekarCommented:
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
 
jkeoghAuthor Commented:
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
 
mohan_sekarCommented:
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
 
Dave_GreeneCommented:
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
 
jkeoghAuthor Commented:
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
 
Dave_GreeneCommented:
It can be automated.. one second
0
 
Dave_GreeneCommented:
'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
 
Dave_GreeneCommented:
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
 
DanRollinsCommented:
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
 
NetminderCommented:
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now