Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro record in Word 2010

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,548
US

In Word 2010, I have a picture on the page that I want to resize to full page. I can do it manually by:
Home – Editing – Select – Select Objects – Click on the picture to select it.
Format – Size – type 8 in height and hit Enter to size it to full page.

(there may be a better way to do it, I just know about this way)

Then I want to record a macro to see the code behind my actions:

Developer – Record Macro – Macro Name: “Macro1” – OK
All the steps from above

Developer – Stop Recording

I get:
Code:
Sub Macro1()[green]
'
' Macro1 Macro
'
'[/green]
End Sub
What am I doing wrong? Where is my macro code?

Have fun.

---- Andy
 
Hi Andy. The answer is with this: "Click on the picture to select it."

Word does not record (most) macro instructions for selected objects on the graphics layer. Never has.

55,687.00 hours down....
a big fat ZERO hours to go
 
Hi Andy,

Code to rescale the image...

I have some vb6 code which had to be patched when automating Word 2010; here it is;

'determine required w and H from WordObj.Selection.PageSetup

Clipboard.Clear
Clipboard.SetData Picture1.Image, vbCFDIB 'a full print area A4 sized image
WordObj.Selection.Paste

'Acccomodate Word 2010 which auto rescales the pasted image so it only appears at about 2/3 rds the required size
' has advantage under Word 2007 too
If Val(WordObj.Version) >= 12 Then
With WordObj.Activedocument.inlineshapes(WordObj.Activedocument.inlineshapes.Count)
.LockAspectRatio = False
.Width = w
.Height = H
End With
End If

Hope it helps.
 

@fumei, I was able to select the pasted picture in Word 2002, see the line in [red]RED[/red] that I took out from my code

@ HughLerwill, thanks for pointing my in the right direction (kind of). After pasting the picture from Clipboard, the line [tt]WordObj.Activedocument.inlineshapes.Count[/tt] gave me 0 (zero), but what I have noticed the image in Word was already selected, so I had to do very little modification to my original code - just took the line in [red]RED[/red] :)
Code:
With ObjWord
    .Documents.Add DocumentType:=wdNewBlankDocument
    On Error Resume Next
    .ActiveDocument.PageSetup.Orientation = wdOrientLandscape
    .ActiveDocument.PageSetup.TopMargin = .InchesToPoints(0.75)
    .ActiveDocument.PageSetup.BottomMargin = .InchesToPoints(0.75)
    .ActiveDocument.PageSetup.LeftMargin = .InchesToPoints(0.75)
    .ActiveDocument.PageSetup.RightMargin = .InchesToPoints(0.75)
    On Error GoTo 0
    .ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
    If .Options.PictureWrapType <> wdWrapMergeTight Then
        .Options.PictureWrapType = wdWrapMergeTight
    End If
    .Selection.Paste
    [red]
    '.ActiveDocument.Shapes("Picture 2").Select[/red]
    .Selection.ShapeRange.ScaleWidth [blue]1.5[/blue], msoFalse, msoScaleFromTopLeft
    .Selection.ShapeRange.ScaleHeight [blue]1.5[/blue], msoFalse, msoScaleFromTopLeft

    .Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
        False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
    
    .Visible = True
    .Activate
End With

The blue hard coded 1.5 'enlargement' is still in question but I will deal with it later.

Have fun.

---- Andy
 
Hi Andy,
in case of inline shape:
Code:
Dim oInlineShape As InlineShape, ResizeRatio As Double
Set oInlineShape = ThisDocument.InlineShapes(1)
With oInlineShape
    With .Range.Sections(1).PageSetup
        ResizeRatio = (.PageWidth - .LeftMargin - .RightMargin) / oInlineShape.Width
    End With
    .Width = .Width * ResizeRatio
    .Height = .Height * ResizeRatio
End With

combo
 

Thank you combo, but the line of code:

Code:
WordObj.ActiveDocument.InlineShapes.Count

gives me 0 (zero) :-(
The image I just placed on the document is not recognized for some reason.

Have fun.

---- Andy
 
So, have you tried the Shapes collection instead of InlineShapes ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
For a shape:
Code:
Dim oShape As Shape, ResizeRatio As Double
Set oShape = ThisDocument.Shapes(1)
With oShape
    With .Anchor.Sections(1).PageSetup
        ResizeRatio = (.PageWidth - .LeftMargin - .RightMargin) / oShape.Width
    End With
    .Width = .Width * ResizeRatio
    '.Height = .Height * ResizeRatio
End With
I commented the height setting as in this case the image keeps the aspect ratio after setting the width.

combo
 
@fumei, I was able to select the pasted picture in Word 2002, see the line in RED that I took out from my code

Yes, that is true, but that was not the question. You can Select just about anything when you write code. However, as stated, - and that was the question - the macro recorder does not produce code for actions performed in the GUI for objects on the graphics layer. It catches a few things, but not much.

You have to write code, either selecting the graphical element (in code), or creating and SETing an object for it.

55,687.00 hours down....
a big fat ZERO hours to go
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top