none
Get text in a table from a word file, using a nother word file RRS feed

  • Question

  • Hi i trying to open another Word file find some text in a table
    and then later get it to the first Word file also in a table

    I try this

    Sub automateword() Set wordapp = CreateObject("word.Application") wordapp.Documents.Open "\\main\foralle\alvin\hans.docx" wordapp.Visible = True strCelltext = ? .Tables(1).Cell(2, 2).Range.Text MsgBox strCelltext End Sub

    As you can see i don't know what to write in
    strCelltext = ? .Tables(1).Cell(2, 2).Range.Text
    Can someone help ?

    regards

    Alvin


     

    ALK

    Thursday, June 20, 2013 11:06 AM

Answers

  • Hi Alvin

    Firstly, if Word is already running - which is certainly the case if this is VBA code in Word - then you don't need and shouldn't use these three lines:

       Set wordapp = CreateObject("word.Application")
        wordapp
    .Documents.Open "\\main\foralle\alvin\hans.docx"
        wordapp
    .Visible = True

    Instead, do this:

       Dim docSource as Word.Document
       Dim docTarget as Word.Document
       Set docTarget = ActiveDocument
       Set docSource = Documents.Open    "\\main\foralle\alvin\hans.docx"
       strCelltext = docSource .Tables(1).Cell(2, 2).Range.Text
       docTarget.Tables(1).Cell(2,2).Range.Text = strCellText


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, June 20, 2013 12:02 PM
    Moderator