Insert picture into a table in header

S

Sean

My code to insert a picture is below. I have a 1row x
2column table in the header and I would like to place
this picture in the right column of the table. How can i
accomplish this using code?
Thanks.

With Dialogs(wdDialogInsertPicture)
If .Display Then
'Populate the DocProperty with file path
ActiveDocument.CustomDocumentProperties
("ClientLogPath").Value = WordBasic.FilenameInfo$(.Name,
1)
End If
End With

Dim oRng As Range
Set oRng = ActiveDocument.Sections(1).Headers
(wdHeaderFooterPrimary).Range
Selection.Collapse Direction:=wdCollapseStart
ActiveDocument.InlineShapes.AddPicture Range:=oRng, _
FileName:=ActiveDocument.CustomDocumentProperties
("ClientLogPath").Value
 
J

Jay Freedman

Hi Sean,

The key is to assign the range oRng to the range of the specific cell in the
specific table, not just to the whole header. In doing this, the Selection
object isn't involved at all -- it can remain pointing to wherever the user
left the cursor, while oRng does all the work.

Also, the CustomDocumentProperties collection is kind of finicky: You'll get
an error if you try to assign a value to a property that hasn't been added,
and you'll get an error if you try to add one that already exists. One way
to handle this is the error trap shown below.

Finally, the code that inserts the picture should be inside the "If .Display
Then"..."End If" construction. Otherwise you'll get an error if the user
cancels the dialog, because you're trying to add a picture whose filename is
an empty string.

Dim oRng As Range

On Error Resume Next
ActiveDocument.CustomDocumentProperties.Add _
Name:="ClientLogPath", _
LinkToContent:=False, Value:="", _
Type:=msoPropertyTypeString
On Error GoTo 0

With Dialogs(wdDialogInsertPicture)
If .Display Then
'Populate the DocProperty with file path
ActiveDocument.CustomDocumentProperties( _
"ClientLogPath").Value = _
WordBasic.FilenameInfo$(.Name, 1)

Set oRng = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range _
.Tables(1).Cell(1, 2).Range

' The next line has nothing to do with the Range!
'Selection.Collapse Direction:=wdCollapseStart

ActiveDocument.InlineShapes.AddPicture Range:=oRng, _
FileName:=ActiveDocument.CustomDocumentProperties( _
"ClientLogPath").Value
End If
End With
 
S

Sean

That's great Jay! Thanks alot, it will work great. But
what if I wanted to use an image control out of the
control toolbox. I guess it doesn't matter what I use as
long as I can control the dimensions of the inserted
picture using something similar to the Picture Size Mode
Zoom property. Is this possible to do?

-----Original Message-----
Hi Sean,

The key is to assign the range oRng to the range of the specific cell in the
specific table, not just to the whole header. In doing this, the Selection
object isn't involved at all -- it can remain pointing to wherever the user
left the cursor, while oRng does all the work.

Also, the CustomDocumentProperties collection is kind of finicky: You'll get
an error if you try to assign a value to a property that hasn't been added,
and you'll get an error if you try to add one that already exists. One way
to handle this is the error trap shown below.

Finally, the code that inserts the picture should be inside the "If .Display
Then"..."End If" construction. Otherwise you'll get an error if the user
cancels the dialog, because you're trying to add a picture whose filename is
an empty string.

Dim oRng As Range

On Error Resume Next
ActiveDocument.CustomDocumentProperties.Add _
Name:="ClientLogPath", _
LinkToContent:=False, Value:="", _
Type:=msoPropertyTypeString
On Error GoTo 0

With Dialogs(wdDialogInsertPicture)
If .Display Then
'Populate the DocProperty with file path
ActiveDocument.CustomDocumentProperties( _
"ClientLogPath").Value = _
WordBasic.FilenameInfo$(.Name, 1)

Set oRng = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range _
.Tables(1).Cell(1, 2).Range

' The next line has nothing to do with the Range!
'Selection.Collapse Direction:=wdCollapseStart

ActiveDocument.InlineShapes.AddPicture Range:=oRng, _
FileName:=ActiveDocument.CustomDocumentProperties( _
 
S

Sean

One more thing.. my reasoning for this is so.. I use an
image control so that I can use the VBA code to control
what happens upon a ClientLog_Click event. I would
really like to be able to change the picture within this
image control. I just couldn't figure out how to replace
the path...
 

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