Trying to reference MS Word from VB.NET

  • Thread starter Thread starter ryguy7272
  • Start date Start date
R

ryguy7272

I am trying to make the below code work:

Dim WordApp As New Word.Application()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim thisDoc As New Word.Document()
thisDoc = WordApp.Documents.Add
With thisDoc
..Range.InsertAfter(“Printing with Wordâ€)
..Paragraphs.Item(1).Range.Font.Bold = True
..Paragraphs.Item(1).Range.Font.Size = 14
..Range.InsertParagraphAfter()
..Paragraphs.Item(2).Range.Font.Bold = False
..Paragraphs.Item(2).Range.Font.Size = 12
..Range.InsertAfter(“This is the first line of the test printoutâ€)
..Range.InsertParagraphAfter()
..Range.InsertAfter(“and this is the second line of the test printoutâ€)
Try
..PrintOut(True, True)
Catch exc As Exception
MsgBox(exc.Message)
End Try
..Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End With
WordApp.Quit()
End Sub

I see little squiggle lines under Word.Application and Word.Document and
Word. I am thinking that I have to add some kind of library or reference.
Is that right?

What do I need to do to get this to work?

Thanks!
Ryan---
 
Oh, I think i just found it...Click Project > Add Reference > COM > Microsoft
Word 10.0 Object Library

That seems to work.
 
Back
Top