Results 1 to 5 of 5
  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 using VB.NET 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
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    I don't work with VB.Net yet, but I suspect it doesn't allow you coerce datatypes the way VB6 did and it may grab the style object rather than its default property using the syntax you posted. Try using this:

    myLabel2.Text = objParagraph.Range.Style.NameLocal
    Charlotte

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    You can try adding

    OPtion Strict Off

    to the VB.Net code.

    VB.Net does not require explicit type conversion, but it is much safer to to the conversion.

    Lots of stuff from VB 6 will NOT work in VB.Net without some, sometines significant, reprogramming.

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    Have you look at the other answers to your multiple posts? USE the NameLocal property of the Styles object. Reference <!post=this post,131745>this post<!/post>.

    Now, the teacher in me will come out: stop using sloppy habits: define everything, put Option Explicit at the top, never use Option Strict Off.

    Finally, when you are posting, PLEASE only post on one board and if there are additional questions continue using the same thread. Whenever you make a post or a reply the thread moves to the top of the board, so duplicate threads are unnecessary and very aggravating to track. --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    My fault, sorry.

    Thanks for the replies though. I managed to get it to work using:

    myLabel2.Text = objParagraph.Range.Style.NameLocal

    Thanks again.

Posting Permissions

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