VBA Toggle

Joined
Apr 29, 2012
Messages
1
Reaction score
0
I am trying to write VBA code to toggle a draft watermark on and then off. I have written one macro to insert the watermark, and a seperate one to remove, but would like one command for all, I assume this is a toggle button, but don't know where to begin.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
You could do it with a single macro, but the first thing it would have to do is to test whether the watermark is present, then decide what to do on that basis. Try something like:
Code:
Sub Demo()
Dim i As Long, bWtrmk As Boolean, Shp As Shape
bWtrmk = False
With ActiveDocument.Sections.First.Headers(wdHeaderFooterPrimary)
  For i = .Shapes.Count To 1 Step -1
    If InStr(.Shapes(i).Name, "WordPictureWatermark") > 0 Then
      .Shapes(i).Delete
      bWtrmk = True
    End If
  Next
  If bWtrmk = False Then
    Set Shp = .Shapes.AddPicture(LinkToFile:=False, SaveWithDocument:=True, _
      FileName:="C:\Users\" & Environ("UserName") & "\Pictures\IMG_0001.JPG")
    With Shp
      .Name = "WordPictureWatermark"
      .PictureFormat.Brightness = 0.85
      .PictureFormat.Contrast = 0.15
      .LockAspectRatio = True
      .Width = InchesToPoints(6)
      .WrapFormat.AllowOverlap = True
      .WrapFormat.Type = 3
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      .Left = wdShapeCenter
      .Top = wdShapeCenter
    End With
  End If
End With
End Sub
Note that you'll need to change the filename to suit your requirements
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top