RMac,
There is an apparent time lag between our postings and I hope my previous
post clears up some of your quesitons.
As for "oRng." I am a novice VBA user without any formal training and some
of my code probably appalls the purists. I pick up bits and pieces of what
the masters use but I can never keep it all straight. I do know that
"range" is an object and I often use a camelback convention in defining
variables (e..g, oRng as Range, strText as String, bGate as Boolean). You
might look up something like "VBA variable naming convention" in Google to
get more scoop on this.
I think in most intances using a range rather than a selection is more
efficient. I can't give you the technical reasons, but I generally feel
there is no reason to move the selection if you don't need to.
Say you have a bookmark in a Textbox called "TestBM"
You could write text after that bookmark simply with:
Sub ScratchMacro()
ActiveDocument.Bookmarks("TestBM").Range.InsertAfter "Your Text Here"
End Sub
That works, but if you run the code again you will have "Your Text HereYour
Text Here" in the textbox.
If you use this instead then you write your text "in" or enclosed by the
bookmark range. If you run the same code a second time the the text won't
repeat.
Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("TestBM").Range
oRng.Text = "Your Text Here"
ActiveDocument.Bookmarks.Add "TextBM", oRng
End Sub
Yes, as I illustrated in my last post since "TestBM" and "Your Text Here"
are passed as varibles in your code then they would be entered without the
"".
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RMac wrote:
> Thanks for your response, Greg;
>
> If you read my response to "sparklynic" you'll see that, by using a
> suggestion you posted to another question, I was able to identify and
> rename the textbox containing the sought for bookmark. I then
> programatically select the textbox; and using the selection.goto
> method, place the text or graphic where I need them.
>
> Having said that, I'm wondering if there is some inherent weakenss in
> the approach I used, which is:
>
> 1. Giving the document user a UserForm with a bunch of comboboxes
> and text entry controls that they manipulate to get the selections
> they want, then;
>
> 2. Taking those selections piece by piece--control by control, and
> using the I-Beam, placeholder type bookmarks and the selection.goto
> method, inserting the bits into the document.
>
> I have to say that it was a little annoying to find that the bookmarks
> inside the textboxes were invisible to my program until I set focus
> on the textbox. Since the textboxes are the last things that get
> filled, I haven't had to try to go back into the main document.
> Maybe that would be a problem. I'm not sure.
>
> Anyway, if you think the approach you suggest below would be more
> robust, I am interested in learning more about it. Can you suggest
> some MVP papers or other sources that discuss the basics of using
> .range in the way you are suggesting?
>
> By the way, I've seen oRng used as a variable in other bits of code
> examples. What does the 'o' stand for?
>
> One question on the code you sent below: In my usage, sMark is a
> string variable that carries the name of a bookmark that marks an
> insertion point in the document. "sBoxVal" is another string
> variable that contains the choice the user has selected from a
> UserForm control. sMark and sBoxVal change with each new bookmark to
> be filled and get passed to my subroutine "FillMark" (from my
> original post) to do the actual insertion. If I understand
> correctly, your code would look for a bookmark in the document
> literally called "sMark". In my case, should sMark and sBoxVal,
> having previously been dimensioned as string variables, be used
> without the quotes as follows?
>
> Sub Test()
> Dim oRng As Word.Range
> Set oRng = ActiveDocument.Bookmarks(sMark).Range
> oRng.Text = sBoxVal
> ActiveDocument.Bookmarks.Add sMark, oRng
> End Sub
>
> One other question: If the bookmark I'm trying to access is inside a
> textbox, would your code below be able to find it if the textbox does
> not have focus?
>
> Thanks again for your help.
>
> Bob
>
>
> "Greg Maxey" wrote:
>
>> I wouldn't try to use GoTo. Try something like:
>>
>> Sub Test()
>> Dim oRng As Word.Range
>> Set oRng = ActiveDocument.Bookmarks("sMark").Range
>> oRng.Text = "Your Text"
>> ActiveDocument.Bookmarks.Add "sMark", oRng
>> End Sub
>>
>>
>> --
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> Greg Maxey - Word MVP
>>
>> My web site http://gregmaxey.mvps.org
>> Word MVP web site http://word.mvps.org
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>>
>> RMac wrote:
>>> Using Word 2000.
>>>
>>> I am building a document that I want to use to generate written
>>> prescriptions for medicines. In order to control the exact
>>> placement of some text and graphics on the page, I would like to
>>> use textboxes. However, when I use the following code to do the
>>> insertion, the code can't find the bookmarks that are inside a
>>> textbox.
>>>
>>> In the instance of inserting text (for the final version of code,
>>> sBoxVal and sMark are set from controls on a UserForm):
>>>
>>> Sub Macro3()
>>> Dim sBoxVal As String, sMark As String
>>>
>>> sBoxVal = "SomeDrugName"
>>> sMark = "DrugName"
>>> Call FillMark(sMark, sBoxVal)
>>>
>>> End Sub
>>>
>>> Sub FillMark(sMark As String, sBoxVal As String)
>>> 'Go to bookmark and transfer the data.
>>> Selection.GoTo What:=wdGoToBookmark, Name:=sMark
>>> Selection.Range.Text = sBoxVal
>>> End Sub
>>>
>>> In the instance of inserting a graphic (in this case a .bmp of a
>>> signature that will vary depending on what doctor is writing the
>>> prescription):
>>>
>>> Sub Macro2()
>>>
>>> 'Go to Signature bookmark
>>> Selection.GoTo What:=wdGoToBookmark, Name:="Signature"
>>> 'Insert Signature
>>> Selection.InlineShapes.AddPicture FileName:= _
>>> ThisDocument.Path & "\JPM Signature.bmp" _
>>> , LinkToFile:=False, SaveWithDocument:=True
>>>
>>> End Sub
>>>
>>> Both of these approaches seem to work if I am using bookmarks in the
>>> main body of the document. I have discovered through fiddling
>>> around that if I click on the desired textbox before running the
>>> macro so that it has the focus before the macro runs the insertion
>>> works just fine. So, I think it might work if I could figure out
>>> how to set the focus on the correct textbox programatically.
>>>
>>> Thanks in advance