We help IT Professionals succeed at work.

Need help Launching word and write some labels in the opened document

165 Views
Last Modified: 2011-09-20
Hi there
I am new to VB6.
I have written a functionality that print some labels directly.
Now they want to preview in the exactly same format in word.

I need to add a button "print Preview" which will load a word document and print  5 address for each column (2 column max) in label format.
The six fields that will compose the address to print will be retrieved from a database.

How do you pass these fields to a word doc and make them to appear in the right format?

Thanks for your help
Comment
Watch Question

I think you should also study Visual Basic for applications..   It basically Visual basic code and you can contol output using any of the microsoft office suite.  I can help you if you would prefer the output to be in excel.

regards,
Brian MulderFreelance
Top Expert 2006

Commented:
Title : HOWTO: Automate Word from Visual Basic to Create a Mail Merge for Mailing Labels
Source : http://support.microsoft.com/?kbid=258512

Description :
-----------
instead of the textfile as datasource you could use your own data to fill up the labels
-----------

hope this helps a bit

Author

Commented:
Thanks bruintje

thats definetely the kind of things i was looking for.

I am trying to figure out and implement with my data.

Do you have or anybody has some other link or small code snippet that i could use as a learning tool

Thanks
Brian MulderFreelance
Top Expert 2006

Commented:
you could ook at the office automation help file, containing several samples

http://support.microsoft.com/default.aspx?scid=kb;EN-US;253235

Commented:
Take a look at www.ppreview.net

you will be able to do it

Author

Commented:

thanks for your advice i am using mailmerge for this functionality.


what i have done  is

 Dim oApp As Object
   Dim oDoc As Object


    Set oApp = CreateObject("Word.Application")

     'Start a new document in Word
    Set oDoc = oApp.Documents.Add
    oApp.Visible = True
   

  With oDoc.MailMerge
       With .Fields
         MyRSLabel.MoveFirst
         Do While Not MyRSLabel.EOF
            .Add oApp.Selection.Range, lblHeader.Caption
            oApp.Selection.TypeParagraph
           
          .Add oApp.Selection.Range, strAddress
            oApp.Selection.TypeParagraph
      
        
          .Add oApp.Selection.Range, name
            oApp.Selection.TypeParagraph

          etc..
         MyRSLabel.MoveNext
          Loop
        End With


       Dim oAutoText As Object
        Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
        oDoc.Content.Delete
                           
        'Set up the mail merge type as mailing labels and use
        .MainDocumentType = wdMailingLabels

      '////// NOTE HERE
       .OpenDataSource Name:="c:\label.txt"
        oApp.MailingLabel.CreateNewDocument Name:=oApp.MailingLabel.DefaultLabelName, Address:="", _
        AutoText:="MyLabelLayout", LaserTray:=wdPrinterManualFeed

      'Execute the mail merge to generate the labels.
        .Destination = wdSendToNewDocument
        .Execute

        'Delete the AutoText entry you added
        oAutoText.Delete
       
    End With
   
    oDoc.Close False
    oApp.Visible = True



the problems i have with the above is

1) if value is null and i replace with vbnullstring it still crashes the app.
      does it accept null values?
when i value is null i need to insert a blank space Can i do this?

2) i figured out i needed to create a textfile as datasource is this correct?
3) also when it reaches the file to open it says it cannot open the file

can you give some suggestions?

thanks



Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.