jkeogh
asked on
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.
(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.
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
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
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.
Other alternatives include:
use a datareport.
use richtextbox control.
personally, i'd probably use the datareport.
Hi,
Why don't u try the following command
set word1=new Word.Application
word1.Documents.Open("TEST FILE.DOC") .Protect(. ......)
I hope this would work....
:-)
bye
Mohan
Why don't u try the following command
set word1=new Word.Application
word1.Documents.Open("TEST
I hope this would work....
:-)
bye
Mohan
ASKER
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.
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.
Hi again,
Try the following code
Set word1 = Nothing
Set word1 = New Word.Application
word1.Documents.Add
word1.Application.WindowSt ate = wdWindowStateMaximize
word1.ActiveDocument.Conte nt.Font.Na me = "Arial"
word1.ActiveDocument.Conte nt.Font.Si ze = "14"
word1.ActiveDocument.Conte nt.Font.Bo ld = True
word1.ActiveDocument.Conte nt.Paragra phs.Alignm ent = wdAlignParagraphCenter
word1.ActiveDocument.Conte nt = "TEXT YOU WANT TO ENTER"
bye
Try the following code
Set word1 = Nothing
Set word1 = New Word.Application
word1.Documents.Add
word1.Application.WindowSt
word1.ActiveDocument.Conte
word1.ActiveDocument.Conte
word1.ActiveDocument.Conte
word1.ActiveDocument.Conte
word1.ActiveDocument.Conte
bye
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
ASKER
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?
It can be automated.. one second
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
Per recommendation, force-accepted.
Netminder
CS Moderator
Netminder
CS Moderator
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.O
"C:\test.txt", ConfirmConversions:=False,
, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", WritePasswordDocument:="",
"", Revert:=False, Format:=wdOpenFormatAuto, Connection:="", SQLStatement _
:="", SQLStatement1:=""
etc....