Variable as Object

squeebles

New Member
Joined
Feb 13, 2006
Messages
11
i have some labels in a word document that i can manipulate from excel but i want to be able to change each label with one sub. the naming of my labels is in the form of "Label5y" and "Label5n". this sub is meant to determine the contents of a cell and change the labels accordingly. this sub works if i hard code the name of each label but i need them to change dynamically. if there is a different way to go about this i am all ears.

Sub yesNo(col As String, ynLabel As String, i)

Dim labely As String
Dim labeln As String

labely = "Label" & ynLabel & "y"
labeln = "Label" & ynLabel & "n"

If Range(col & i) = "yes" Then
appWD.ActiveDocument.labely.Caption = "(Yes)"
appWD.ActiveDocument.labeln.Caption = "No"
appWD.ActiveDocument.labeln.Font.Strikethrough = True
Else
appWD.ActiveDocument.labely.Caption = "Yes"
appWD.ActiveDocument.labely.Font.Strikethrough = True
appWD.ActiveDocument.labeln.Caption = "(No)"
End If

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How have you created these labels?

Are they ActiveX labels?
 
Upvote 0
I don't think Labels will work.

I have found a couple of ways to refer to ActiveX labels on a Word document but not by name.

I'm going to have a Google now to see if I can find something for that.
 
Upvote 0
Sorry-didn't see that it was an ActiveX object.

I don't know Word VBA, but you'll need something like:

Code:
Dim MyObj As OLEObject
For Each MyObj In ActiveDocument.OLEObjects
    If MyObj.Name = labely Then
        MyObj.Object.Caption = "(Yes)"
    End If
Next MyObj

That might not work right off the bat (it'd work for sure if the label were in Excel), but it's the right idea. You might have to record a macro in Word to get the syntax correct; for example, ActiveDocument.OLEObjects might be ActiveDocument.SomethingElse.OLEObjects.
 
Upvote 0
hahdawg

Unfortunately OLEObjects doesn't appear to exist in Word.

The only ways I found were InlineShapes and Fields, but as I said neither allows you to use the name.

I'm sure there's something I'm missing though which is why I'm still looking.
 
Upvote 0
i recorded a macro in word but it would not let me edit the label. however the code for adding the label is as such:
Code:
Selection.InlineShapes.AddOLEControl ClassType:="Forms.Label.1"
btw thank you both for your responses
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,540
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top