Prev: Need Word 2003 code to send email as attachment
Next: Word form - hidden rows appear when form is clicked
From: Robin on 13 Jan 2010 14:05 I have a document that has a header in it. I want to add a watermark from my building blocks into that header, without erasing or replacing the existing header. This means I would show the header and the watermark simultaneously. So far I have managed to replace the header with the watermark, but I don't want that, I want both to show. thanks Robin
From: Jay Freedman on 13 Jan 2010 21:07 On Wed, 13 Jan 2010 11:05:01 -0800, Robin <Robin(a)discussions.microsoft.com> wrote: >I have a document that has a header in it. I want to add a watermark from my >building blocks into that header, without erasing or replacing the existing >header. This means I would show the header and the watermark simultaneously. >So far I have managed to replace the header with the watermark, but I don't >want that, I want both to show. > >thanks >Robin The "trick" is to declare a Range object, set it to the header's range, *and collapse it to the start or end*. If you use the entire range of the header, then the existing text will be removed. This works: Sub InsertWatermark() Dim oRg As Range Dim oTempl As Template, tempTempl As Template Dim oBBE As BuildingBlock, tempBBE As BuildingBlock Dim idx As Long Dim BBEname As String BBEname = "CONFIDENTIAL 1" ' name of watermark to insert Templates.LoadBuildingBlocks Set oRg = _ ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range oRg.Collapse wdCollapseStart ' <== MUST DO THIS For Each tempTempl In Templates If InStr(LCase(tempTempl.Name), "building blocks.dotx") Then Set oTempl = tempTempl Exit For End If Next If oTempl Is Nothing Then MsgBox "Building Blocks.dotx not found" Exit Sub End If For idx = 1 To oTempl.BuildingBlockEntries.Count Set tempBBE = oTempl.BuildingBlockEntries(idx) If InStr(LCase(tempBBE.Name), LCase(BBEname)) Then Set oBBE = tempBBE Exit For End If Next If oBBE Is Nothing Then MsgBox BBEname & " not found" Exit Sub End If oBBE.Insert where:=oRg, RichText:=True End Sub -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
From: Robin on 14 Jan 2010 18:16 Jay thanks for the good info. I'm still battling along here. What you suggest adds a watermark to a header without removing the existing header. If I try the opposite, for example I have a header that is a building block (a collection of linked images defined as a header building block), and I want to apply it in a section where there is a watermark (a single linked image defined as a watermark building block) in that section's header - how would I do that? Do I use the same code? From my experience the watermark and everything else gets erased if I use the WordBasic.RemoveHeader method. I can see that even though the watermark is a watermark building block, it has a MSOShapeType of 11, as do the other images in the header I am trying to apply. So does VBA even differentiate between watermark and header building blocks or the content therein? Thanks Robin
From: Jay Freedman on 14 Jan 2010 20:56
On Thu, 14 Jan 2010 15:16:01 -0800, Robin <Robin(a)discussions.microsoft.com> wrote: >Jay thanks for the good info. I'm still battling along here. > >What you suggest adds a watermark to a header without removing the existing >header. If I try the opposite, for example I have a header that is a building >block (a collection of linked images defined as a header building block), and >I want to apply it in a section where there is a watermark (a single linked >image defined as a watermark building block) in that section's header - how >would I do that? Do I use the same code? > >From my experience the watermark and everything else gets erased if I use >the WordBasic.RemoveHeader method. I can see that even though the watermark >is a watermark building block, it has a MSOShapeType of 11, as do the other >images in the header I am trying to apply. So does VBA even differentiate >between watermark and header building blocks or the content therein? > >Thanks >Robin Yes, you can use the same code. The macro I posted doesn't look at the gallery the building block belongs to; it just uses the first building block whose name matches the BBEname variable. As long as there's only one building block with that name, the macro is fine. There can actually be multiple building blocks with the same name, as long as each one is has a different type/category pair. You can select a specific building block to insert by type and category. The principle of collapsing the range in which to insert it still applies. For example, in my macro, instead of looping through the entire BuildingBlockEntries collection looking for the name, you could use Set oBBE = oTempl.BuildingBlockTypes(wdTypeWatermarks) _ .Categories("Confidential").BuildingBlocks(BBEname) There are a lot more details in the VBA help topic "Working with Building Blocks". The WordBasic.RemoveHeader method is a very blunt tool. I'm not surprised that it nukes everything in the header. Once a graphic or other object is inserted into the document, VBA doesn't "know" that it was a building block before it was inserted. It's just a shape of a particular type (linked picture, text effect [WordArt], etc.). -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. |