Avatar of Andreas Hermle
Andreas Hermle
Flag for Germany asked on

Create bookmark marking just part of third paragraph of, using VBA

Dear Experts:

below macro adds a bookmark named 'Date' to the third paragraph of a specific document.

I would like to get this macro tweaked so ...
... that not the whole third paragraph gets bookmarked but just part of it, i.e. just the date (27. Sept. 2013) should be bookmarked without the paragraph mark

Date of Evaluation: 27. Sept. 2013

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Sub AddBookmark_Date()

Dim myDoc As Document
 
Set myDoc = Documents("Project_Sheet.docm")
 myDoc.Bookmarks.Add Name:="Date", _
 Range:=myDoc.Paragraphs(3).Range
 myDoc.ActiveWindow.View.ShowBookmarks = True
End Sub

Open in new window

Microsoft Word

Avatar of undefined
Last Comment
DrTribos

8/22/2022 - Mon
DrTribos

Something like this might work - sorry have not been able to test
Sub AddBookmark_Date()

Dim myDoc As Document
dim rngBookMark as Range 

set rngBookMark = myDoc.Paragraphs(3).Range
rngBookMark.End = rngBookMark.End - 1

Set myDoc = Documents("Project_Sheet.docm")
 myDoc.Bookmarks.Add Name:="Date",  rngBookMark 
myDoc.ActiveWindow.View.ShowBookmarks = True
End Sub

Open in new window

Rgonzo1971

Hi,

pls try

Sub AddBookmark_Date()

Dim myDoc As Document
Dim rngBookMark As Range
Set myDoc = Documents("Project_Sheet.docm")
Set rngBmk = ActiveDocument.Paragraphs(3).Range
rngBmk.Start = InStr(1, rngBmk, ":") + rngBmk.Start + 1

myDoc.Bookmarks.Add Name:="Date", Range:=rngBmk
myDoc.ActiveWindow.View.ShowBookmarks = True
End Sub

Open in new window

Regards
Andreas Hermle

ASKER
Hi DrTribos,

thank you very much for your quick help. I am afraid to tell you that the macro does not work, it is throwing an error message. Moreover I just would like the date bookmarked, i.e. in the above example '27. Sept. 2013

Thank you

Regards, Andreas
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Andreas Hermle

ASKER
Hi Rgonzo,

thank you very much for your great help. We are almost there, the paragraph mark should not be part of the bookmark, i.e. the range should be reduced by just one character at the end. I guess this is possible.

Thank you very much in advance. Regards, Andreas
Andreas Hermle

ASKER
Hi Rgonzo,

found out myself how to tweak your code so that the paragraph mark is not included in the range:

Line 8
rngBmk.MoveEnd wdCharacter, -1

So no need for you to get back with a feedback.

Thank you so much for your valuable help.

Regards, Andreas
ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi Rgonzo, good  job, thank you very much for your professional help.

Regards, Andreas
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DrTribos

Hi Andreas,

Thanks for your fast response - sorry I could not provide a tested solution in time.
 
Just for completeness here is the same solution with errors removed. Reading on small screen and missed the date only requirement (so took more notice of the title).

At any rate I was curious as to where my errors were and hope the comments help others better understand the code as well.
Sub AddBookmark_Date2()
Dim myDoc As Document
Dim rngBookMark As Range
Set myDoc = Documents("Project_Sheet.docm")  ' Had to move this line up as my doc is used below
Set rngBookMark = myDoc.Paragraphs(3).Range
rngBookMark.End = rngBookMark.End - 1
myDoc.Bookmarks.Add "Date", rngBookMark  ' Used lazy syntax here (my untested offering was inconsistent)
myDoc.ActiveWindow.View.ShowBookmarks = True
End Sub

Open in new window

Obviously I'm still missing the bit that finds the date.

Nice work Rgonzo  :-)