Word Document

G

Guest

I am trying to insert text into a Word document and then apply a style to the
paragraph I just wrote. My problem is that the range stays set to the entire
document and I can't figure out how to reset it to the end of the document so
whatever style I apply applies to just the last paragraph I wrote. Right now
the style gets applied to the entire document. Here is the class:

Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.PowerPoint

Public Class CR_Document_Writer
Private _Word As New Microsoft.Office.Interop.Word.Application
Private _doc As Microsoft.Office.Interop.Word.Document
Public Sub Create()
Try
_doc = _Word.Documents.Add("Test.dot")
_doc.Activate()
_Word.Visible = True
Append("Title Of The Document", "Title")
Append("This is a line of text.", "List Bullet")
_doc.SaveAs("c:\test.doc")
Catch ex As COMException
MessageBox.Show("Error accessing Word document.")
End Try
End Sub
Private Sub Append(ByVal TextString As String, ByVal inStyle As String)
Try
_doc.Range.Collapse(WdCollapseDirection.wdCollapseEnd)
_doc.Range.InsertAfter(TextString)
_doc.Range.Style = inStyle
_doc.Range.InsertParagraphAfter()
Catch ex As Exception
MessageBox.Show("Error in writing to Word document.")
End Try
End Sub
End Class

What am I doing wrong?

Thanks,
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top