Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error - Please help!! (.NET / Office XP)

    Hi,

    I am creating an application that loops through each of the paragraphs in a Word Document and writes the paragraph text to a text box and the paragraph style type to the text of a label.

    In VB6.0 this worked perfectly, however, using VB.NET I am getting an error when I try to set:

    myLabel2.Text = objParagraph.Range.Style

    ***************************************
    Error:
    "An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll"

    "Cast from type 'Style' to type 'String' is not valid."
    ****************************************

    I am guessing this is because the .Text property is a String() and the Range.Style is an object(). However, this did work in VB6.0

    Here is my code:

    Dim objParagraph As Word.Paragraph
    Dim myTextBox as TextBox
    Dim myLabel2 as Label

    For Each objParagraph In objWord.Documents.Item(i).Paragraphs
    'Create TextBoxes:
    myTextBox = New TextBox()
    myTextBox.Name = "TextBox" & i
    myTextBox.Text = objParagraph.Range.Text
    myTextBox.Location = New System.Drawing.Point(locX, locY)
    f1.Controls.Add(myTextBox)

    'Create Labels:
    myLabel2 = New Label()
    myLabel2.Size = New System.Drawing.Size(100, 16)
    myLabel2.Text = objParagraph.Range.Style
    myLabel2.Visible = True
    f1.Controls.Add(myLabel2)

    Next

    Any help in getting this to work for VB.NET would be much appreciated

    Many thanks,

    Chris

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Error - Please help!! (.NET / Office XP)

    The style name is <font color=blue>objParagraph.Style.NameLocal</font color=blue> although you would get the same result from <font color=blue>objParagraph.Range.Style.NameLocal</font color=blue>

    StuartR

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Error - Please help!! (.NET / Office XP)

    To follow the further adventures of this inquiry, refer to the duplicate post on the VBA board.

Posting Permissions

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