简体   繁体   中英

Add shadow to text in ms word by vba

I am working on a template in ms word, I need to add shadow to a selection.

The macro recorder gives

Selection.Font.Shadow = true

but I didn't know how to use it and how to determine the type of shadow and its properties. I didn't find the name of these types or how to deal with them, like setting the depth or angle.

Indeed, the macro recorder does not yield much information about working with "Text Effects"...

The Shadow property can only set whether a text effect shadow is "on" or "off".

To change how the shadow looks it's necessary to work with a ShadowFormat object. The documentation for ShadowFormat is somewhat misleading since it refers only to Shape objects. But the same properties apply to shadows for fonts.

What's more, the property for returning a ShadowFormat object for a Font is not ShadowFormat, it's TextShadow . So code like the following example:

Sub AddFontShadow()
    Dim shad As Word.ShadowFormat

    Set shad = Selection.Font.TextShadow
    With shad
        Debug.Print "Blur: " & .Blur, _
                    "ForeColor: " & .ForeColor, _
                    "Obscured: " & .Obscured, _
                    "OffsetX: " & .OffsetX, _
                    "OffsetY: " & .OffsetY, _
                    "Style: " & .style, _
                    "Transparency: " & .Transparency, _
                    "Type: " & .Type
    End With
End Sub

Extending on the accepted answer:

    Set img = ActiveDocument.Shapes.AddPicture(FileName:=fn)
    img.ConvertToInlineShape
    With img.Shadow
        .Blur = 5
        .ForeColor = 0
        ' .Obscured = -2 (fails for negative number)
        .OffsetX = 0
        .OffsetY = 0
        .Style = 2
        .Transparency = 0.6
        .Type = 25
        .Visible = True
    End With

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