Results 1 to 5 of 5

Thread: Writing formatted text at a bookmark in Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Cork, Ireland
    Posts
    14

    Writing formatted text at a bookmark in Word

    I been going over a few of the posts on opening Word documents and writing to them at a Bookmark through VB.NET and I have that working OK. What I would like to do is use bold and underlining formatting (and more no doubt)

    At the top of the class I have
    Code:
    Private WordApp As New Word.Application
    and the main code is:
    Code:
    Dim fileName As Object = "C:\Proposal.doc"
            Dim [readOnly] As Object = False
            Dim isVisible As Object = True
    
            Dim missing As Object = System.Reflection.Missing.Value
    
            Dim WordDoc As New Word.Document
    
            WordApp.Visible = True
            WordDoc = WordApp.Documents.Open(fileName, missing,
    [readOnly], missing, missing, missing, missing, missing, missing,
     missing, missing, isVisible)
    
            WordApp.Activate()
    
            WordDoc.Bookmarks.Item("MyBookmark").Range.Text = "I am a line of text"
    If I were to start typing at the start of the document the following works:
    Code:
    WordApp.Selection.Font.Bold = True
            WordApp.Selection.TypeText("Bold Text")
            WordApp.Selection.Font.Bold = False
    but I can't this working at the bookmark.

    Help much appreciated
    Last edited by chakotha; Mar 12th, 2004 at 10:24 AM.

  2. #2
    Member
    Join Date
    Mar 2004
    Posts
    39
    This worked for me:

    Code:
            Dim wdApp As New Word.Application
            Dim wdDoc As Word.Document = wdApp.Documents.Open("C:\Test.doc")
            wdDoc.Bookmarks.Item("Test").Select()
            wdApp.Selection.Font.Bold = True
            wdApp.Selection.Text = "Testtext"
            wdApp.Visible = True
    /Nisse

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Cork, Ireland
    Posts
    14
    Excellent thanks!

    Would you know how fix this?

    If I go

    Code:
    wdApp.Selection.Text = "Testtext"
    wdApp.Selection.Text = "Testing 1 2 3"
    The second line over-writes the first. What I want to do is put a line break between the two lines. I am not sure how to do this either. I tried

    Code:
    wdApp.Selection.Text = "Testtext"
    wdApp.Selection.TypeParagraph()
    wdApp.Selection.Text = "Testing 1 2 3"
    but the second line just over-wrote the first. I am trying to do something like this:

    Heading

    Small heading

    yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda

    Small heading

    yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda yadda

  4. #4
    Member
    Join Date
    Mar 2004
    Posts
    39
    Well you could try to just build the entire string and have a go at it for a single row, like the following:

    Code:
    wdApp.Selection.Text = "Text1" & ControlChars.CrLf & "Text2" & ControlChars.CrLf & "Text3" & ControlChars.CrLf &"Text4"

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Cork, Ireland
    Posts
    14
    Thanks ControlChars.CrLf and other formatting all into 1 string is working! I'll proceed with that

    Do you know how I can switch to bold within that string?

    WordApp.Selection.Text = "Text1" & WordApp.Selection.Font.Bold = True & "Text2"

    writes 'False' to the doc.
    Last edited by chakotha; Mar 15th, 2004 at 03:54 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width