PDA

View Full Version : Position cursor



Lartk
01-14-2013, 09:07 AM
I am working in excel VBA but am within that am "talking to" a word document.

Is there a way, in excel VBA, to position the cursor in a word document 2 returns down the page?

p45cal
01-15-2013, 11:25 AM
Based on your recent thread's code, if the returns don't already exist then:
.Application.Selection.TypeParagraph
.Application.Selection.TypeParagraph

If the returns do exist:
.Application.Selection.MoveDown Unit:=wdLine, Count:=2

Lartk
01-15-2013, 01:25 PM
The returns are not already there. I open the word document, and I would like to start by positioning my cursor two returns down the page. I tried this code but I get an error saying "Object Variable or with block variable not set"

Sub Test()
Dim FName As String
Dim FPath As String
Sheets("Sheet1").Range("A3:G80").CopyPicture Appearance:=xlScreen, Format:=xlPicture
With CreateObject("C:\Users\kl\Desktop\Master Format.docm")

.Application.Selection.TypeParagraph
.Application.Selection.TypeParagraph

.Application.Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False

FPath = "C:\Users\kl\Desktop"
FName = Sheets("Sheet1").Range("A6").Text

.SaveAs2 Filename:=FPath & "\" & FName
.Close 0

End With
End Sub

Kenneth Hobs
01-15-2013, 01:48 PM
Look at the links to the threads that I referenced in your earlier thread: http://www.vbaexpress.com/forum/showthread.php?t=44990

I recommend setting a reference to the MSWord application and then have it open the MSWord file and then do something with it. If you have the document open already, GetObject() can be used. Several of those threads show how to do that.

snb
01-15-2013, 02:04 PM
or


Sub Test()
Sheets("Sheet1").Range("A3:G80").CopyPicture

With GetObject("C:\Users\kl\Desktop\Master Format.docm")
.content.insertafter string(3,vbcr)
.paragraphs(3).range.pastespecial datatype:=wdPasteEnhancedMetafile
.SaveAs2 "C:\Users\kl\Desktop\" & Sheets("Sheet1").Range("A6").Value
.Close 0
End With
End Sub

Lartk
01-15-2013, 02:10 PM
Getting there,

Sub Test()
Sheets("Sheet1").Range("A3:G80").CopyPicture

With GetObject("C:\Users\\Desktop\Master Format.docm")
.Content.InsertAfter String(1, vbCr)
.Paragraphs(1).Range.PasteSpecial DataType:=wdPasteEnhancedMetafile
.SaveAs2 "C:\Users\kl\Desktop\" & Sheets("Sheet1").Range("A6").Value
.Close 0
End With
End Sub


This code pastes the excel data into word. Although, it comes up in a very small font. The word document i am trying to paste into is attached. There is a border on the word document so when i paste my excel stuff into word using the above code, the border shifts down so its not working perfectly.