Insert file at variable position in document

A

A Kabak

I need to insert a word doc at a dynamically discovered
position within another word doc at runtime. I'm using
VB.NET to try to do this. In the base doc, I've got a
bookmark that shows where these inserted files should
start. I want to set my cursor one space after the end of
the bookmark and insert another file at that point, but
I'm having problems. I've tried lots of different ways to
do this; some of them are listed below:

'get the character position at the end of the bookmark
intStart = WordDoc.Bookmarks.Item(strBookmark).End

WordDoc.Range.SetRange(intStart, intStart + 1)
WordDoc.Range.Select()
WordDoc.Range.InsertFile "D:\Topeka\Output\BESubreportTest.
doc")

or

WordDoc.Characters.Item(intStart + 1).Select()
WordDoc.Application.Selection.InsertFile
("D:\Topeka\Output\BESubreportTest.doc")

Neither seems to work. In fact the first one selects the
entire document as the range so that when I run the
insert, it replaces the document with the inserted file.
Any ideas?
 
J

Jay Freedman

The expression WordDoc.Range *always" represents the range of the entire
document, and you can't change its Start and End values (although you don't
get an error if you try <shrug>). Instead, you need to declare a Range
object of your own, and manipulate that:

Dim MyRange As Word.Range
Set MyRange = WordDoc.BookmarksItem(strBookmark).Range
With MyRange
.Move (Unit=wdCharacter, Count=1)
.InsertFile ("D:\Topeka\Output\BESubreportTest.doc")
End With

You may need to define the constant wdCharacter = 1.

The .Move method collapses the range object to a single point at what was
its End, and then moves it one character to the right.
 

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