Who can help making a macro?

S

Stella

the macro should insert a picture from c:/my pictures
than insert a page
than insert another picture from c:/my pictures
than insert a page and so forth

Can someone tell me how to write the code?
 
E

Ed Bennett

Stella said:
the macro should insert a picture from c:/my pictures
than insert a page
than insert another picture from c:/my pictures
than insert a page and so forth

Can someone tell me how to write the code?

The command for Insert Page is (Document object).Pages.Add. (Document
object) is either ThisDocument (if you are macro writing);
ActiveDocument (if you're writing an add-in); or another document object
that you've created (if you're automating Publisher from another
application).

The command for Insert Shape is (Page object).Shapes.AddPicture. (Page
object) can be (Document object).ActiveView.ActivePage, or you can Set
the page you created in the first step to be in a variable (e.g.
CurrentPage), and refer to that instead.

The syntax for both these commands is in the Publisher VBA Help file. To
access this, in Publisher 2002 or 2003 you can simply open c:\Program
Files\Microsoft Office\Office1?\1033\VBAPB10.CHM, where ? is 0 for
Publisher2002 and 1 for Publisher 2003; 1033 will change if you copy of
Publisher isn't in US English. In all versions you can hit F1 in the VBA
IDE to bring up help, too.
 
S

Stella

sorry ed,
I am trying to learn by doing but its hard. Look at the code. How do I add
the particular file on E drive. Thanks

Sub newtrial()
With ActiveDocument.Pages.Add(Count:=1, After:=1)
With .Shapes.AddPicture(E:\picture1)
End With
End With

End Sub
 
E

Ed Bennett

Stella said:
sorry ed,
I am trying to learn by doing but its hard. Look at the code. How do I add
the particular file on E drive. Thanks

Sub newtrial()
With ActiveDocument.Pages.Add(Count:=1, After:=1)
With .Shapes.AddPicture(E:\picture1)
End With
End With

The path to the file has to be enclosed in quotes (as otherwise the
language thinks it is a variable name or other operation).

There is no need to use a With block for inserting the picture - VBA is
somewhat funnily-behaved, in that if you have a function call on its
own, you don't use brackets. So your sub would look something like

Sub newtrial()
With ActiveDocument.Pages.Add(Count:=1, After:=1)
.Shapes.AddPicture "E:\picture1"
End With
End Sub

You could then use a Do...While loop to add multiple pictures. How
exactly this is coded depends on how you are wanting to provide the
program with a list of pictures.
 
S

Stella

Thanks Ed
I got the macro to work.
Now I would like to use an If/then statement.

The macro should do the following.

Insert picture1 to Left: 10, Top: 10 if the picture is landscape
but if picture is portrait Insert picture1 to Left: 50, Top: 70

Is this determination possible? Or if not is there another solution.
Thanks
 
E

Ed Bennett

Stella said:
Insert picture1 to Left: 10, Top: 10 if the picture is landscape
but if picture is portrait Insert picture1 to Left: 50, Top: 70

Is this determination possible? Or if not is there another solution.

I'd use the following method:
Insert Picture1 at 10, 10.
If Picture1.Width < Picture1.Height, then move it to 50, 70.
(This assumes that square pictures are 'landscape'. If you want to
assume square pictures are portrait, replace < with <=

The syntax for an If block is given here:
http://msdn2.microsoft.com/en-us/library/5h27x7e9.aspx
(It claims to be .NET 3.0-specific, but applies just as much to VBA.)
 
S

Stella

Where in my existing and working code do I fit this in?

My Code at the moment is

Sub addpic()
With ActiveDocument.Pages.Add(Count:=1, After:=1)

Dim shpPicture As Shape
Set shpPicture = ActiveDocument.Pages(1).Shapes.AddPicture _
(Filename:="E:\picture1.jpg", _
LinkToFile:=msoTrue, _
SaveWithDocument:=msoFalse, _
Left:=50, Top:=90, Height:=300)
 
E

Ed Bennett

Stella said:
Where in my existing and working code do I fit this in?

Dim shpPicture As Shape
Set shpPicture = ActiveDocument.Pages(1).Shapes.AddPicture _
(Filename:="E:\picture1.jpg", _
LinkToFile:=msoTrue, _
SaveWithDocument:=msoFalse, _
Left:=50, Top:=90, Height:=300)

That's the add picture code, which you'll want to change the Left: and
Top: parameters of. After this you'll want to add the If block.
 
S

Stella

If I am adding the If block, do I need to end my sub first and open up a new
one?
How do I best connect the two codes?

For Each shp In ActiveDocument.Pages(1).Shapes
If shp.Type = pbPicture Then
.Move Left:=50, Top:=50
End If
 
E

Ed Bennett

Stella said:
If I am adding the If block, do I need to end my sub first and open up a new
one?

No, you just want one procedure that adds the picture and then modifies
it if it is portrait.
How do I best connect the two codes?

For Each shp In ActiveDocument.Pages(1).Shapes
If shp.Type = pbPicture Then
.Move Left:=50, Top:=50
End If

This isn't what you want to do. As I said on Sunday, once the shape is
inserted, in the same procedure you want to move it if it is portrait.

This will give you something like:

Dim shpPicture As Shape
Set shpPicture = ActiveDocument.Pages(1).Shapes.AddPicture _
(Filename:="E:\picture1.jpg", _
LinkToFile:=msoTrue, _
SaveWithDocument:=msoFalse, _
Left:=10, Top:=10, Height:=300)
If shpPicture.Width < shpPicture.Height Then
shpPicture.Left = 50
shpPicture.Top = 70
End If
 

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