Solved

Custom Printing from VB

Posted on 1999-01-14
8
149 Views
Last Modified: 2010-05-03
I want to print a standard letter from VB. I want to be able to change "fields" in this letter at runtime before I print it. For example: I have a MSWord doc all nicely formatted and including some graphics, but now I want to change the name/address etc and only then print it. I'm using VB6 (VisStudio).
0
Comment
Question by:ayrezyle
8 Comments
 
LVL 1

Expert Comment

by:wford
ID: 1456483
You can do this buy getting control of the word application as an object and then useing VBA to Load the file, Insert name and address into the field then tell it to print.

If this sound right I can post you some code
0
 
LVL 14

Expert Comment

by:waty
ID: 1456484
You could try the PrintPreview OCX. It will do what you need.

http://www.geocities.com/ResearchTriangle/6311/
0
 
LVL 1

Expert Comment

by:ikxsoft
ID: 1456485
try using the rich edit control to load the file, then change the field and print
0
 
LVL 1

Author Comment

by:ayrezyle
ID: 1456486
I still don't know *how* to edit those fields. I want to do some funky DTP and only then edit those fields. That implies that this RTF control must support inserted images with text over backgrounds, etc. Thus for ikxsoft's answer to be accepted, I need code that shows *how* to edit fields and how do I initialise the RTF doc. With Word I assume, but what/how/where those fields I wanna change?
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 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 1456487
Create bookmarks in your MSWord document. Then open this document from VB using automation. Replace the bookmarks with your values. Launch the print.

Try this code snippet:
Private Sub Command1_Click()
Dim objWord As Word.Application

    Set objWord = New Word.Application
    With objWord
        .Visible = True
        .Documents.Open "c:\test.doc"
        .ActiveDocument.Bookmarks.Item("aa").Select
        .Selection.InsertAfter "Hello"
        .PrintOut
        .Quit False
    End With
    Set objWord = Nothing
End Sub

0
 
LVL 1

Author Comment

by:ayrezyle
ID: 1456488
ummm, problem. For some reason, when I run the code above, I get the error "User-defined type not defined" with "Word.Application" highlighted <spelling?> Where/how do I get it to recognise that object type?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 1456489
In the Project References, you have to select Microsoft Word Object Library.
0
 
LVL 1

Author Comment

by:ayrezyle
ID: 1456490
Yep, that was it! Thanks!!!
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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

920 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