chrisatwork
asked on
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
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
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.
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.
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.Dupli cate
'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
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.Dupli
'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
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, that clarified it for me
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.