Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-26-2015, 11:34 AM
Schildkröte Schildkröte is offline Bookmarks Windows 8 Bookmarks Office 2013
Novice
Bookmarks
 
Join Date: Jun 2015
Posts: 6
Schildkröte is on a distinguished road
Default Bookmarks

Hi,
I have a bookmark which contains text and sometimes also a table.
Is there an opportunity to delete the whole bookmark containing a table and set a new one in which should sometimes be another table?
Thanks
Reply With Quote
  #2  
Old 06-26-2015, 11:43 PM
gmayor's Avatar
gmayor gmayor is offline Bookmarks Windows 7 64bit Bookmarks Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The short answer is yes. You can set a range to the bookmark range, then replace the range content with your new content, then reapply the bookmark to the range. (See for example the FillBM function at http://www.gmayor.com/word_vba_examples.htm.) If you need more information then supply more information about what you are doing.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 06-27-2015, 12:12 AM
Schildkröte Schildkröte is offline Bookmarks Windows 8 Bookmarks Office 2013
Novice
Bookmarks
 
Join Date: Jun 2015
Posts: 6
Schildkröte is on a distinguished road
Default

Tanks for your answer. The problem is that all this functions only allow to insert a new text. I would like to insert a text and a table and both should be the new range for the bookmark.
Sorry, I could have described it more precisely.
Reply With Quote
  #4  
Old 06-27-2015, 12:58 AM
Schildkröte Schildkröte is offline Bookmarks Windows 8 Bookmarks Office 2013
Novice
Bookmarks
 
Join Date: Jun 2015
Posts: 6
Schildkröte is on a distinguished road
Default

Ok, I´m going to describe my problem more detailed:

I have a dropdown with 3 entries (Hi, Hello, Welcome).
Then I have a bookmark in this form:

Some Text
-blank space as a bookmark-
Some Text

According to what I choose I want to change the content of the bookmark.
Hi: " "
Hello: newline & "Some new Text: " & newline
Welcome: newline & a table & newline

The first two cases are not too hard I think:

This is a sub to call a makro according to which entry is choosen:

Code:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)

Dim Text As String
Dim Empty As String
Text = vbCrLf & "Some new Text: " & vbCrLf
Empty = " "

    Select Case CC.Tag
    Case Is = "Tagname"
        Select Case CC.Range.Text
            Case Is = "Hi"
                Call ChangeText(Empty)
            Case Is = "Hello"
                Call ChangeText(Text)
            Case Is = "Welcome"
                Call ???
        End Select
    End Select

End Sub
This is a macro to change the text of a bookmark.

Code:
Sub ChangeText(ByVal Text As String)
Application.ScreenUpdating = False

    Selection.GoTo What:=wdGoToBookmark, Name:="TestBookMark"
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.InsertAfter Text
     
    ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
       Name:="TestBookMark"
    Selection.Collapse

Application.ScreenUpdating = True
End Sub
But in the third case I would like to insert a table (7 rows, 2 columns) and put some text in the first column. Then everything should be the range of my bookmark again.

Appreciating your help
Reply With Quote
  #5  
Old 06-27-2015, 02:52 AM
macropod's Avatar
macropod macropod is offline Bookmarks Windows 7 64bit Bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could try something based on:
Code:
Sub UpdateBookMark()
Dim BmkNm As String, NewTxt As String, BmkRng As Range, vUpdType As Variant, Tbl As Table
BmkNm = InputBox("Bookmark Name")
vUpdType = InputBox("Bookmark Update Kind" & vbCr & _
          "1. Predefined Text" & vbCr & _
          "2. A Table & Predefined Text" & vbCr & _
          "3. Variable Text")
With ActiveDocument
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    With BmkRng
      Select Case vUpdType
      Case 1
        .Text = "Predefined Text"
      Case 2
        .Delete
        .InsertBefore vbCr & vbCr
        Set Tbl = .Tables.Add(Range:=.Characters.Last, Numrows:=5, NumColumns:=5)
        With Tbl
          'do stuff with the table
        End With
        .Start = Tbl.Range.Start - 1
        .Characters.First.InsertBefore "Predefined Start Text"
        .End = Tbl.Range.End + 1
        .Characters.Last.InsertAfter "Predefined End Text"
      Case 3
        .Text = InputBox("New Bookmark Text")
      Case Else
        GoTo ErrExit
      End Select
    End With
    .Bookmarks.Add BmkNm, BmkRng
  Else
    MsgBox "Bookmark: " & BmkNm & " not found."
  End If
End With
ErrExit:
Set BmkRng = Nothing: Set Tbl = Nothing
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 06-28-2015, 05:59 AM
Schildkröte Schildkröte is offline Bookmarks Windows 8 Bookmarks Office 2013
Novice
Bookmarks
 
Join Date: Jun 2015
Posts: 6
Schildkröte is on a distinguished road
Default

Thanks a lot for your solution.
I tried it and it works but I have some problems with a new line.
Heres my modified code:

Code:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim BmkNm As String, BmkRng As Range, Tbl As Table

With ActiveDocument
  If .Bookmarks.Exists("Text") Then
    Set BmkRng = .Bookmarks("Text").Range
    With BmkRng
      Select Case CC.Tag
      Case Is = "Tagname"
      Select Case CC.Range.Text
      Case Is = "Hi"
        .Delete
        .Text = ""
      Case Is = "Welcome"
        .Delete
        .InsertBefore " " & vbCr
        Set Tbl = .Tables.Add(Range:=.Characters.Last, NumRows:=7, NumColumns:= _
        2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed)
        With Tbl
          .Cell(1, 1).Range.Text = "Text 1"
          .Cell(2, 1).Range.Text = "Text 2"
          .Cell(2, 1).Range.Font.Bold = True
          .Cell(3, 1).Range.Text = "Text 3"
          .Cell(4, 1).Range.Text = "Text 4"
          .Cell(5, 1).Range.Text = "Text 5"
          .Cell(6, 1).Range.Text = "Text 6"
          .Cell(7, 1).Range.Text = "Text 7"
        End With
        .Start = Tbl.Range.Start - 2
        .Characters.First.InsertBefore " "
        .End = Tbl.Range.End + 1
        .Characters.Last.InsertAfter vbCr & "  "
      Case Is = "Hello"
        .Delete
        .Text = vbCr & "  Some new Text: " & vbCr
      Case Else
        GoTo ErrExit
      End Select
      End Select
    End With
    .Bookmarks.Add "Text", BmkRng
  Else
    MsgBox "Bookmark not found."
  End If
End With
ErrExit:
Set BmkRng = Nothing: Set Tbl = Nothing
End Sub
When I choose "Welcome" everything works fine. But when I then choose "Hi" or "Hello", there is an empty line which doesnt belong to the bookmark.
I suppose this is because I add a new line after the end of the bookmark in "Welcome".
Is there a way to extend the bookmark so that this new line is deleted when I choose something else?

By the way: In my document there are two blank spaces at the beginning of each line..

Thanks for helping
Reply With Quote
  #7  
Old 06-28-2015, 07:09 AM
gmayor's Avatar
gmayor gmayor is offline Bookmarks Windows 7 64bit Bookmarks Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Assuming you have a complete paragraph bookmarked, the following should work:
Code:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim BmkNm As String, BmkRng As Range, Tbl As Table

    With ActiveDocument
        If .Bookmarks.Exists("Text") Then
            Set BmkRng = .Bookmarks("Text").Range
            With BmkRng
                Select Case CC.Tag
                    Case Is = "Tagname"
                        Select Case CC.Range.Text
                            Case Is = "Hi"
                                .Text = ""
                            Case Is = "Welcome"
                                Set Tbl = ActiveDocument.Tables.Add(Range:=BmkRng, NumRows:=7, NumColumns:= _
                                                                    2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
                                                                    wdAutoFitFixed)
                                With Tbl
                                    .Cell(1, 1).Range.Text = "Text 1"
                                    .Cell(2, 1).Range.Text = "Text 2"
                                    .Cell(2, 1).Range.Font.Bold = True
                                    .Cell(3, 1).Range.Text = "Text 3"
                                    .Cell(4, 1).Range.Text = "Text 4"
                                    .Cell(5, 1).Range.Text = "Text 5"
                                    .Cell(6, 1).Range.Text = "Text 6"
                                    .Cell(7, 1).Range.Text = "Text 7"
                                End With
                                Tbl.Range.Next.InsertBefore vbCr
                                .End = Tbl.Range.End + 1
                            Case Is = "Hello"
                                .Text = "  Some new Text: " & vbCr
                            Case Else
                                GoTo ErrExit
                        End Select
                End Select
            End With
            .Bookmarks.Add "Text", BmkRng
        Else
            MsgBox "Bookmark not found."
        End If
    End With
ErrExit:
    Set BmkRng = Nothing: Set Tbl = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 06-28-2015, 07:49 AM
Schildkröte Schildkröte is offline Bookmarks Windows 8 Bookmarks Office 2013
Novice
Bookmarks
 
Join Date: Jun 2015
Posts: 6
Schildkröte is on a distinguished road
Default

Hi,
Thanks for your answer. It does work very good.
But when I cklick on "Hello" and afterwards on "Welcome" the line with " Some new Text:" isnt´t deleted.
When i first click on "Hi" and then on "Welcome" it works and also for "Hi" and "Hello".
But the text is marked as bookmark.
Do you also have a solution for this?
Reply With Quote
  #9  
Old 06-28-2015, 11:13 AM
Schildkröte Schildkröte is offline Bookmarks Windows 8 Bookmarks Office 2013
Novice
Bookmarks
 
Join Date: Jun 2015
Posts: 6
Schildkröte is on a distinguished road
Default

Sorry, I fixed it.
Once again: Thank you very much for your help
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bookmarks Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM
Bookmarks TOC hyperlinks and bookmarks. GregCFII Word 9 03-02-2014 06:43 AM
Can't See Bookmarks bobmayo Word 21 06-04-2013 07:37 AM
Bookmarks Table of Bookmarks Niy Word 3 03-28-2012 12:18 AM
Bookmarks for a PDF? Ownaholic Word 0 10-30-2010 12:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:54 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft