Inserting a graphic at a bookmark with a Userform

K

Karen Sigel

Hi-

I'm working in Office XP / Word 2003. I'm trying to create a simple
userform that brings up a dialog box when a new document is created.
The dialog box uses three option buttons to ask the user to choose one
of three logos for insertion into the header (where the bookmark is
located). Right now, the dialog box has only the option buttons and
Clear / OK / Cancel command buttons. How do I code the option buttons
to insert their own specific logo at the bookmark? I know I have to
tell each of them to look for its own file; I just can't figure out
how to do that. And once I do, how do I make sure the graphic is
sized and proportioned correctly?

My code's pretty thin right now, so I'm not including it, but if it
would help someone tell me how toproceed, I'll gladly post it.

Thanks in advance for any help-
Karen
 
J

Jay Freedman

Hi-

I'm working in Office XP / Word 2003. I'm trying to create a simple
userform that brings up a dialog box when a new document is created.
The dialog box uses three option buttons to ask the user to choose one
of three logos for insertion into the header (where the bookmark is
located). Right now, the dialog box has only the option buttons and
Clear / OK / Cancel command buttons. How do I code the option buttons
to insert their own specific logo at the bookmark? I know I have to
tell each of them to look for its own file; I just can't figure out
how to do that. And once I do, how do I make sure the graphic is
sized and proportioned correctly?

My code's pretty thin right now, so I'm not including it, but if it
would help someone tell me how toproceed, I'll gladly post it.

Thanks in advance for any help-
Karen

You don't actually tell the option buttons to do anything. The code you need goes in the _Click procedure of the OK button. That code should figure out which one of the option buttons has the
selection dot, and then insert the appropriate logo for that choice.

Let's assume you inserted a bookmark named bkLogo in the header, you named the three option buttons opt1, opt2, and opt3, and the corresponding logos are saved as AutoText entries with the names
logo1, logo2, and logo3. Then the following code will do what you want:

Private Sub btnOK_Click()
Dim ATname As String

'if there's already a logo, remove it and recreate the bookmark
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
If .Shapes.Count Then
.Shapes(1).Delete
End If
ActiveDocument.Bookmarks.Add Name:="bkLogo", Range:=.Range
End With

If opt1.Value = True Then
ATname = "logo1"
End If

If opt2.Value = True Then
ATname = "logo2"
End If

If opt3.Value = True Then
ATname = "logo3"
End If

ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
Where:=ActiveDocument.Bookmarks("bkLogo").Range, _
RichText:=True

Me.Hide
End Sub


You would also want the following to make sure the first option button is selected when the form opens:

Private Sub UserForm_Initialize()
opt1.Value = True
End Sub


This could be made "smarter", depending on what you want to provide for the users. For example, you could use a document variable to store the name of the button that was chosen; and the _Initialize
procedure can be told to read the variable and turn on that option button. That way, if the form is run again for an existing document, the form starts with the same button chosen.
 
K

Karen Sigel

You don't actually tell the option buttons to do anything. The code you need goes in the _Click procedure of the OK button. That code should figure out which one of the option buttons has the
selection dot, and then insert the appropriate logo for that choice.

Let's assume you inserted a bookmark named bkLogo in the header, you named the three option buttons opt1, opt2, and opt3, and the corresponding logos are saved as AutoText entries with the names
logo1, logo2, and logo3. Then the following code will do what you want:

Private Sub btnOK_Click()
    Dim ATname As String

    'if there's already a logo, remove it and recreate the bookmark
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
        If .Shapes.Count Then
            .Shapes(1).Delete
        End If
        ActiveDocument.Bookmarks.Add Name:="bkLogo", Range:=.Range
    End With

    If opt1.Value = True Then
        ATname = "logo1"
    End If

    If opt2.Value = True Then
        ATname = "logo2"
    End If

    If opt3.Value = True Then
        ATname = "logo3"
    End If

    ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
       Where:=ActiveDocument.Bookmarks("bkLogo").Range, _
       RichText:=True

    Me.Hide
End Sub

You would also want the following to make sure the first option button isselected when the form opens:

Private Sub UserForm_Initialize()
    opt1.Value = True
End Sub

This could be made "smarter", depending on what you want to provide for the users. For example, you could use a document variable to store the name of the button that was chosen; and the _Initialize
procedure can be told to read the variable and turn on that option button.. That way, if the form is run again for an existing document, the form starts with the same button chosen.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Jay-

Thank you so much. I was doing a WAY more complicated thing to get
what I wanted. A few questions:

When I compile the project, I get an error: "Sub or Function not
defined at Shapes(1).Delete"; when I comment out that whole procedure,
it compiles just fine. (I'm not sure I need it anyway, but I'm curious
to know why it doesn't work, and I don't think it would be a bad thing
to include.)

Can I insert an option for "no logo?" How? I have an option button
for it, I just don't know what to do with it.

How does my userform dialog box connect to this? Because when I try
to use the template, it doesn't appear.

Thanks again-
Karen
 
K

Karen Sigel

Jay-

Thank you so much.  I was doing a WAY more complicated thing to get
what I wanted.  A few questions:

When I compile the project, I get an error: "Sub or Function not
defined at Shapes(1).Delete"; when I comment out that whole procedure,
it compiles just fine. (I'm not sure I need it anyway, but I'm curious
to know why it doesn't work, and I don't think it would be a bad thing
to include.)

Can I insert an option for "no logo?"  How?  I have an option button
for it, I just don't know what to do with it.

How does my userform dialog box connect to this?  Because when I try
to use the template, it doesn't appear.

Thanks again-
Karen- Hide quoted text -

- Show quoted text -

And... never mind that third question: I figured out how to initialize
the form. It's showing up now, but when I choose a logo option,
nothing's happening. Here's what I have at this point. My userform
dialog box asks "Which company is sending this letter?" and offers
four option buttons, one for each of three companies and one for no
logo, and it has OK, Clear, and Cancel buttons.

Private Sub btnOK_Click()

Dim ATname As String
'if there's already a logo, remove it and recreate the bookmark
'With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
'If .Shapes.Count Then
'Shapes(1).Delete
'End If

'ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
'End With

If optBHlogo.Value = True Then
ATname = "BHlogo"
End If

If optBHBAlogo.Value = True Then
ATname = "BHBAlogo"
End If

If optBASCOlogo.Value = True Then
ATname = "BASCOlogo"
End If

ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True
Me.Hide
End Sub


Private Sub Cancel_Click()
Unload Me
End Sub

Private Sub Clear_Click()
optBASCOlogo.Value = True
optBHBAlogo.Value = True
optBHlogo.Value = True
End Sub

Private Sub OK_Click()
Application.ScreenUpdating = True
Unload Me
End Sub

Private Sub UserForm_Click()
End Sub


I'm hoping whatever's wrong is something minor. (I still have
questions about the code that's commented out and how to make the "no
logo" option work.)

Thanks for your assistance!
Karen
 
J

Jay Freedman

I'm working in Office XP / Word 2003. I'm trying to create a simple
userform that brings up a dialog box when a new document is created.
The dialog box uses three option buttons to ask the user to choose one
of three logos for insertion into the header (where the bookmark is
located).  Right now, the dialog box has only the option buttons and
Clear / OK / Cancel command buttons.  How do I code the option buttons
to insert their own specific logo at the bookmark?  I know I have to
tell each of them to look for its own file; I just can't figure out
how to do that.  And once I do, how do I make sure the graphic is
sized and proportioned correctly?
My code's pretty thin right now, so I'm not including it, but if it
would help someone tell me how toproceed, I'll gladly post it.
Thanks in advance for any help-
Karen
You don't actually tell the option buttons to do anything. The code you need goes in the _Click procedure of the OK button. That code should figure out which one of the option buttons has the
selection dot, and then insert the appropriate logo for that choice.
Let's assume you inserted a bookmark named bkLogo in the header, you named the three option buttons opt1, opt2, and opt3, and the corresponding logos are saved as AutoText entries with the names
logo1, logo2, and logo3. Then the following code will do what you want:
[snip]
--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- Hide quoted text -
- Show quoted text -

Jay-

Thank you so much.  I was doing a WAY more complicated thing to get
what I wanted.  A few questions:

When I compile the project, I get an error: "Sub or Function not
defined at Shapes(1).Delete"; when I comment out that whole procedure,
it compiles just fine. (I'm not sure I need it anyway, but I'm curious
to know why it doesn't work, and I don't think it would be a bad thing
to include.)

Can I insert an option for "no logo?"  How?  I have an option button
for it, I just don't know what to do with it.

How does my userform dialog box connect to this?  Because when I try
to use the template, it doesn't appear.

Thanks again-
Karen- Hide quoted text -

- Show quoted text -

And... never mind that third question: I figured out how to initialize
the form. It's showing up now, but when I choose a logo option,
nothing's happening. Here's what I have at this point. My userform
dialog box asks "Which company is sending this letter?" and offers
four option buttons, one for each of three companies and one for no
logo, and it has OK, Clear, and Cancel buttons.

Private Sub btnOK_Click()

Dim ATname As String
'if there's already a logo, remove it and recreate the bookmark
'With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
'If .Shapes.Count Then
'Shapes(1).Delete
'End If

'ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
'End With

If optBHlogo.Value = True Then
ATname = "BHlogo"
End If

If optBHBAlogo.Value = True Then
ATname = "BHBAlogo"
End If

If optBASCOlogo.Value = True Then
ATname = "BASCOlogo"
End If

ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True
Me.Hide
End Sub


Private Sub Cancel_Click()
Unload Me
End Sub

Private Sub Clear_Click()
optBASCOlogo.Value = True
optBHBAlogo.Value = True
optBHlogo.Value = True
End Sub

Private Sub OK_Click()
Application.ScreenUpdating = True
Unload Me
End Sub

Private Sub UserForm_Click()
End Sub


I'm hoping whatever's wrong is something minor. (I still have
questions about the code that's commented out and how to make the "no
logo" option work.)

Thanks for your assistance!
Karen

The reason you aren't getting any result is easy to explain and easier to fix. The names of the procedures have to match the names of the buttons (or other controls) on the form. Evidently the name of
the OK button on your form is just "OK", while I named mine "btnOK". So when you click the OK button, your form runs the procedure named OK_Click, and the procedure named btnOK_Click never runs. The
fix is to change the procedure name from btnOK_Click to OK_Click, and delete the existing OK_Click procedure.

You don't need the statement Application.ScreenUpdating = True unless something in your code previously set Application.ScreenUpdating = False.

There's a bit of an issue around when to use Me.Hide to shut down the form and when to use Unload Me. It depends on how you write the macro that calls up the userform. To keep things simple, just
replace the Me.Hide at the end of the btnOK_Click procedure with Unload Me.

The other problem was a little harder to spot, but also easy to fix. In the line Shapes(1).Delete, you need to insert a period (dot) before the word Shapes. The meaning of this involves the With
statement that precedes it: It's equivalent to the full expression
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Delete
where the dot means "this Shapes object is a property of the header object determined by the earlier part of the expression".
 
K

Karen Sigel

Hi-
I'm working in Office XP / Word 2003. I'm trying to create a simple
userform that brings up a dialog box when a new document is created..
The dialog box uses three option buttons to ask the user to choose one
of three logos for insertion into the header (where the bookmark is
located).  Right now, the dialog box has only the option buttons and
Clear / OK / Cancel command buttons.  How do I code the option buttons
to insert their own specific logo at the bookmark?  I know I haveto
tell each of them to look for its own file; I just can't figure out
how to do that.  And once I do, how do I make sure the graphic is
sized and proportioned correctly?
My code's pretty thin right now, so I'm not including it, but if it
would help someone tell me how toproceed, I'll gladly post it.
Thanks in advance for any help-
Karen
You don't actually tell the option buttons to do anything. The code you need goes in the _Click procedure of the OK button. That code should figure out which one of the option buttons has the
selection dot, and then insert the appropriate logo for that choice.
Let's assume you inserted a bookmark named bkLogo in the header, younamed the three option buttons opt1, opt2, and opt3, and the correspondinglogos are saved as AutoText entries with the names
logo1, logo2, and logo3. Then the following code will do what you want:
[snip]
--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- Hide quoted text -
- Show quoted text -
Jay-
Thank you so much.  I was doing a WAY more complicated thing to get
what I wanted.  A few questions:
When I compile the project, I get an error: "Sub or Function not
defined at Shapes(1).Delete"; when I comment out that whole procedure,
it compiles just fine. (I'm not sure I need it anyway, but I'm curious
to know why it doesn't work, and I don't think it would be a bad thing
to include.)
Can I insert an option for "no logo?"  How?  I have an option button
for it, I just don't know what to do with it.
How does my userform dialog box connect to this?  Because when I try
to use the template, it doesn't appear.
Thanks again-
Karen- Hide quoted text -
- Show quoted text -
And... never mind that third question: I figured out how to initialize
the form.  It's showing up now, but when I choose a logo option,
nothing's happening.  Here's what I have at this point.  My userform
dialog box asks "Which company is sending this letter?" and offers
four option buttons, one for each of three companies and one for no
logo, and it has OK, Clear, and Cancel buttons.
Private Sub btnOK_Click()
   Dim ATname As String
   'if there's already a logo, remove it and recreate the bookmark
   'With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
       'If .Shapes.Count Then
       'Shapes(1).Delete
   'End If
   'ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
   'End With
   If optBHlogo.Value = True Then
       ATname = "BHlogo"
   End If
   If optBHBAlogo.Value = True Then
       ATname = "BHBAlogo"
   End If
   If optBASCOlogo.Value = True Then
       ATname = "BASCOlogo"
   End If
   ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range,_
       RichText:=True
   Me.Hide
End Sub
Private Sub Cancel_Click()
   Unload Me
End Sub
Private Sub Clear_Click()
   optBASCOlogo.Value = True
   optBHBAlogo.Value = True
   optBHlogo.Value = True
End Sub
Private Sub OK_Click()
   Application.ScreenUpdating = True
   Unload Me
End Sub
Private Sub UserForm_Click()
End Sub
I'm hoping whatever's wrong is something minor.  (I still have
questions about the code that's commented out and how to make the "no
logo" option work.)
Thanks for your assistance!
Karen

The reason you aren't getting any result is easy to explain and easier tofix. The names of the procedures have to match the names of the buttons (or other controls) on the form. Evidently the name of
the OK button on your form is just "OK", while I named mine "btnOK". So when you click the OK button, your form runs the procedure named OK_Click, and the procedure named btnOK_Click never runs. The
fix is to change the procedure name from btnOK_Click to OK_Click, and delete the existing OK_Click procedure.

You don't need the statement  Application.ScreenUpdating = True unless something in your code previously set  Application.ScreenUpdating = False.

There's a bit of an issue around when to use Me.Hide to shut down the form and when to use Unload Me. It depends on how you write the macro that calls up the userform. To keep things simple, just
replace the Me.Hide at the end of the btnOK_Click procedure with Unload Me.

The other problem was a little harder to spot, but also easy to fix. In the line Shapes(1).Delete, you need to insert a period (dot) before the wordShapes. The meaning of this involves the With
statement that precedes it: It's equivalent to the full expression
   ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Delete
where the dot means "this Shapes object is a property of the header object determined by the earlier part of the expression".

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

It works! Brilliant! (and so simple!) Thank you!

My only remaining issue is with the "no logo" option. When I test it
by creating a new document from the template and selecting that
option, the debugger gives me this error:

"Runtime error '5941': The requested member of the collection does
not exist."

and lands me here in the code:

ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert
_
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True

I have no idea if that's really where the error is or not (partly
because I'm not clear what the code is saying). I think the code to
not insert a logo would be something like

If optNoLogo.Value = true then
ATName = ""
End If

and when I insert that just after the other option subs, , the project
compiles just fine, but when I test it, I get the same error and land
in the same section of the code. Again, everything works just fine
when I select any of the other options (that have logos as autotext
entries). I'm lost.

Again, thank you so much for your help with this.
Karen
 
J

Jay Freedman

[snip]
Private Sub btnOK_Click()
   Dim ATname As String
   'if there's already a logo, remove it and recreate the bookmark
   'With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
       'If .Shapes.Count Then
       'Shapes(1).Delete
   'End If
   'ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
   'End With
   If optBHlogo.Value = True Then
       ATname = "BHlogo"
   End If
   If optBHBAlogo.Value = True Then
       ATname = "BHBAlogo"
   End If
   If optBASCOlogo.Value = True Then
       ATname = "BASCOlogo"
   End If
   ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
       RichText:=True
   Me.Hide
End Sub
Private Sub Cancel_Click()
   Unload Me
End Sub
Private Sub Clear_Click()
   optBASCOlogo.Value = True
   optBHBAlogo.Value = True
   optBHlogo.Value = True
End Sub
Private Sub OK_Click()
   Application.ScreenUpdating = True
   Unload Me
End Sub
Private Sub UserForm_Click()
End Sub
I'm hoping whatever's wrong is something minor.  (I still have
questions about the code that's commented out and how to make the "no
logo" option work.)
Thanks for your assistance!
Karen

The reason you aren't getting any result is easy to explain and easier to fix. The names of the procedures have to match the names of the buttons (or other controls) on the form. Evidently the name of
the OK button on your form is just "OK", while I named mine "btnOK". So when you click the OK button, your form runs the procedure named OK_Click, and the procedure named btnOK_Click never runs. The
fix is to change the procedure name from btnOK_Click to OK_Click, and delete the existing OK_Click procedure.

You don't need the statement  Application.ScreenUpdating = True unless something in your code previously set  Application.ScreenUpdating = False.

There's a bit of an issue around when to use Me.Hide to shut down the form and when to use Unload Me. It depends on how you write the macro that calls up the userform. To keep things simple, just
replace the Me.Hide at the end of the btnOK_Click procedure with Unload Me.

The other problem was a little harder to spot, but also easy to fix. In the line Shapes(1).Delete, you need to insert a period (dot) before the word Shapes. The meaning of this involves the With
statement that precedes it: It's equivalent to the full expression
   ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Delete
where the dot means "this Shapes object is a property of the header object determined by the earlier part of the expression".

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- Hide quoted text -

- Show quoted text -

It works! Brilliant! (and so simple!) Thank you!

My only remaining issue is with the "no logo" option. When I test it
by creating a new document from the template and selecting that
option, the debugger gives me this error:

"Runtime error '5941': The requested member of the collection does
not exist."

and lands me here in the code:

ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert
_
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True

I have no idea if that's really where the error is or not (partly
because I'm not clear what the code is saying). I think the code to
not insert a logo would be something like

If optNoLogo.Value = true then
ATName = ""
End If

and when I insert that just after the other option subs, , the project
compiles just fine, but when I test it, I get the same error and land
in the same section of the code. Again, everything works just fine
when I select any of the other options (that have logos as autotext
entries). I'm lost.

Again, thank you so much for your help with this.
Karen

We're getting there. :)

You can't tell Word to insert an AutoText entry named "" because there is no such thing (hence the error message). There are two ways to handle this:

(1) Create a dummy AutoText entry that contains a space character, give it a valid name, and use that name in the "nologo" case.

OR

(2) Continue to set ATName = "" for the "nologo" case, but use a construction like this to avoid trying to insert any AutoText in that case:

If ATName <> "" Then
   ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
       RichText:=True
End If
 
K

Karen Sigel

[snip]
Private Sub btnOK_Click()
   Dim ATname As String
   'if there's already a logo, remove it and recreate the bookmark
   'With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
       'If .Shapes.Count Then
       'Shapes(1).Delete
   'End If
   'ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
   'End With
   If optBHlogo.Value = True Then
       ATname = "BHlogo"
   End If
   If optBHBAlogo.Value = True Then
       ATname = "BHBAlogo"
   End If
   If optBASCOlogo.Value = True Then
       ATname = "BASCOlogo"
   End If
   ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
       RichText:=True
   Me.Hide
End Sub
Private Sub Cancel_Click()
   Unload Me
End Sub
Private Sub Clear_Click()
   optBASCOlogo.Value = True
   optBHBAlogo.Value = True
   optBHlogo.Value = True
End Sub
Private Sub OK_Click()
   Application.ScreenUpdating = True
   Unload Me
End Sub
Private Sub UserForm_Click()
End Sub
I'm hoping whatever's wrong is something minor.  (I still have
questions about the code that's commented out and how to make the "no
logo" option work.)
Thanks for your assistance!
Karen
The reason you aren't getting any result is easy to explain and easierto fix. The names of the procedures have to match the names of the buttons(or other controls) on the form. Evidently the name of
the OK button on your form is just "OK", while I named mine "btnOK". So when you click the OK button, your form runs the procedure named OK_Click, and the procedure named btnOK_Click never runs. The
fix is to change the procedure name from btnOK_Click to OK_Click, and delete the existing OK_Click procedure.
You don't need the statement  Application.ScreenUpdating = True unless something in your code previously set  Application.ScreenUpdating = False.
There's a bit of an issue around when to use Me.Hide to shut down the form and when to use Unload Me. It depends on how you write the macro that calls up the userform. To keep things simple, just
replace the Me.Hide at the end of the btnOK_Click procedure with Unload Me.
The other problem was a little harder to spot, but also easy to fix. In the line Shapes(1).Delete, you need to insert a period (dot) before the word Shapes. The meaning of this involves the With
statement that precedes it: It's equivalent to the full expression
   ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Delete
where the dot means "this Shapes object is a property of the header object determined by the earlier part of the expression".
--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- Hide quoted text -
- Show quoted text -
It works!  Brilliant! (and so simple!)  Thank you!
My only remaining issue is with the "no logo" option.  When I test it
by creating a new document from the template and selecting that
option, the debugger gives me this error:
"Runtime error '5941': The requested member of the collection does
not exist."
and lands me here in the code:
       ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert
_
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range,_
       RichText:=True
I have no idea if that's really where the error is or not (partly
because I'm not clear what the code is saying).  I think the code to
not insert a logo would be something like
    If optNoLogo.Value = true then
          ATName = ""
    End If
and when I insert that just after the other option subs, , the project
compiles just fine, but when I test it, I get the same error and land
in the same section of the code.  Again, everything works just fine
when I select any of the other options (that have logos as autotext
entries).  I'm lost.
Again, thank you so much for your help with this.
Karen

We're getting there. :)

You can't tell Word to insert an AutoText entry named "" because there isno such thing (hence the error message). There are two ways to handle this:

(1) Create a dummy AutoText entry that contains a space character, give it a valid name, and use that name in the "nologo" case.

OR

(2) Continue to set ATName = "" for the "nologo" case, but use a construction like this to avoid trying to insert any AutoText in that case:

If ATName <> "" Then
    ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
        Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
        RichText:=True
End If

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Jay-

Thanks; I'm still getting the exact same runtime error, though, AND
now it's not working at all. Just so we can both see exactly what's
going on, here's my code to date:

(this is in Forms\ChooseLogo):


Private Sub btnOK_Click()

Dim ATname As String
'if there's already a logo, remove it and recreate the bookmark
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
If .Shapes.Count Then
.Shapes(1).Delete
End If

ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
End With

If optBHlogo.Value = True Then
ATname = "BHlogo"
End If

If optBHBAlogo.Value = True Then
ATname = "BHBAlogo"
End If

If optBASCOlogo.Value = True Then
ATname = "BASCOlogo"
End If

If ATname <> "" Then
ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True
End If

Unload Me

End Sub

Private Sub UserForm_Initialize()
optBHlogo.Value = True
End Sub

Private Sub btnCancel_Click()
Unload Me
End Sub

Private Sub btnClear_Click()

optBASCOlogo.Value = True
optBHBAlogo.Value = True
optBHlogo.Value = True

End Sub

Private Sub OK_Click()
Unload Me
End Sub


(this is in Microsoft Word Objects\This Document):

Private Sub Document_New()

chooselogo.Show

End Sub

(this is in Modules\New Macro):

Sub autonew()
'
' autonew Macro
' Macro created 8/11/2010 by klsigel
'
chooselogo.Show
End Sub

I'm hoping it's something simple again, but I just can't see it. The
error lands me in that same piece of code every time. What does that
statement do, exactly?

Thanks-
Karen
 
J

Jay Freedman

[snip]
It works!  Brilliant! (and so simple!)  Thank you!
My only remaining issue is with the "no logo" option.  When I test it
by creating a new document from the template and selecting that
option, the debugger gives me this error:
"Runtime error '5941': The requested member of the collection does
not exist."
and lands me here in the code:
       ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert
_
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
       RichText:=True
I have no idea if that's really where the error is or not (partly
because I'm not clear what the code is saying).  I think the code to
not insert a logo would be something like
    If optNoLogo.Value = true then
          ATName = ""
    End If
and when I insert that just after the other option subs, , the project
compiles just fine, but when I test it, I get the same error and land
in the same section of the code.  Again, everything works just fine
when I select any of the other options (that have logos as autotext
entries).  I'm lost.
Again, thank you so much for your help with this.
Karen

We're getting there. :)

You can't tell Word to insert an AutoText entry named "" because there is no such thing (hence the error message). There are two ways to handle this:

(1) Create a dummy AutoText entry that contains a space character, give it a valid name, and use that name in the "nologo" case.

OR

(2) Continue to set ATName = "" for the "nologo" case, but use a construction like this to avoid trying to insert any AutoText in that case:

If ATName <> "" Then
    ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
        Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
        RichText:=True
End If

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- Hide quoted text -

- Show quoted text -

Jay-

Thanks; I'm still getting the exact same runtime error, though, AND
now it's not working at all. Just so we can both see exactly what's
going on, here's my code to date:

(this is in Forms\ChooseLogo):


Private Sub btnOK_Click()

Dim ATname As String
'if there's already a logo, remove it and recreate the bookmark
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
If .Shapes.Count Then
.Shapes(1).Delete
End If

ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
End With

If optBHlogo.Value = True Then
ATname = "BHlogo"
End If

If optBHBAlogo.Value = True Then
ATname = "BHBAlogo"
End If

If optBASCOlogo.Value = True Then
ATname = "BASCOlogo"
End If

If ATname <> "" Then
ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True
End If

Unload Me

End Sub

Private Sub UserForm_Initialize()
optBHlogo.Value = True
End Sub

Private Sub btnCancel_Click()
Unload Me
End Sub

Private Sub btnClear_Click()

optBASCOlogo.Value = True
optBHBAlogo.Value = True
optBHlogo.Value = True

End Sub

Private Sub OK_Click()
Unload Me
End Sub


(this is in Microsoft Word Objects\This Document):

Private Sub Document_New()

chooselogo.Show

End Sub

(this is in Modules\New Macro):

Sub autonew()
'
' autonew Macro
' Macro created 8/11/2010 by klsigel
'
chooselogo.Show
End Sub

I'm hoping it's something simple again, but I just can't see it. The
error lands me in that same piece of code every time. What does that
statement do, exactly?

Thanks-
Karen

When you say "now it's not working at all" does that mean you're getting the error when you choose the other option buttons too? If so, make sure that the AutoText entries named "BHlogo", "BHBAlogo",
and "BASCOlogo" do exist in the template that contains the macros, which is also the template being used to create the new document. Also make sure the bookmark named "bkinsertlogo" exists in the
header of the template (and therefore exists in the header of the new document).

Here's what the statement is supposed to do, bit by bit:

- It looks in the template attached to the current document -- since the document was just created from the template containing the code, that's the attached template. (After a document is created
from a template, it's possible to attach a different template to it, but that's not what's going on here.) That's the ActiveDocument.AttachedTemplate part of the statement.

- Then it looks into the template's collection of AutoText entries; that's the ActiveDocument.AttachedTemplate.AutoTextEntries part. It tries to locate the specific entry with the name equal to the
string in the variable ATname. That's the ActiveDocument.AttachedTemplate(ATname) part of the statement. If there is no entry with that name, you'll get the Runtime error '5941'. If it does exist,
then its Insert method (some code internal to Word) gets called. That code needs to know where to insert the entry.

- The underscore at the end of the line says that the next line is part of the same statement, so move on to that...

- It looks in the Bookmarks collection of the current document for one named "bkinsertlogo". That's the ActiveDocument.Bookmarks("bkinsertlogo") part. If there is no bookmark with that name, you get
the Runtime error '5941' there. If the bookmark does exist, then its Range (more or less its location in the document) is assigned to the Where parameter of the Insert method, so the logo will be
inserted at the bookmark.

- Again, there's an underscore to say to go on to the next line.

- Finally, the RichText parameter of the Insert method is assigned the value True, which means to insert a graphic (or formatted text, depending on what's in the entry) instead of plain text.

***
One more unrelated item: You need either the Document_New procedure OR the AutoNew macro, but not both. Take a look at http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm.
 
K

Karen Sigel

[snip]
It works!  Brilliant! (and so simple!)  Thank you!
My only remaining issue is with the "no logo" option.  When I test it
by creating a new document from the template and selecting that
option, the debugger gives me this error:
"Runtime error '5941': The requested member of the collection does
not exist."
and lands me here in the code:
       ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert
_
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
       RichText:=True
I have no idea if that's really where the error is or not (partly
because I'm not clear what the code is saying).  I think the code to
not insert a logo would be something like
    If optNoLogo.Value = true then
          ATName = ""
    End If
and when I insert that just after the other option subs, , the project
compiles just fine, but when I test it, I get the same error and land
in the same section of the code.  Again, everything works just fine
when I select any of the other options (that have logos as autotext
entries).  I'm lost.
Again, thank you so much for your help with this.
Karen
We're getting there. :)
You can't tell Word to insert an AutoText entry named "" because thereis no such thing (hence the error message). There are two ways to handle this:
(1) Create a dummy AutoText entry that contains a space character, give it a valid name, and use that name in the "nologo" case.
OR
(2) Continue to set ATName = "" for the "nologo" case, but use a construction like this to avoid trying to insert any AutoText in that case:
If ATName <> "" Then
    ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
        Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
        RichText:=True
End If
--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- Hide quoted text -
- Show quoted text -

Thanks; I'm still getting the exact same runtime error, though, AND
now it's not working at all.  Just so we can both see exactly what's
going on, here's my code to date:
(this is in Forms\ChooseLogo):
Private Sub btnOK_Click()
   Dim ATname As String
   'if there's already a logo, remove it and recreate the bookmark
   With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
       If .Shapes.Count Then
       .Shapes(1).Delete
   End If
   ActiveDocument.Bookmarks.Add Name:="bkinsertlogo", Range:=.Range
   End With
   If optBHlogo.Value = True Then
       ATname = "BHlogo"
   End If
   If optBHBAlogo.Value = True Then
       ATname = "BHBAlogo"
   End If
   If optBASCOlogo.Value = True Then
       ATname = "BASCOlogo"
   End If
If ATname <> "" Then
   ActiveDocument.AttachedTemplate.AutoTextEntries(ATname).Insert _
       Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range,_
       RichText:=True
End If
   Unload Me
Private Sub UserForm_Initialize()
   optBHlogo.Value = True
End Sub
Private Sub btnCancel_Click()
   Unload Me
End Sub
Private Sub btnClear_Click()
   optBASCOlogo.Value = True
   optBHBAlogo.Value = True
   optBHlogo.Value = True
Private Sub OK_Click()
      Unload Me
End Sub
(this is in Microsoft Word Objects\This Document):
Private Sub Document_New()

End Sub
(this is in Modules\New Macro):
Sub autonew()
'
' autonew Macro
' Macro created 8/11/2010 by klsigel
'
chooselogo.Show
End Sub
I'm hoping it's something simple again, but I just can't see it.  The
error lands me in that same piece of code every time.  What does that
statement do, exactly?
Thanks-
Karen

When you say "now it's not working at all" does that mean you're getting the error when you choose the other option buttons too? If so, make sure that the AutoText entries named "BHlogo", "BHBAlogo",
and "BASCOlogo" do exist in the template that contains the macros, which is also the template being used to create the new document. Also make sure the bookmark named "bkinsertlogo" exists in the
header of the template (and therefore exists in the header of the new document).

Here's what the statement is supposed to do, bit by bit:

- It looks in the template attached to the current document -- since the document was just created from the template containing the code, that's theattached template. (After a document is created
from a template, it's possible to attach a different template to it, but that's not what's going on here.) That's the ActiveDocument.AttachedTemplate part of the statement.

- Then it looks into the template's collection of AutoText entries; that's the ActiveDocument.AttachedTemplate.AutoTextEntries part. It tries to locate the specific entry with the name equal to the
string in the variable ATname. That's the ActiveDocument.AttachedTemplate(ATname) part of the statement. If there is no entry with that name, you'llget the Runtime error '5941'. If it does exist,
then its Insert method (some code internal to Word) gets called. That code needs to know where to insert the entry.

- The underscore at the end of the line says that the next line is part of the same statement, so move on to that...

- It looks in the Bookmarks collection of the current document for one named "bkinsertlogo". That's the ActiveDocument.Bookmarks("bkinsertlogo") part. If there is no bookmark with that name, you get
the Runtime error '5941' there. If the bookmark does exist, then its Range (more or less its location in the document) is assigned to the Where parameter of the Insert method, so the logo will be
inserted at the bookmark.

- Again, there's an underscore to say to go on to the next line.

- Finally, the RichText parameter of the Insert method is assigned the value True, which means to insert a graphic (or formatted text, depending on what's in the entry) instead of plain text.

***
One more unrelated item: You need either the Document_New procedure OR the AutoNew macro, but not both. Take a look athttp://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Thanks. I understand much better now what's supposed to happen.

And yes, that's exactly what I mean; none of the option buttons were
working, nor were the Clear, Cancel, or OK buttons, and I got the same
runtime error for all of them. I double-checked to make sure all the
names matched and that the correct AutoText entries were in the
template, and as far as I could see, they were. Still didn't work.

All that said, IT salvaged an an early version of the template for me.
It works perfectly. I have no idea what the difference is; it looks
identical to the one that doesn't. Oddly enough, it didn't appear to
have a "bkinsertlogo" bookmark at all. I reinserted it, and it works
with or without. So, that problem is gone (forever, I hope).

This is my last issue: I need to insert a graphic that has to be
constant in all versions of the document. I added it to the footer,
laid out behind the text, and the form runs just fine, but the image
gets deleted. I tried making it a watermark, but the same thing
happens, plus the image placement is off and I can't control it. I
suspect it's your "if there's already a logo, remove it and recreate
the bookmark" code that's doing it, but maybe I'm wrong. If I comment
out that section, I'm back to the 5941 runtime error. If I add the
image as a background, I'm back to the 5941 runtime error. I hate
that error!

Thank you so much for your help-
Karen
 
J

Jay Freedman

Thanks. I understand much better now what's supposed to happen.

And yes, that's exactly what I mean; none of the option buttons were
working, nor were the Clear, Cancel, or OK buttons, and I got the same
runtime error for all of them. I double-checked to make sure all the
names matched and that the correct AutoText entries were in the
template, and as far as I could see, they were. Still didn't work.

All that said, IT salvaged an an early version of the template for me.
It works perfectly. I have no idea what the difference is; it looks
identical to the one that doesn't. Oddly enough, it didn't appear to
have a "bkinsertlogo" bookmark at all. I reinserted it, and it works
with or without. So, that problem is gone (forever, I hope).

This is my last issue: I need to insert a graphic that has to be
constant in all versions of the document. I added it to the footer,
laid out behind the text, and the form runs just fine, but the image
gets deleted. I tried making it a watermark, but the same thing
happens, plus the image placement is off and I can't control it. I
suspect it's your "if there's already a logo, remove it and recreate
the bookmark" code that's doing it, but maybe I'm wrong. If I comment
out that section, I'm back to the 5941 runtime error. If I add the
image as a background, I'm back to the 5941 runtime error. I hate
that error!

Thank you so much for your help-
Karen

Well, you stepped on a land mine there. For reasons known only to the Microsoft designers, there is no way in VBA to refer separately to shapes in the header and not those in the footer. According to
the help topic on the Shapes property, "When applied to a HeaderFooter object, the Shapes property returns all the Shape objects found in all the headers and footers in the document." So yes, deleting
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1) will delete whatever happens to be the first shape in the (headers + footers) combination. Because a watermark is actually just a
particular shape anchored in the header, that could go too.

The only reasonable way to handle this requirement is to start by deleting *all* the shapes in the headers and footers, and then adding the ones that you need -- namely the chosen logo plus the
background. First set up the background image (place it and format it where and how you want it to appear) and make it into another AutoText entry in the template -- let's say you name it "bkgrd".
Then revise the code something like this:

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
' remove all non-inline graphics
While .Shapes.Count
.Shapes(1).Delete
Wend
ActiveDocument.Bookmarks.Add Name:="bkLogo", Range:=.Range
End With

and then later, after inserting the logo, add this

ActiveDocument.AttachedTemplate.AutoTextEntries("bkgrd").Insert _
Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
RichText:=True
 
K

Karen Sigel

Well, you stepped on a land mine there. For reasons known only to the Microsoft designers, there is no way in VBA to refer separately to shapes in the header and not those in the footer. According to
the help topic on the Shapes property, "When applied to a HeaderFooter object, the Shapes property returns all the Shape objects found in all the headers and footers in the document." So yes, deleting
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1) will delete whatever happens to be the first shape in the (headers + footers) combination. Because a watermark is actually just a
particular shape anchored in the header, that could go too.

The only reasonable way to handle this requirement is to start by deleting *all* the shapes in the headers and footers, and then adding the ones that you need -- namely the chosen logo plus the
background. First set up the background image (place it and format it where and how you want it to appear) and make it into another AutoText entry in the template -- let's say you name it "bkgrd".
Then revise the code something like this:

    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
        ' remove all non-inline graphics
        While .Shapes.Count
            .Shapes(1).Delete
        Wend
        ActiveDocument.Bookmarks.Add Name:="bkLogo", Range:=.Range
    End With

and then later, after inserting the logo, add this

    ActiveDocument.AttachedTemplate.AutoTextEntries("bkgrd").Insert _
        Where:=ActiveDocument.Bookmarks("bkinsertlogo").Range, _
        RichText:=True

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

I was afraid of that. Can I use code to insert the background any
other way than inline with the text?

Right now we've basically got an image that's 8 1/2 x 11, so it can be
inserted as either a background or as a graphic behind the text, at
100% and centered vertically and horizontally. It's the only way I
can figure to get the "real" image (which is really a graphic element
that's supposed to be sized and placed in a particular location and
orientation) to land where it's supposed to every time, and I can't
make an image into an autotext entry unless it's inline, right? When
I try to place it as a background in the template, I get the 5941
error (again!), so that's out, I fear.

I recorded a macro to insert a graphic the way I want it. Can I set it
up to run automatically after the chooselogo process? If I run the
userform and then the macro after the document's created, that seems
to work, but I'm nervous about how to incorporate them into one
another and/or into the template (the code the macro recrder wrote is
long and complicated.)

Thanks-
Karen
 
K

Karen Sigel

I was afraid of that.  Can I use code to insert the background any
other way than inline with the text?

Right now we've basically got an image that's 8 1/2 x 11, so it can be
inserted as either a background or as a graphic behind the text, at
100% and centered vertically and horizontally.  It's the only way I
can figure to get the "real" image (which is really a graphic element
that's supposed to be sized and placed in a particular location and
orientation) to land where it's supposed to every time, and I can't
make an image into an autotext entry unless it's inline, right?  When
I try to place it as a background in the template, I get the 5941
error (again!), so that's out, I fear.

I recorded a macro to insert a graphic the way I want it. Can I set it
up to run automatically after the chooselogo process? If I run the
userform and then the macro after the document's created, that seems
to work, but I'm nervous about how to incorporate them into one
another and/or into the template (the code the macro recrder wrote is
long and complicated.)

Thanks-
Karen- Hide quoted text -

- Show quoted text -

One more thing; I have the template (it's letterhead) set up with
"different first page" so the logo doesn't show up on the second and
proceeding pages. I insert a page break, put the bookmark in the
first page header, delete it from the second page, save it, then
delete the page break and save it again. The bookmark stays in the
first page header. If I insert a page break in the template, it's not
on the second page. Everything looks just right.

When I create a document from the template, though, there's no logo on
the first page, but if I insert a page break, there it is on the
second! What the hey?!? I create merge templates for a database
application we use and this method works for them; why not for this?

I can't quite believe this is getting so complicated; I thought it
would be a pretty simple thing to do.

Thanks again-
Karen
 
J

Jay Freedman

I was afraid of that. Can I use code to insert the background any
other way than inline with the text?

Right now we've basically got an image that's 8 1/2 x 11, so it can be
inserted as either a background or as a graphic behind the text, at
100% and centered vertically and horizontally. It's the only way I
can figure to get the "real" image (which is really a graphic element
that's supposed to be sized and placed in a particular location and
orientation) to land where it's supposed to every time, and I can't
make an image into an autotext entry unless it's inline, right? When
I try to place it as a background in the template, I get the 5941
error (again!), so that's out, I fear.

No, that is NOT right.

- Start with the cursor in the header, and insert the "real" image at whatever size it should be.

- Right-click the image and choose Format Picture.

- Click the Layout tab of the Format Picture dialog. Click the Behind Text button. Then click the Advanced button.

- In the Advanced Layout dialog, set either the alignment (e.g., Centered relative to Page) or the absolute position (e.g., 1.4" to the right of Margin) for the horizontal and vertical settings.

- You may also use the settings on the Picture tab of the Format Picture dialog to increase the brightness and reduce the contrast so the image is washed out like a watermark.

- After OKing the dialogs, with the image selected, press Alt+F3 and give the AutoText entry a name.

When the cursor is in any header and you insert that AutoText entry, it will be positioned exactly as you set it up.
I recorded a macro to insert a graphic the way I want it. Can I set it
up to run automatically after the chooselogo process? If I run the
userform and then the macro after the document's created, that seems
to work, but I'm nervous about how to incorporate them into one
another and/or into the template (the code the macro recrder wrote is
long and complicated.)

Thanks-
Karen

Yes, it's getting far too complicated, and as a result it's likely to become difficult to maintain, if not just plain unstable. So let me propose something different:

Make a separate template for each letterhead, with just one logo and the background image permanently in place and with no macro code. Then create an empty template that contains only code; instead of
choosing and inserting different logos, it should just create a new document based on the chosen template. You do this (in the OK button's Click procedure) with code like

Documents.Add Template:="myLogo1.dot"

but the part in quotes depends on which option button was selected.

Then distribute all the templates to each user. The template with the code can go in the Startup folder so it's always available, and it can also place an item on a toolbar and/or menu to start up the
userform. The other templates should go in the User Templates folder.
 
J

Jay Freedman

One more thing; I have the template (it's letterhead) set up with
"different first page" so the logo doesn't show up on the second and
proceeding pages. I insert a page break, put the bookmark in the
first page header, delete it from the second page, save it, then
delete the page break and save it again. The bookmark stays in the
first page header. If I insert a page break in the template, it's not
on the second page. Everything looks just right.

When I create a document from the template, though, there's no logo on
the first page, but if I insert a page break, there it is on the
second! What the hey?!? I create merge templates for a database
application we use and this method works for them; why not for this?

I can't quite believe this is getting so complicated; I thought it
would be a pretty simple thing to do.

Thanks again-
Karen

A bit of background first:

Every section in a document has three headers and three footers. If the "Different first page" and "Different odd and even" options are turned off, you only see the "primary" header and footer. If the
"Different odd and even" option is turned on, then the primary header/footer become the odd-page header/footer, and a separate even-page headerr/footer appear. If the "Different first page" option is
turned on, the primary header/footer become the page-2-and-following header/footer (unless both options are turned on, and then the primary becomes the "odd starting with page 3").

The code is putting the logo in the primary header because I wrote ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) as the location for the bookmark. You can change that to
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage) to get the behavior you want.

But before you get involved with this, read my suggestion in my other reply.
 
J

Jay Freedman

No, that is NOT right.

- Start with the cursor in the header, and insert the "real" image at whatever size it should be.

- Right-click the image and choose Format Picture.

- Click the Layout tab of the Format Picture dialog. Click the Behind Text button. Then click the Advanced button.

- In the Advanced Layout dialog, set either the alignment (e.g., Centered relative to Page) or the absolute position (e.g., 1.4" to the right of Margin) for the horizontal and vertical settings.

- You may also use the settings on the Picture tab of the Format Picture dialog to increase the brightness and reduce the contrast so the image is washed out like a watermark.

- After OKing the dialogs, with the image selected, press Alt+F3 and give the AutoText entry a name.

When the cursor is in any header and you insert that AutoText entry, it will be positioned exactly as you set it up.


Yes, it's getting far too complicated, and as a result it's likely to become difficult to maintain, if not just plain unstable. So let me propose something different:

Make a separate template for each letterhead, with just one logo and the background image permanently in place and with no macro code. Then create an empty template that contains only code; instead of
choosing and inserting different logos, it should just create a new document based on the chosen template. You do this (in the OK button's Click procedure) with code like

Documents.Add Template:="myLogo1.dot"

but the part in quotes depends on which option button was selected.

Then distribute all the templates to each user. The template with the code can go in the Startup folder so it's always available, and it can also place an item on a toolbar and/or menu to start up the
userform. The other templates should go in the User Templates folder.

I should explain what I mean by "the Startup folder" and the "User Templates" folder. Read http://word.mvps.org/faqs/customization/WhatTemplatesStore.htm for that.
 
K

Karen Sigel

I should explain what I mean by "the Startup folder" and the "User Templates" folder. Readhttp://word.mvps.org/faqs/customization/WhatTemplatesStore..htmfor that.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Holy mackerel.

Welllll... I love your solution; it's perfect, and it's simple, and I
could do it, but there's a reason I was doing what I was trying to do,
and I'm not sure how to reconcile the two concepts.

My company is composed of three firms, hence the three logos. The
letterhead has to show one of the logos, depending on which firm is
corresponding. The footer of the letterhead contains one of 15 (right
now, though it changes) office addresses. While they typically use
their “home” office, any one of our 300 employees at any of the
offices could need to send a letter from any of the firms and/or
offices. So in order to do what you're suggesting, I'm pretty sure
I'd need 45 templates (15 addresses x 3 logos each). IT says that's
doable, but one of the things we're trying to do here is streamline
and simplify a thing that right now has the most complicated,
ridiculously poorly formatted and conceived templates you can possibly
imagine, so creating 45 templates isn't really an appropriate
solution.

My original idea was to have code that also offered an address option,
but that seemed like there'd be too much room for glitches, so we
thought of having just 15 templates (a lot, but not 45!), and the only
thing a user would have to do is select the correct location template
and answer the question "Which firm is sending this letter?" Some
people only have BW printers and work with pre-printed letterhead,
which is why I need a "no logo" option. It seems like such a simple
thing. I had no idea it would get so convoluted.

If I understand your most recent comments, you're suggesting that I
really need to rethink the whole thing. Maybe adding an address
option is the right way to go after all? I’d want each office to have
its own location be the pre-selected default, but I think that part
would be easy, based on what you’ve shown me so far. My only concern
would be what happens when an existing office moves or a new one
opens.

Given my situation, what would *you* do?

Thanks-
Karen
 
K

Karen Sigel

I should explain what I mean by "the Startup folder" and the "User Templates" folder. Readhttp://word.mvps.org/faqs/customization/WhatTemplatesStore..htmfor that.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

After talking it over with one of our IT folks, we're thinking a
better way to go with this may be to do as you said and have four
templates with the graphics preinserted and formatted, and create a
userform with the office locations as the selections, even though that
will require close maintenance across all the offices when changes
happen. What do you think?

Karen
 
J

Jay Freedman

After talking it over with one of our IT folks, we're thinking a
better way to go with this may be to do as you said and have four
templates with the graphics preinserted and formatted, and create a
userform with the office locations as the selections, even though that
will require close maintenance across all the offices when changes
happen. What do you think?

Karen

I do think this is the way to go.

To simplify the maintenance of the list of addresses that may change, store them in a separate file -- this could be a Word document, a plain text document, or an Excel spreadsheet -- and distribute
that file along with the templates. The article at http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm describes how to load data into a list box in a userform; the sections on multi-column lists
and external data sources are of special interest here.

The idea is that each of the four templates displays the same userform from the Document_New macro, and the userform gets the data from the external file to fill the list box. The user chooses the
office's name, and the OK button's Click procedure puts the office's address into the document. When a new office is added or an existing one moves, you just have to distribute a revised data file;
nothing in the template has to change.

To have each office get its own address as the default, you can supply a little text file that just contains that office's name. The userform's Initialize procedure can read that file and use the
contents to select the office's name in the list box as the userform is about to be displayed.
 
K

Karen Sigel

I do think this is the way to go.

To simplify the maintenance of the list of addresses that may change, store them in a separate file -- this could be a Word document, a plain text document, or an Excel spreadsheet -- and distribute
that file along with the templates. The article athttp://gregmaxey.mvps.org/Populate_UserForm_ListBox.htmdescribes how to load data into a list box in a userform; the sections on multi-column lists
and external data sources are of special interest here.

The idea is that each of the four templates displays the same userform from the Document_New macro, and the userform gets the data from the externalfile to fill the list box. The user chooses the
office's name, and the OK button's Click procedure puts the office's address into the document. When a new office is added or an existing one moves,you just have to distribute a revised data file;
nothing in the template has to change.

To have each office get its own address as the default, you can supply a little text file that just contains that office's name. The userform's Initialize procedure can read that file and use the
contents to select the office's name in the list box as the userform is about to be displayed.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Jay-

Thanks for all your help. It took a long time to get to it, but I
think this will work - and I know where to come if I need help!

Karen
 

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