Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Automate Microsoft Word From VB6

CarterLangleyCarterLangley Member Posts: 29
Hi guys,
Haven't laid code to screen in about 8 years so am very rusty! I need to use MS Word to show results from a database search, Database is Access 2003. Word is also 2003. I am querying the database and getting the info I need no problem. Just need to display that information in Word. Now the information might contain just one record or it might contain over 200 records. How do I display these records in Word? The code must do everything, as the people who are going to be using this little program know how to switch off a computer and then switch it back on again and that is about the level of their knowledge!! Ok, joking, but you get the picture!! They use a form to enter search criteria. Once they have entered that criteria, they click a button and then that must query the database and then display the results in a word document that they can then choose whether they would like to print or save or whatever. I have got as far as getting the info from my database but need to now display it. So......... help would be most gratefully accepted!!!!!

Comments

  • dokken2dokken2 Member Posts: 532
    simple automation to create a new word document and insert some text, office 2002 but should be similar to 2003-


    'PROJECT, REFERENCES: need to add a ref for
    ' MICROSOFT WORD xx.x OBJECT LIBRARY

    'ms office automation
    'declare word app
    Dim wordapp As Word.Application

    Private Sub Command1_Click()
    'create new word instance
    Set wordapp = New Word.Application
    'make word visible
    wordapp.Visible = True
    'add new document
    wordapp.Documents.Add
    wordapp.Documents(1).Activate

    'insert text into paragraph
    wordapp.Documents(1).Paragraphs(1).Range.InsertAfter ("MyText")
    End Sub

    Private Sub Command2_Click()
    'close word document
    wordapp.Documents(1).Close
    'quit word
    wordapp.Quit False
    'release resouces
    Set wordapp = Nothing
    End Sub
  • CarterLangleyCarterLangley Member Posts: 29
    : simple automation to create a new word document and insert some
    : text, office 2002 but should be similar to 2003-
    :
    :
    : 'PROJECT, REFERENCES: need to add a ref for
    : ' MICROSOFT WORD xx.x OBJECT LIBRARY
    :
    : 'ms office automation
    : 'declare word app
    : Dim wordapp As Word.Application
    :
    : Private Sub Command1_Click()
    : 'create new word instance
    : Set wordapp = New Word.Application
    : 'make word visible
    : wordapp.Visible = True
    : 'add new document
    : wordapp.Documents.Add
    : wordapp.Documents(1).Activate
    :
    : 'insert text into paragraph
    : wordapp.Documents(1).Paragraphs(1).Range.InsertAfter ("MyText")
    : End Sub
    :
    : Private Sub Command2_Click()
    : 'close word document
    : wordapp.Documents(1).Close
    : 'quit word
    : wordapp.Quit False
    : 'release resouces
    : Set wordapp = Nothing
    : End Sub
    :

    Ok, thanks for this!!
    the next question that comes to mind is:-
    After you have added the word "MyText", how do you add another word on the same line without overwriting the first word? I need to use my recordset to add one record per line, and then the code must create a new line and enter the next recordset, and so on until the end of the recordset.

    So creating the word object and then using that to create a new document and then to populate the document is not a problem, I can see the logic in that. I just need to be able to use my recordset to populate the line.
    You will have a top line that will contain the "headers" and then under each "header" you will have the information from the recordset.

    For example:-

    Depth In Depth Out AzIn AzOut (The Headers)
    3000 4000 30 170 (First Record)
    4000 5000 40 60 (Second Record)
    5000 5100 0 10 (Third Record)
    and on and on and on until the end of the recordset.

    If you could maybe help me on this one then I will be eternally gratefull!!



  • dokken2dokken2 Member Posts: 532
    : Ok, thanks for this!!
    : the next question that comes to mind is:-
    : After you have added the word "MyText", how do you add another word
    : on the same line without overwriting the first word? I need to use
    : my recordset to add one record per line, and then the code must
    : create a new line and enter the next recordset, and so on until the
    : end of the recordset.
    :
    : So creating the word object and then using that to create a new
    : document and then to populate the document is not a problem, I can
    : see the logic in that. I just need to be able to use my recordset to
    : populate the line.
    : You will have a top line that will contain the "headers" and then
    : under each "header" you will have the information from the recordset.
    :
    : For example:-
    :
    : Depth In Depth Out AzIn AzOut (The Headers)
    : 3000 4000 30 170 (First Record)
    : 4000 5000 40 60 (Second Record)
    : 5000 5100 0 10 (Third Record)
    : and on and on and on until the end of the recordset.
    :
    : If you could maybe help me on this one then I will be eternally
    : gratefull!!
    :
    :
    :
    :
    loop thru the records, append a carriage-return for a new line, and insert after the last-paragraph [use the vbTAB constant to space columns out with tab-stops. ie: myfield = "abc" & vbTab & "123"]

    Do While Not rs.EOF
    myfield = rs!field1 & vbCr 'FORCE NEWLINE
    lastpara = wordapp.Documents(1).Paragraphs.Count
    wordapp.Documents(1).Paragraphs(lastpara).Range.InsertAfter (myfield)
    rs.movenext
    Loop
Sign In or Register to comment.