How to insert text into bookmark

V

VK

I know well JavaScript, Java, Perl, but I never programmed in VBA (just
never needed)
Now I need to insert my custom date (translated in German) into my
document (US version of Word). I did the best I remembered from my
QBasic youth, but I stoke in 3 problems:

1. I cannot collect the final string from strings (month, weekday) and
numbers (day, year)
I'm getting "Type Mismatch". Is there any toString casting in VBA, or
how does it work here?

2. I cannot understand how to assign this string to a bookmark. I used
GoTo dialog for now.

3. How to execute the script onLoad (on opening document). I remember
there was some autoexec() function, but I did not found any refs on it
in the help.

Please help me save my time and nerves! :)

Sub DateEN2DE()

Dim CurrentYear, GermanMonth, CurrentDay, GermanWeekDay As Variant

GermanMonth = Array("dummy", "Januar", "Februar", "März", "April",
"Mai", "Juni", "Juli", "August", "September", "Oktober", "November",
"Dezember")
GermanWeekDay = Array("dummy", "Sonntag", "Montag", "Dienstag",
"Mittwoch", "Donnerstag", "Freitag", "Samstag")

Selection.GoTo What:=wdGoToBookmark, Name:="MyDate"
Selection.Text = GermanWeekDay(WeekDay(Date)) + ", " +
GermanMonth(Month(Date)) + " "
' and this I cannot add because of "Type Mismatch"
' + Selection.Add Day(Date) + ", " + Year(Date)
End Sub
 
M

Malcolm Smith

Hello, The following may help, though be warned that I am not in front of
Word myself right now so I am flying by the seat of my pants.

A few things to be concerned about there. First, why have you defined
your arrays as containing Variants, when they are strings.

Please define them as such
Dim CurrentYear as string
Dim GermanMonth as string
Dim CurrentDay as string
Dim GermanWeekDay As string

Secondly when you are concatenating strings do not use the '+' operator;
use the '&' instead. What the '+' is not only the concatenation but also
the addition operator (depending on type). Therefore to save problems
change all concatenations to use the ampersand.

The 'toString()' function is the Str$() command. If you look under
strings in the Help then this will get you going.


Right, the event in which to run code when the Document is open. In the
document or in the template look in the This_Document object in the
project tree and then in the IDE select 'Document' in the left hand drop
down above the editor client area (this is the object list) and then in
the right hand drop down you can select the event which you wish to trap.
You will be looking for Document_Open at the end of the day.

I have a few tutorials and sample code templates on my site which may
help.


Right, how to assign a string to a bookmark. You could do it the way that
you have done but I don't like to do it that way for a couple of reasons.

The first is that it's not using the objects neatly and, secondly, if the
bookmark is not visible then Word will redraw showing the bookmark as the
selection is always visible. If you have a large document with bookmarks
all over the place this will be slow and looks dreadful.

So, I do it this following way. Remember that the bookmark is an object
and it has a range object. So, assuming that you want to change the text
at the bookmark named 'bmkRecipientName' then the code will be:

ActiveDocument.Bookmarks("bmkRecipientName").Range.Text = "Ringo Starr"

Does this help a little?

As I say there are some sample code and templates on my site with
explanations which you may find useful.

Regards
- Malc
www.dragondrop.com
 
Top