How to change Picture in image control programmatically

T

Tony Strazzeri

Hi All,

A company I work for has recently changed their Brand image. This
involves changing document fonts and layouts as well as changes to
some template code and forms.

I have written a program to adjust the template layout. This program
will also go through the template's codemodules and update amendment
history and version numbers in the template's VBA code.

The templates also have userforms, all of which have a company logo in
an image control on the form.

I would like to ALSO automate this part. I thought it would be
possible to create a reference to the control and to change the image
with the program.

However, the Picture property of the image control seems to be an OLE
container and I have been unable to work out how to drop the new image
into this container.

Does anyone know how I can do this?

Cheers
TonyS.

Below is some of the code I use to change the codemodules.

Dim myComponent As VBComponent
Const FORM_NAME = "frmDocData"
Const RELMARKER = "Private Const Relver ="
Const MODULE_NAME = "modAmendmentHistory"
Const IMAGE_CONTROL_NAME = "imgClientLogo"

Dim I
Dim strLine As String
Dim currProj As VBProject

Dim myControl As Control
Dim strNewHistory As String

Dim CurrDateStamp
CurrDateStamp = "11/08/2004"

strNewHistory = "Private Const Relver = ""2K04.11""" _
& vbCr & "Private Const ModDate = """ & CurrDateStamp &
"""" _
& vbCr & "'#" & String(82, "*") _
& vbCr & "'# Version Date Who Details" _
& vbCr & "'# ====== ========== === " & String(54,
"=") _
& vbCr & "'# 2K04.11 " & CurrDateStamp _
& " TS Branding update."


Set currProj = ActiveDocument.VBProject

'This looks for the line beginning with RELMARKER and replaces it
and
'the following five lines with the new amendment history and
version info
'The amendment history looks something like this.
'Note the actual history does not have the comment char before the
Private Const below.
'Private Const Relver = "v1.01"
'Private Const ModDate = "29/02/2003"
''#**********************************************************************************
''# Version Date Who Details
''# ====== ========== ===
======================================================
''# v1.01 25/03/2003 TS User Updates.
''# v1.00 29/02/2003 TS New Template.

' This finds the appropriate spot and updates the amendment
history
Set myComponent = currProj.VBComponents(MODULE_NAME)

With myComponent.CodeModule
For I = 1 To .CountOfLines
strLine = Trim(.Lines(I, 1))
If Left(strLine, Len(RELMARKER)) = RELMARKER Then
.DeleteLines I, 5

.InsertLines I, strNewHistory
Exit For
End If
Next I
End With

'This hopes to change the logo image on the form
Set myComponent = currProj.VBComponents(FORM_NAME)

For I = 1 To myComponent.Designer.Controls.Count
With myComponent.Designer
If .Controls(I).Name = IMAGE_CONTROL_NAME Then
Set myControl = .Controls(I)

'**************************************************************
'This is where it fails because the .Picture is an OLE object.
'**************************************************************
'**************************************************************
'**************************************************************

' Its only properties are
' name Type
' ======= ==============
' Handle OLE_HANDLE
' Height OLE_YSIZE_HIMETRIC
' hPAL OLE_HANDLE
' Type Integer
' Width OLE_XSIZE_HIMETRIC
'**************************************************************
myControl.Picture = "newLogo" ''?????
Exit For
End If
End With
Next I
 
D

Doug Robbins

Hi Tony,

Use Image1.Picture = LoadPicture("drive:\Path\[filename].bmp")

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
T

Tony Strazzeri

Hi Doug,

Thanks for the reply. I don't think I made myself clear.

I believe the loadpicture method will load the specified graphic into
the image control at "runtime".

What I am trying to do is to permanently change the picture assigned
to the control by using a program. ie make the change as if I was
doing it manually at "design time".

I thought I was making this clear by the earlier changes which
directly modify module's procedure content. In the case of the
earlier example you can see that running it multiple times will add
those lined to the code. if this was at runtime it would only add the
lines once and they would be lost when the program ended.

Cheers
TonyS.

Doug Robbins said:
Hi Tony,

Use Image1.Picture = LoadPicture("drive:\Path\[filename].bmp")

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
Tony Strazzeri said:
Hi All,

A company I work for has recently changed their Brand image. This
involves changing document fonts and layouts as well as changes to
some template code and forms.

I have written a program to adjust the template layout. This program
will also go through the template's codemodules and update amendment
history and version numbers in the template's VBA code.

The templates also have userforms, all of which have a company logo in
an image control on the form.

I would like to ALSO automate this part. I thought it would be
possible to create a reference to the control and to change the image
with the program.

However, the Picture property of the image control seems to be an OLE
container and I have been unable to work out how to drop the new image
into this container.

Does anyone know how I can do this?

Cheers
TonyS.

Below is some of the code I use to change the codemodules.

Dim myComponent As VBComponent
Const FORM_NAME = "frmDocData"
Const RELMARKER = "Private Const Relver ="
Const MODULE_NAME = "modAmendmentHistory"
Const IMAGE_CONTROL_NAME = "imgClientLogo"

Dim I
Dim strLine As String
Dim currProj As VBProject

Dim myControl As Control
Dim strNewHistory As String

Dim CurrDateStamp
CurrDateStamp = "11/08/2004"

strNewHistory = "Private Const Relver = ""2K04.11""" _
& vbCr & "Private Const ModDate = """ & CurrDateStamp &
"""" _
& vbCr & "'#" & String(82, "*") _
& vbCr & "'# Version Date Who Details" _
& vbCr & "'# ====== ========== === " & String(54,
"=") _
& vbCr & "'# 2K04.11 " & CurrDateStamp _
& " TS Branding update."


Set currProj = ActiveDocument.VBProject

'This looks for the line beginning with RELMARKER and replaces it
and
'the following five lines with the new amendment history and
version info
'The amendment history looks something like this.
'Note the actual history does not have the comment char before the
Private Const below.
'Private Const Relver = "v1.01"
'Private Const ModDate = "29/02/2003"

''#**********************************************************************************
''# Version Date Who Details
''# ====== ========== ===
======================================================
''# v1.01 25/03/2003 TS User Updates.
''# v1.00 29/02/2003 TS New Template.

' This finds the appropriate spot and updates the amendment
history
Set myComponent = currProj.VBComponents(MODULE_NAME)

With myComponent.CodeModule
For I = 1 To .CountOfLines
strLine = Trim(.Lines(I, 1))
If Left(strLine, Len(RELMARKER)) = RELMARKER Then
.DeleteLines I, 5

.InsertLines I, strNewHistory
Exit For
End If
Next I
End With

'This hopes to change the logo image on the form
Set myComponent = currProj.VBComponents(FORM_NAME)

For I = 1 To myComponent.Designer.Controls.Count
With myComponent.Designer
If .Controls(I).Name = IMAGE_CONTROL_NAME Then
Set myControl = .Controls(I)

'**************************************************************
'This is where it fails because the .Picture is an OLE object.
'**************************************************************
'**************************************************************
'**************************************************************

' Its only properties are
' name Type
' ======= ==============
' Handle OLE_HANDLE
' Height OLE_YSIZE_HIMETRIC
' hPAL OLE_HANDLE
' Type Integer
' Width OLE_XSIZE_HIMETRIC
'**************************************************************
myControl.Picture = "newLogo" ''?????
Exit For
End If
End With
Next I
 

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