set the cursor on the second page

T

Torsten Hansen

Hello again,

I had a similar problem with the orientation on landscape. When I've put a
picture from the clipboard into my new word document the cursor appears in
the first row of the document.
Then I make a page break and now I want to set the cursor in the first line
of the second page. This works in a word macro with "Selection.MoveDown...",
but when I accomplish the makro from Excel, Access... the command doesn't
work.
Can anybody give me an advice?

Sub Test_Makro_From_Excel()
Dim WordObj As Object
Dim WordDoc As Object
'copy anything to the excel-clipboard
Range("B5:D8").Select
Selection.Copy
On Error Resume Next
Set WordObj = GetObject(, "Word.Application.10")
If Err.Number = 429 Then
Set WordObj = CreateObject("Word.Application.10")
Err.Number = 0
End If
WordObj.Visible = True
Set WordDoc = WordObj.Documents.Add
With WordObj
.Selection.PasteSpecial DataType:=3 'insert as picture
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.InsertBreak Type:=1
.ActiveDocument.Shapes("Picture 2").Select
.Selection.ShapeRange.ScaleWidth 1.52, msoFalse, msoScaleFromTopLeft
.Selection.ShapeRange.ScaleHeight 1.52, msoFalse, msoScaleFromTopLeft
.Selection.ShapeRange.ScaleHeight 1.32, msoFalse, msoScaleFromTopLeft
.ActiveWindow.ActivePane.VerticalPercentScrolled = 0
.ActiveWindow.ActivePane.VerticalPercentScrolled = 32
.Selection.ShapeRange.IncrementTop 9#
.Selection.Collapse

'this command doesn't work from Excel, Access...
!!!!!!!!!!!!!!!!!!!!!!!
.Selection.MoveDown Unit:=wdLine, Count:=22

End With
Set WordDoc = Nothing
Set WordObj = Nothing
End Sub


Many Thanks!

Torsten
 
J

JGM

Hi Torsten

When using Word VBA Objects from another environment you have to be carful
with the constants you use.
'this command doesn't work from Excel, Access...
!!!!!!!!!!!!!!!!!!!!!!!
.Selection.MoveDown Unit:=wdLine, Count:=22
In this case, wdLine is a constant, when you use it within Word, Word knows
its real value, but when you are executing the code from outside Word, the
other environment does not know the value of wdLine. Either you look it up
in the Word VBE Object brower, then click on it when you find it and you
will see its numerical value at the bottom in the status bar of the Object
browser (F2 from the editing window, or look in the View menu of the VBE
window). Type wdLine in the serach field... Then you will discover that this
constant value is 5.

Alternatively, you can prefix the constant with the Application object you
have set.
This gives you 2 choices:

..Selection.MoveDown Unit:=5, Count:=22
or
..Selection.MoveDown Unit:=WordObj.wdLine, Count:=22

HTH
Cheers!
 

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