none
Problem with VSTO or VBA RRS feed

  • Question

  • I'm assuming that I'm using VSTO.  I'm started VB from inside of Word.  I'm trying to automate a templete in word for printing labels.  In the code below I get a runtime error 424 "object required" at the line where I try to establish the arrays (I also Bolded and Underlined the line).  I have the labels in the word document and named the same as it is in the code.  Not sure what I'm doing wrong but I pretty sure its something simple.  Thanks to anyone that can help.

     

    Private Sub cmdrun_Click()

    Dim amount As Integer
    Dim sonumber As String
    Dim partnumber As String
    Dim partarray(5) As Label
    Dim soarray(5) As Label
    Dim index As Integer
    Dim counter As Integer
    Dim linenumber As String
    Dim pnumber As String
    Dim pcdis As String

    linenumber = txtline.Text
    amount = txtamount.Text
    sonumber = txtso.Text
    partnumber = txtpart.Text
    counter = 0
    index = 1

    partarray(0) = lblpart0.Caption
    partarray(1) = lblpart1.Caption
    partarray(2) = lblpart2.Caption
    partarray(3) = lblpart3.Caption
    partarray(4) = lblpart4.Caption
    partarray(5) = lblpart5.Caption

    soarray(0) = lblso0.Caption
    soarray(1) = lblso1.Caption
    soarray(2) = lblso2.Caption
    soarray(3) = lblso3.Caption
    soarray(4) = lblso4.Caption
    soarray(5) = lblso5.Caption

    Do While index <= amount
        pcdis = index
        partarray(counter) = partnumber
        soarray(counter) = Str(sonumber + "-" + linenumber + "-" + pcdis)
       
        counter = counter + 1
        index = index + 1
       
    Loop
       
       

    End Sub

    Monday, May 16, 2011 9:50 PM

Answers

  • This is a VBA question and should be in a VBA forum.

    But, I think your arrays should be strings not labels.

    Dim partarray(5) As String

    Dim soarray(5) As String


    Kevin Warner
    Tuesday, May 17, 2011 1:30 AM
  • HI adam

    I agree with Kevin on all points. In addition, I see another problem: You've declared an array with five members, but you're trying to assign six values to it. Word won't like that :-)

    I'm moving your question to the Word for Developers forum where it will be more on-target.


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 18, 2011 7:02 AM
    Moderator
  • Slow down a bit! You have not coded what I posted.
     
    You are building an array of the labels, not the captions themselves, so
     
        Dim PartArray(5) As Label
     
    Next, now that you have it declared as a label, you need to 'set' it, so ..
     
        Set PartArray(0) = lblpart0
        Set PartArray(1) = lblpart1
        ' etc.
     
    The code in the loop should then work.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, May 19, 2011 4:53 PM

All replies

  • This is a VBA question and should be in a VBA forum.

    But, I think your arrays should be strings not labels.

    Dim partarray(5) As String

    Dim soarray(5) As String


    Kevin Warner
    Tuesday, May 17, 2011 1:30 AM
  • HI adam

    I agree with Kevin on all points. In addition, I see another problem: You've declared an array with five members, but you're trying to assign six values to it. Word won't like that :-)

    I'm moving your question to the Word for Developers forum where it will be more on-target.


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 18, 2011 7:02 AM
    Moderator
  • If you started VB from inside Word, you are using VBA.
     
    VBA doesn't know what lblpart0 is (or any of the various other lbl... objects). My guess is that this code was copied from a UserForm where these were labels, to somewhere else where they don't exist.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, May 18, 2011 8:42 AM
  • Actually I created this code myself.  I do have the labels and they are named exactly as I'm calling them in the code.  I think the problem has to do with I trying to use the lables in the word doctument as the insertion points of the automated information.  Can I do this or do I have to use bookmarks?
    Wednesday, May 18, 2011 1:43 PM
  • Actually I created this code myself.  I do have the labels and they are named exactly as I'm calling them in the code.  I think the problem has to do with I trying to use the lables in the word doctument as the insertion points of the automated information.  Can I do this or do I have to use bookmarks?


    Hi Adam

    Kevin already showed you how you need to change your code. You may have labels, but you don't want an array of labels: you want an array of their captions. And the data type of a caption is a string. So you need to declare the arrays as strings, or as type Variant. But not as labels.


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 18, 2011 4:30 PM
    Moderator
  • I understand that the variables should be declared as a string or variant.  However I still get the same error; 424 required object.  Its like vb can't find the labels in the document.  Is there a special call out for the document form that I need to use?  If so what is it and how do I use it?  Are you able to place a label in the document and alter the it's caption without getting this error?
    Wednesday, May 18, 2011 5:59 PM
  • I know that part of the problemis because the labels are in the word document and VB doesn't know where to find it, because I tried to reproduce what I was trying to do with labels in the form and had no problems.  However I did find another problem with my code.  In the while loop I'm trying to change the caption of the labels to something else and a get an error miss match type because earlier where I am seting the array = to the label.caption its actually pulling the value from the label, but what I want to do is set the variable = "label(n).Caption" so that I can call the variable and change the caption property with the array.
    Wednesday, May 18, 2011 6:25 PM
  • If the labels are ActiveX controls in the document then, unless your code is in the ThisDocument module, you need to reference them as

        document-ref.lblpart1.Caption

    where document-ref may be ActiveDocument or some other reference depending on exactly what you are doing.


    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, May 18, 2011 7:32 PM
  • Tony on were on the right track thanks so much.  The call out that worked was:

                          ThisDocument.lblpart.Caption

    However I still have this problem as I stated earlier:

                In the while loop I'm trying to change the caption of the labels to something else and a get an error miss match type because earlier where I am seting the array = to the label.caption its actually pulling the value from the label, but what I want to do is set the variable = "label(n).Caption" so that I can call the variable and change the caption property with the when I call the array.

    So what happens when I trouble shoot the partarray(0) = ""         Because  lblpart.caption = "" when the form starts

    What I want to happen is similar to lblpart.caption = partnumber  but instead of using a bunch of redundant code I want to use an array.

    Wednesday, May 18, 2011 8:48 PM
  • I'm not entirely following but, I think, maybe, you want to build an array of your labels, and then do whatever in your loop ....
     
        Dim PartArray(5) As Label
     
        Set PartArray(0) = lblpart0
        Set PartArray(1) = lblpart1
        ' etc.
     
        Do While whatever
            PartArray(counter).Caption = "I have been changed"
        Loop
     
    When you have an array of the captions, they are just strings copied from the labels, and changing them changes those copies, not the labels themselves. Building the array of labels gives you access to the actual captions in  the loop.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, May 19, 2011 6:15 AM
  • Tony thats exactly what I'm trying to acheive, but I thought about that and when the code compiles I get a compile error invaild quailifer on the array variables.  Is there any way I can get around this or do I just have to write some super long and redondant code?  

    Thursday, May 19, 2011 1:26 PM
  • What's the exact code you now have? I lost track with the various comments and changes along the way.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, May 19, 2011 3:09 PM
  • Here is the code that I have now, but it doesn't work in the while loop.  I get a Compile Error "invaild quailifer" on partarray. 

     

     

    Private Sub cmdrun_Click()

    Dim amount As Integer
    Dim sonumber As String
    Dim partnumber As String
    Dim partarray(6) As String
    Dim soarray(6) As String
    Dim index As Integer
    Dim counter As Integer
    Dim linenumber As String
    Dim pnumber As String
    Dim pcdis As String

    linenumber = txtline.Text
    amount = txtamount.Text
    sonumber = txtso.Text
    partnumber = txtpart.Text
    counter = 0
    index = 1

    partarray(0) = ThisDocument.lblpart
    partarray(1) = ThisDocument.lblpart1
    partarray(2) = ThisDocument.lblpart2
    partarray(3) = ThisDocument.lblpart3
    partarray(4) = ThisDocument.lblpart4
    partarray(5) = ThisDocument.lblpart5

    soarray(0) = ThisDocument.lblso
    soarray(1) = ThisDocument.lblso1
    soarray(2) = ThisDocument.lblso2
    soarray(3) = ThisDocument.lblso3
    soarray(4) = ThisDocument.lblso4
    soarray(5) = ThisDocument.lblso5

    Do While index <= amount
        pcdis = index
        partarray(counter).Caption = partnumber
        soarray(counter).Caption = Str(sonumber + "-" + linenumber + "-" + pcdis)
       
        counter = counter + 1
        index = index + 1
       
    Loop
       

     


    End Sub

    Thursday, May 19, 2011 4:02 PM
  • I dodged this error be changing:

            Dim partarray(6) As String
            To:

            Dim partarray(6) As Variant

    However I am now getting the error 424 "object required" at this line:

           partarray(counter).Caption = partnumber
     

    I think that is something wring with my arrays.  When debugging that line it showing counter = 0 and partnumber =  to the correct string.  However partarray is not showing partarray(0) = ThisDocument.lblpart like I think it should (when I put the cursor over partarray(counter) nothing apears at all).  Am I declairing the array correctly?  I feel like I am missing something there.

    Thursday, May 19, 2011 4:21 PM
  • Slow down a bit! You have not coded what I posted.
     
    You are building an array of the labels, not the captions themselves, so
     
        Dim PartArray(5) As Label
     
    Next, now that you have it declared as a label, you need to 'set' it, so ..
     
        Set PartArray(0) = lblpart0
        Set PartArray(1) = lblpart1
        ' etc.
     
    The code in the loop should then work.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, May 19, 2011 4:53 PM
  • Tony,  you hit the nail right on the head.  Ithink that I missing the post were you explained setting the array to label.  Now my code works like I wanted to thanks so much.  Thanks again!!!
    Thursday, May 19, 2011 5:45 PM