帮我写个excel加水印的代码
时间: 2024-05-05 11:05:47 浏览: 104
Sub AddWatermark()
Dim ws As Worksheet
Dim shp As Shape
Dim txt As String
Dim fontName As String
Dim fontSize As Integer
Set ws = ActiveSheet
'输入水印内容
txt = InputBox("请输入水印内容:")
If txt = "" Then Exit Sub
'输入字体名称
fontName = InputBox("请输入字体名称:")
If fontName = "" Then Exit Sub
'输入字体大小
On Error Resume Next
fontSize = InputBox("请输入字体大小:")
If fontSize = 0 Then Exit Sub
'创建文本框
Set shp = ws.Shapes.AddTextbox(msoTextOrientationHorizontal, _
0, 0, ws.Range("A1").Width, ws.Range("A1").Height)
shp.Select
'设置文本框属性
With Selection
.Name = "水印"
.TextFrame.AutoSize = True
.TextFrame2.TextRange.Font.Name = fontName
.TextFrame2.TextRange.Font.Size = fontSize
.TextFrame2.TextRange.Font.Color.RGB = RGB(192, 192, 192)
.TextFrame2.TextRange.Text = txt
.TextFrame2.VerticalAnchor = msoAnchorMiddle
.TextFrame2.HorizontalAnchor = msoAnchorCenter
.TextFrame2.Orientation = msoTextOrientationHorizontal
.TextFrame2.MarginBottom = 0
.TextFrame2.MarginTop = 0
.TextFrame2.MarginLeft = 0
.TextFrame2.MarginRight = 0
.Line.Visible = msoFalse
.Placement = xlFreeFloating
.LockAspectRatio = msoTrue
.Top = ws.Range("A1").Top + (ws.Range("A1").Height - shp.Height) / 2
.Left = ws.Range("A1").Left + (ws.Range("A1").Width - shp.Width) / 2
End With
'设置文本框为图层底部
shp.ZOrder msoSendToBack
End Sub
阅读全文