PDA

View Full Version : control word from excel



enfantter
04-15-2010, 12:16 AM
hello

i have written a code that exports data from excel to word. when one line is pasted, it automatically inserts a line break.

is it possible to prevent this ?

or alternatively move the cursor to left ?

the following code debugs
wdapp.documents("ordliste").MoveLeft Unit:=wdCharacter, Count:=1

TonyJollans
04-15-2010, 02:20 AM
If you get a line break, it's because you're pasting one, because you've copied it to the clipboard.

The cursor is presented to VBA as the Selection. You can do a Selection.MoveLeft if you wish, but there may well be a better way to do what you want, if you can give us some more details.

enfantter
04-15-2010, 03:51 AM
I've copied the cells straight, and there is no line break in the cells.

I have posted the code below ...
Sub Macro7()
Set wdapp = CreateObject("Word.Application")
Set ppapp = CreateObject("powerpoint.application")
wdapp.Visible = True
ppapp.Visible = True

wdapp.documents.Open Filename:="J:\Countries\Denmark\OUF\ordliste.doc"
ppapp.Presentations.Open Filename:="J:\Countries\Denmark\OUF\ordliste.ppt"
antal = Range("a65536").End(xlUp).Row
For i = 1 To antal
Sheets("Output").Cells(i, 1).Copy
wdapp.Activate
wdapp.documents("ordliste").Activate
wdapp.Selection.PasteSpecial
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Sheets("Output").Cells(i, 2).Copy
wdapp.Selection.PasteSpecial
Next i

' wrdDoc.Close savechanges:=True
End Sub

the selection.moveleft debugs and gives run time error 438

does this clearify anything ?!

Tinbendr
04-15-2010, 05:19 AM
wdapp.Selection.PasteSpecial
Selection.MoveLeft Unit:=wdCharacter, Count:=1
The line above reveals the problem.
So your code should read...
wdapp.Selection.MoveLeft Unit:=wdCharacter, Count:=1 You probably have two errors actually. I have always had trouble using Word property names from Excel. So one final change.
wdapp.Selection.MoveLeft 1, 1Good luck!

TonyJollans
04-15-2010, 08:49 AM
When you copy cells, as opposed to cell contents, and paste them into text, the carriage return is what terminates the copied cell.

And Tinbendr is right, wdCharacter either needs qualifying or replacing.