PDA

View Full Version : UserForm Multiple Bookmark Delete



ajhez
05-15-2017, 07:16 AM
Hi all

I have a document which is populated by a userform.

The date inputted in the userform is then used to fill in the respective bookmarks within the document.

One element of document is a number of different schedules at the end. The user will select from a combobox the relevant schedule which needs to be attached and all of the other schedules will need to be deleted.

E.g. User selects "S1" then Schedule 1 will remain and Schedules 2, 3, 4, 5 will all be deleted.

Each schedule has a bookmark which covers the entire schedule - so I want to have it that when "S1" is selected it will delete the bookmarks (and the contents of) Schedules 2, 3, 4, 5 or when "S4" is selcted it will delete the bookmarks (and contents of) Schedules 1, 2, 3 and 5.

Any ideas?

Thanks,
AJH

gmayor
05-15-2017, 08:01 AM
If you have the schedules as autotext entries stored in the template then you could use FillBM to write an empty string to clear the named bookmark or simply use AutoTextToBM to write the appropriate autotext to a named bookmark.


Public Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub


Sub AutoTextToBM(strBMName As String, oTemplate As Template, strAutotext As String)
'Graham Mayor - http://www.gmayor.com
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
With ActiveDocument
Set oRng = .Bookmarks(strBMName).Range
Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
(Where:=oRng, RichText:=True)
.Bookmarks.Add Name:=strBMName, Range:=oRng
End With
lbl_Exit:
Exit Sub
End Sub