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.Offic e.Interop.Word
Imports Microsoft.Offic e.Interop.Power Point
Public Class CR_Document_Wri ter
Private _Word As New Microsoft.Offic e.Interop.Word. Application
Private _doc As Microsoft.Offic e.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.Coll apse(WdCollapse Direction.wdCol lapseEnd)
_doc.Range.Inse rtAfter(TextStr ing)
_doc.Range.Styl e = inStyle
_doc.Range.Inse rtParagraphAfte r()
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
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.Offic e.Interop.Word
Imports Microsoft.Offic e.Interop.Power Point
Public Class CR_Document_Wri ter
Private _Word As New Microsoft.Offic e.Interop.Word. Application
Private _doc As Microsoft.Offic e.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.Coll apse(WdCollapse Direction.wdCol lapseEnd)
_doc.Range.Inse rtAfter(TextStr ing)
_doc.Range.Styl e = inStyle
_doc.Range.Inse rtParagraphAfte r()
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
Comment