Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-15
8
Medium Priority
?
309 Views
Last Modified: 2014-01-15
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

0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 15

Expert Comment

by:DrTribos
ID: 39782072
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

0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 39782101
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
0
 

Author Comment

by:AndreasHermle
ID: 39782117
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:AndreasHermle
ID: 39782170
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
0
 

Author Comment

by:AndreasHermle
ID: 39782185
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
0
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 39782211
Hi,

I always forget MoveStart

Sub AddBookmark_Date()

Dim myDoc As Document
Dim rngBmk As Range
Set myDoc = Documents("Project_Sheet.docm")
Set rngBmk = ActiveDocument.Paragraphs(3).Range
rngBmk.MoveStart wdCharacter, InStr(1, rngBmk, ":") + 1
rngBmk.MoveEnd wdCharacter, -1
myDoc.Bookmarks.Add Name:="Date", Range:=rngBmk
myDoc.ActiveWindow.View.ShowBookmarks = True
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:AndreasHermle
ID: 39783441
Hi Rgonzo, good  job, thank you very much for your professional help.

Regards, Andreas
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39783987
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  :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question