? Changing the text in a bookmark

J

JDupont

I am trying to update the text in a word document that has a bookmark called
Text2.
Then save the document as a new doc.
This is my code:

Dim wordApp As New Microsoft.Office.Interop.Word.Application
Dim newDoc As New Word.Document

newDoc = wordApp.Documents.Open("C:\mydoc")
'This is where my error occurs!
newDoc.Bookmarks.Item("Text2").Range.Text = "New Text"
'The error message is "Range can not be deleted!"

newDoc.SaveAs("c:\NewDoc")

Can you please help?
Thanks!
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?SkR1cG9udA==?=,
I am trying to update the text in a word document that has a bookmark called
Text2.
Then save the document as a new doc.
This is my code:

Dim wordApp As New Microsoft.Office.Interop.Word.Application
Dim newDoc As New Word.Document

newDoc = wordApp.Documents.Open("C:\mydoc")
'This is where my error occurs!
newDoc.Bookmarks.Item("Text2").Range.Text = "New Text"
'The error message is "Range can not be deleted!"

newDoc.SaveAs("c:\NewDoc")
Which version of Word, and from within which programming environment are you
working?

Is there anything special about this document? Is forms protection or another
kind of protection in force, for example? Is the bookmark located somewhere
other than the main document body? Does the bookmark contain anything except
"plain text"?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

JDupont

I have just tried new code but still the same trouble.

If newDoc.Bookmarks.Exists("Text2") Then
Dim rng As Word.Range = newDoc.Bookmarks("Text2").Range
rng.Text = "My New Text"
newDoc.Bookmarks.Add("Text2", DirectCast(rng, Word.Range))
End If

Please help.
 
J

JDupont

Version: Word 11
I also have VSTO installed?

First I move the document to another non secure location c:\myform.doc.
This did not fix it.

Protection is off!
No other security is on.

Then I looked for special issuses so I right click on the bookmark it pulls
up text form field Dialog box. The dialog box shows the bookmark = "Text2"

Could this be the problem?
 

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