Inserting jpg's into headers with VBA

E

eselick

Hi

I'm trying to develop a macro in Word to insert a picture (shape
object) in a header. I have no problem inserting lines as inline shapes
using the addline() method and . However when I use AddPicture(), it
always gives me compile errors or runtime type mismatch.

Does anyone have a code snippet that works?

Thanks

Elly Belly
 
G

Greg Maxey

This recorded macro seemed to work without problem:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 10/25/05 by Gregory K. Maxey
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.InlineShapes.AddPicture FileName:= _
"C:\Test.jpg", LinkToFile:=False, _
SaveWithDocument:=True
End Sub
 
E

eselick

Hi Greg

I'm new to the forum but think it's a real find with lots of ideas and
help for VBA programming.

I already tried a similar AddPicture snippet which I found in the
following message.

http://groups.google.com/group/micr...5db?q=AddLogoFromFile&rnum=1#ab78e0101a4165db

I tried to add a repy to that thread and it doesn't seem to have
worked, so I'l just repeat it here.

I'm developing a VBA application using MS Word XP in Visual Basic 6.

I ran the AddPicture code with no problem in the Word built in
VBA environment. However when I ported the exact same code to VB6,
where my quite complex project lives, the code failed with various
compile and runtime errors.

I have no problems with inline shapes like lines etc in VB6. I haven't
tried
adding a picture outside of a header, but I will and I'm presuming that
it will also fail since it doesn't seem that the header location would
have anything to do with the problem. I'll confirm this with another
test.

I have the feeling VB6 does not have access to the exact same object
model as the native Word VBA environment. Is it possible I haven't
installed a necessary project reference in VB6 and if so, what would it
be?
Do you have any other ideas?

Thanks hugely,

Elliot Selick

P.S What is the easiest way to find and follow this thread in Googe.
 
E

eselick

Some new information....

I running Word Version 10. SP3 according to the Word help

I've created a new VB6 project to try to isolate the problem.
I have a project references in VB6 to both Microsoft Word 10 Object
Library and Microsoft Office 10 Object library

Here is the complete code I'm running in VB6 (As I stated above, the
same code works fine when run in the Word 2002 excluding the part to
get Word going.)


Private Sub Command2_Click()
Dim MyRange As Range
Dim myShape As Shape
Dim obj As Object
On Error Resume Next
Set obj = _
CreateObject("Word.Application")
On Error GoTo 0
obj.Documents.Add 'create a new document
obj.Selection.InsertAfter "This is some text"

' Set MyRange = Selection.Sections.Last _
.Headers(wdHeaderFooterPrimary).Range
Set MyRange = ActiveDocument.Paragraphs.Last.Range
MyRange.Collapse wdCollapseEnd
Set myShape = ActiveDocument.Shapes.AddPicture _
(FileName:="C:\ELLIOT\AddressBook\Versals\Wetstein\A.gif", _
linktofile:=False, savewithdocument:=True, _
Anchor:=MyRange)
With myShape
.WrapFormat.Type = wdWrapNone
.Height = 60
.Width = 80
.LockAspectRatio = msoTrue
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Left = InchesToPoints(1#)
.Top = InchesToPoints(1.25)
End With
obj.Quit 'close Word
Set obj = Nothing
End Sub

When I run the code, it stops on the following line:

.WrapFormat.Type = wdWrapNone

with the message::

Compile error:
Method or Data Member not Found.

If I comment out the WrapFormatType line, the same error occurrs on
most of the remaining properites.

If I comment them all out, I get a runtime error 13 Type MisMatch on
the following line:

Set myShape = ActiveDocument.Shapes.AddPicture _
(FileName:="C:\ELLIOT\AddressBook\Versals\Wetstein\A.gif", _
linktofile:=False, savewithdocument:=True, _
Anchor:=MyRange)

That's it. Hope someone can help.

Thanks again
Elliot
 
J

Jonathan West

Hi Elliot,

Your problem is in fact in this line

Dim myShape As Shape

This is because VB6 has its own Shape object. The order of references in the
project references list defines which library's Shape object is used by
default when two different libraries have objects with the same name. VB6's
own libraries are listed above Word's libraries.

To force the use of Word's Shape object, change the declaration to this,
explicitly forcing the use of the Word library

Dim myShape As Word.Shape

All should then be well.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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