template documents and word ranges.

X

XmlAdoNewbie

Hi All,
I am creating a document builder in vba and word 2002. When i run my
code from a file with the extension .doc everything is fine, however i
have just created a template from the same document and my code blows
up when i try and select a range. I get a 'run time error 4608 - value
out of range' message. The code it blows up on is the following.

Public Sub returnText(ByVal childnode As IXMLDOMNode)

Dim temp As String

Selection.GoTo what:=wdGoToBookmark, Name:="A"
Selection.InsertFile FileName:=path & childnode.text

BookMarkStart = ActiveDocument.Bookmarks("A").Start
BookMarkEnd = Len(ActiveDocument.Content) - 1
Set myRange = ThisDocument.Range(Start:=BookMarkStart,
End:=BookMarkEnd)

myRange.Copy

End Sub

it doesn't like the line
Set myRange = ThisDocument.Range(Start:=BookMarkStart,
End:=BookMarkEnd)
bookmarkstart and bookmarkend seem to be proper values and like i said
before it works when the file has a .doc extension

Is there any reason this may be happening?? Any help will be greatly
appreciated!!

Thanks so much in advance!
Erin
 
J

JGM

Hi XmlAd....

My guess is that when you ran the code from the module as part of the doc,
the ThisDocument module referred the open document from which the code was
beign executed, i.e., the doc. Whereas when you ran the code from a dot
document, ThisDocument refers to the attached template where the code is
contained. so, when you try to set a range with
Set myRange = ThisDocument.Range(Start:=BookMarkStart,
End:=BookMarkEnd)
it generates an error because the "ThisDocument" is not opened.

Try changing it to
Set myRange = ActiveDocument.Range(Start:=BookMarkStart, End:=BookMarkEnd)
or something similar...

HTH
Cheers.
 
D

Doug Robbins - Word MVP

Hi Erin,

I would guess that the switch from using ActiveDocument to ThisDocument
could be causing the problem.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
X

XmlAdoNewbie

Thank you so much both of you!!
You were both absolutely right, once i changed thisDocument to
activedocument all was well once again.
Your help is much appreciated!
Erin
 

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