Results 1 to 4 of 4

Thread: use control in word by name

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    Post use control in word by name

    Hi,
    I am trying to assign some values to some activex textboxes in a word 2007 document.
    i have named the textboxes like txt1,txt2,txt3,txt4,...

    Now i m wondering if there is something like this possible:

    for i = 1 to 4
    getControlByName("txt" & i).text = "test " & i
    next

    Thanks in advance

  2. #2
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: use control in word by name

    You might have to go the route of:
    Code:
    For i - 1 to 4
    tmpVal = "txt" & i
    getControlByName(tmpVal).text = "test" & i
    Next i

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    Re: use control in word by name

    Hi fizii,
    Thanks for the quick reply.
    I 've been trying that already and i know it works fine in .NET but in office VBA it gives me "Sub or Function not defined".
    So i was wondering if there is an alternative for this?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: use control in word by name

    you can try like
    vb Code:
    1. For i = 1 To 3
    2.     activedocument.Shapes("Text box " & i).TextFrame.TextRange = "test " & i
    3. Next
    assuming your textboxes are named as default
    text box 1
    text box 2
    etc
    or with your existing texboxes
    activedocument.Shapes("txt" & i).TextFrame.TextRange = "test " & i
    and change to whatever document object you want to use
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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