简体   繁体   中英

MS-Word VBA reference identification

I have a custom content control in Microsoft Word from a third party I am trying to resize the width and height of. Normal content control selection via VBA is not working because this control has no Title or Tag. However, if I manually select the object and resize it programmatically using "Selection.ShapeRange.Height = x" or ShapeRange.Width it does work. So to do it all programmatically I need to determine the name of the "selection" without having to manually select it.

Is there a way to "inspect" the complete reference to the currently selected object in word, so we can then get a starting point to work with it in VBA?

It's hard to know what type of object your dealing with. I tested this by inserting a blank ActiveX image control, selecting it and then running the macro. The code has two methods but one is commented out.

Sub FindName()
    MsgBox (Selection.Fields.Item(1).OLEFormat.ClassType)

    'MsgBox (Selection.InlineShapes.Item(1).OLEFormat.ClassType)

    MsgBox (Selection.InlineShapes.Item(1).Field.Index)
    MsgBox (Selection.InlineShapes.Item(1).AlternativeText)

    'Show current name
    MsgBox (Selection.Fields.Item(1).OLEFormat.Object.Name)

    'Set new name
    Selection.Fields.Item(1).OLEFormat.Object.Name = "Image5"

    'Re-display name to show that it changed
    MsgBox (Selection.Fields.Item(1).OLEFormat.Object.Name)
End Sub

The result was this:

在此处输入图片说明在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM