Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,381 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,370 people online right now. Registration is fast and FREE... Join Now!




Adding to a template using word automation...

 
Reply to this topicStart new topic

Adding to a template using word automation...

hawkvalley1
13 Sep, 2008 - 06:16 PM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2008
Posts: 15

Hi All.

Learning Word automation, this what I have found/learned so far during my searching:

Class Variables:
CODE

Dim oWord As Microsoft.Office.Interop.Word.Application
    Dim oDoc As Microsoft.Office.Interop.Word.Document
    Dim oPar As word.Range

Code in Function that is carried out by background worker:
CODE

Try
            oWord = New word.Application
            oWord = CType(CreateObject("Word.Application"), word.Application)
            oDoc = oWord.Documents.Add(Template:=txtFolderLoc.Text & "\Report.dot")
            Dim save As Object = False
            Dim dest As Object = txtFolderLoc.Text & "\"
            For Each item In ListBox2.Items.ToString
                oPar.Text = item
            Next
            oWord.Visible = True
            oDoc.SaveAs(dest & txtFileName.Text)
            oWord.Quit(save)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return oWord


It opens the template, print the last listbox2.item instead of all the items (maybe the listbox.items.tostring problem)otherwise it is working fine. One time while it was loading I noticed all the items printed to the same line overwriting each other til it printed the last .items printed to the file. What am I missing???
User is offlineProfile CardPM
+Quote Post

Bort
RE: Adding To A Template Using Word Automation...
15 Sep, 2008 - 02:19 AM
Post #2

D.I.C Regular
Group Icon

Joined: 18 Sep, 2006
Posts: 497



Thanked: 5 times
Dream Kudos: 350
My Contributions
Try this:

CODE

For Each item in ListBox2.Items.ToString
        par.Text &= item & ", "
Next


This should list items like this:
Apple, Banana, Cucumber, etc.

Also, take a look at my Office Automation tutorial, here:

http://www.dreamincode.net/forums/index.ph...mp;#entry411354

You may find something useful in there.
User is offlineProfile CardPM
+Quote Post

hawkvalley1
RE: Adding To A Template Using Word Automation...
15 Sep, 2008 - 08:45 AM
Post #3

New D.I.C Head
*

Joined: 2 Jul, 2008
Posts: 15

Thanks Bort.

I will try that, would like each item on there own line, will vbcrlf work after 'item'?
The next problem: I am using a background worker to create the file so I can still work on the program, not sure what I changed but now I get an error for referencing an object(listbox2) from a diff threat that it was created? I probably need to create a safe thread callback for the item(listbox). Ok, now I'm getting an HResult 0x80004002 error, any ideas?

Thanks for the link, will check that out to...

This post has been edited by hawkvalley1: 15 Sep, 2008 - 09:16 AM
User is offlineProfile CardPM
+Quote Post

Bort
RE: Adding To A Template Using Word Automation...
16 Sep, 2008 - 01:33 AM
Post #4

D.I.C Regular
Group Icon

Joined: 18 Sep, 2006
Posts: 497



Thanked: 5 times
Dream Kudos: 350
My Contributions
VbCrlf will work, just change the line to:

CODE

        par.Text &= item & "," & vbcrlf


HTH,
Bort
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month