Consulting

Results 1 to 8 of 8

Thread: transfer data from excel to word

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location

    transfer data from excel to word

    Hi guys

    I have a question about if it is possible to transfer selectively data to an existing word document with VBA?

    I searched the internet but didn't find any information.

    I hope you can help me with this

    thank you very much

    Johan

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim wd As Object
        Dim doc As Object
        Const myDoc = "D:\***\****\test.docx"
        
        Set wd = CreateObject("Word.application")
        wd.Visible = True
         Set doc = wd.Documents.Open(myDoc)
         
         doc.Range(0, 0).Text = Range("a1").Value
    
    
    End Sub

  3. #3
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location
    Hi Mana

    Thank you very much for your help, i think i didn't explain well sorry for that.

    I have a word document and some lines in this document, i want to fill them up with data from Excel with VBA

    Thanks

    Johan

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    >i want to fill them up with data from Excel

    I can't undestand.
    Would you provide a concrete example?

  5. #5
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location
    Yes, i have a word document with placeholders and i want this placeholders to fill them up with excel data.

    I want a button in excel where i can select this word file and then VBA fill the placeholders in Word with data form Excel

    I hope this is possible

    johan

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    you can use word Bookmark.
    A1:data
    B1:bookmark name


    Option Explicit 
    
    Sub test()
        Dim wd As Object
        Dim doc As Object
        Dim r As Object
        Const myDoc = "D:\***\****\test.docx"
         
        Set wd = CreateObject("Word.application")
        wd.Visible = True
        Set doc = wd.Documents.Open(myDoc)
         
        Set r = doc.Bookmarks(Range("b1").Value).Range
        r.Text = Range("a1").Value
        doc.Bookmarks.Add Range("b1").Value, r
           
    End Sub

  7. #7
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location
    Hi Mana

    I have this code to select a certain placeholder <<...>> and select a cell in excel and paste it in word where <<>> is
    but i get this error :run time error 242 object required

    Private Sub CommandButton1_Click()


    Dim wApp As Word.Application
    Dim wDoc As Word.Document
    Set wApp = CreateObject("Word.Application")
    wApp.Visible = True


    Set wDoc = Application.GetOpenFilename("Word Files (*.doc*), *.doc*")


    With wDoc
    .Application.Selection.Find.Text = "<<Invoerveld>>"
    .Application.Selection.Find.Execute
    Application.Selection = Range("A7")
    .Application.Selection.EndOf


    .Application.Selection.Find.Text = "<<Invoerveld>>"
    .Application.Selection.Find.Execute
    Application.Selection = Range("A8")


    End With


    End Sub

  8. #8
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim wApp As Word.Application
        Dim myFile
        Dim wDoc As Word.Document
         
        myFile = Application.GetOpenFilename("Word Files (*.doc*), *.doc*")
        
        If VarType(myFile) = vbBoolean Then Exit Sub
        If Not myFile Like "*.doc*" Then Exit Sub
        
        Set wApp = New Word.Application
        wApp.Visible = True
        Set wDoc = wApp.Documents.Open(myFile)
        
        
        With wDoc.Application.Selection
            .Find.Text = "<<Invoerveld>>"
            If .Find.Execute Then
            .Text = Range("A7")
            .EndOf wdWord, wdMove
            End If
            
            If .Find.Execute Then
                .Text = Range("A8")
            End If
        End With
        
    End Sub

Posting Permissions

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