Consulting

Results 1 to 6 of 6

Thread: Position cursor

  1. #1
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location

    Position cursor

    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?

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    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"

    [VBA]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
    [/VBA]

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    or

    [VBA]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[/VBA]

  6. #6
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location

    paste into word

    Getting there,

    [VBA]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
    [/VBA]

    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.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •