Adding a bookmark, inserting text inside the bookmark

D

David Thielen

Hi;

How can I add a bookmark and then call InsertXML() so that what I
insert is inserted inside the bookmark. If the bookmark Range is left
with Start == End the InsertXML goes after the bookmark.

I can set the bookmark Range.End = Start + 1, then insert at
Range.Start, but then the bookmark includes 1 character after what I
inserted.

??? - thanks - dave

david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
J

Ji Zhou

Hello Dave,
I can set the bookmark Range.End = Start + 1, then insert at
Range.Start, but then the bookmark includes 1 character after what I
inserted.

I think we can choose this pattern. After the XML part is inserted, we can
adjust the bookmark by sub 1 on its End property.

For example, I call the following codes to insert a bookmark at Range(1,1)
with XML content.

Sub Test()

Dim r As Range
Set r = ActiveDocument.Range(1, 2)
ActiveDocument.Bookmarks.Add "locate", r

strXML = "<?xml version=""1.0""?><abc:books xmlns:abc=""urn:books"" " & _
"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
"xsi:schemaLocation=""urn:books books.xsd""><book>" & _
"<author>Matt Hink</author><title>Migration Paths of the Red " & _
"Breasted Robin</title><genre>non-fiction</genre>" & _
"<price>29.95</price><pub_date>2006-05-01</pub_date>" & _
"<abstract>You see them in the spring outside your windows. " & _
"You hear their lovely songs wafting in the warm spring air. " & _
"Now follow their path as they migrate to warmer climes in the fall, " &
_
"and then back to your back yard in the
spring.</abstract></book></abc:books>"

ActiveDocument.Range(1, 1).InsertXML (strXML)
ActiveDocument.Bookmarks("locate").End =
ActiveDocument.Bookmarks("locate").End - 1

End Sub

Could you please give it a test and let me know if this works for you :)

Have a nice day!


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support
 
D

David Thielen

Hi;

That did not work for an empty document or an empty cell. But I then
tried adding a space the my initial Range.Text, inserting the XML,
then setting the range to the last character of the bookmark and
setting that Range.Text =""

That seems to work.

thanks - dave


Hello Dave,


I think we can choose this pattern. After the XML part is inserted, we can
adjust the bookmark by sub 1 on its End property.

For example, I call the following codes to insert a bookmark at Range(1,1)
with XML content.

Sub Test()

Dim r As Range
Set r = ActiveDocument.Range(1, 2)
ActiveDocument.Bookmarks.Add "locate", r

strXML = "<?xml version=""1.0""?><abc:books xmlns:abc=""urn:books"" " & _
"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
"xsi:schemaLocation=""urn:books books.xsd""><book>" & _
"<author>Matt Hink</author><title>Migration Paths of the Red " & _
"Breasted Robin</title><genre>non-fiction</genre>" & _
"<price>29.95</price><pub_date>2006-05-01</pub_date>" & _
"<abstract>You see them in the spring outside your windows. " & _
"You hear their lovely songs wafting in the warm spring air. " & _
"Now follow their path as they migrate to warmer climes in the fall, " &
_
"and then back to your back yard in the
spring.</abstract></book></abc:books>"

ActiveDocument.Range(1, 1).InsertXML (strXML)
ActiveDocument.Bookmarks("locate").End =
ActiveDocument.Bookmarks("locate").End - 1

End Sub

Could you please give it a test and let me know if this works for you :)

Have a nice day!


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top