Word Document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Y3JhaWc=?=

    #1

    Word Document

    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
  • Samuel

    #2
    Re: Word Document

    I suggest you should post to: microsoft.publi c.word.vba.gene ral


    "craig" <craig@discussi ons.microsoft.c omwrote in message
    news:40DD8E55-A891-4FF5-BB4C-66BE2795F6CF@mi crosoft.com...
    >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

    Comment

    Working...