automating word 2003 from vb2010 express - beginner question

Just started with vb 2010 express and want to learn to automate word.

Am able to create a new document from a specific word template, populate bookmarks in the template manually and add some text - basic stuff, just following and modifying example code.

Need to understand how to locate and move the insertion point to a specific place after populating the document - can't find anything specific to guide me.

Still trying to get selections & ranges clear in my head.

Pointers to online and/or hardcopy (preferrably UK style) documentation would be appreciated.   Have beginners books on VBA & VB2008 express.

Thanks

Christopher

chrisatworkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
Hi Christopher,

Instead of showing you any code, I'll tell you how I do interop with VB.Net.
It all boils down to recording a macro from Word/Excel and then to look at the macro.
Then putting the code into VB.Net interop directly.
The same functions and members are all there.
GrahamSkanRetiredCommented:
A Range is a point in or a contiguous portion of a document. It has a start point represented by the .Start property and an end point, similarly defined. They can be the same. You can define and use several range object variables at the same time in your code.

There is only one Selection at a time in any instance of a Word Application. It represents the Range that the author is currently editing, i.e., the insertion point if it has zero length. Because of that, recorded macros always use the Selection object.

There are several techniques to move the insertion point. There are Move methods, such as MoveStart and MoveEnd. The Selection object also has a few like Move Up and MoveLeft which a Range object doesn't have because it works on the document, not on the display.


 

lwebberCommented:
If you want to locate a position in the document so that you can insert text there (or perform some other operation), you can use the Range object like this:

    Dim myRange As Range
    'Pretend that the active document contains "The quick brown fox jumps over the lazy dog. "
    'Every document has a Range property that includes all its contents. But
    'you can't move a document's range -- so we need a copy of it
    Set myRange = ActiveDocument.Range.Duplicate
    'When you execute the Find method of a range, if the sought-after
    'text is found, the range moves to span the found text.
    myRange.Find.Execute "fox"
    If myRange.Find.Found Then
        'Right now the range starts just before the "f" and ends just after the "x"
        myRange.InsertBefore "silly "
        'The document now reads "The quick brown silly fox jumps over the lazy dog."
        'You could use InsertAfter if you want. Check out the other range.Insert...
        'choices yourself.
    End If
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

chrisatworkAuthor Commented:
That seems to be ok if there is text in the document, but I want to return a position value of the edit cursor immediately after the document is created from a template (of which there could be many) - there could be no text, just the cursor after a header, or there could be bookmarks or fields.  Is there no property that gives a character/row position, or even an absolute page position in whatever measurement system is valid?
GrahamSkanRetiredCommented:
If you have just created the document, the cursor will be at the first selectable position in the document body (as opposed to header or footer). If there is no text, there is nowhere for bookmarks to be, except at the beginning/end of the document.

There are no line number or page number properties of a word document. Apart from objects such as tables, pictures and text boxes, there are just Section and Paragraph types that define the document. There are also some collections of  Range types such as Sentences, Words and Characters, but these have no special properties.

The reason is that Word is designed to format and print the document in the current environment, which largely depends on the current printer driver, so lines could fold and 'soft' page breaks be inserted in different places when the printer is changed.

If you do need to ensure that a line is alwas a line, you would have to make sure that it was short enough, and it is terminated with a new line character (Shift + Enter) or a paragraph mark (Enter)

This means that finding pages or lines by number must be done by calculation.

There is, for instance a GoTo method for the Selection and for Ranges, so not all work for Ranges. Or you can use the Selection.MoveDown.

If there are bookmarks, you can add text there:

objDoc.Bookmarks("MyBookmark").Range.Text = "Some text"

GoTo also works with bookmarks

Once inserted, most fields do not require programmatic attention. FormFields are the exception:

objDoc.FormFields("Text1").Result = "Some other text"


 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chrisatworkAuthor Commented:
Thanks, that clarified it for me
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.