+ Reply to Thread
Results 1 to 7 of 7

putting values from excel into word

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-10-2011
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2016
    Posts
    187

    putting values from excel into word

    Hello,

    I am sure, that tjhis question has been discussed several times, but I couldn't find the answer.
    I want to put values into a word documents from excel. I guess, I have to create a dot, and I have to add bookmarks, where I want to add the data.
    What I don't know, how to be coded, that I want to put the value of A1 to the bookmark "Name"?

    Thank you
    Last edited by HarryKlein; 07-10-2011 at 02:37 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: putting values from excel into word

    Create Named ranges in Excel that match the Bookmarks in Word, then try this code

    Option Explicit
     
    Sub namesToBookmarks()
        Dim objWord As Object
        Dim docWord As Object
        Dim wb As Excel.Workbook
        Dim xlName As Excel.Name
        Dim Path As String
         
        Set wb = ActiveWorkbook
        Path = wb.Path & "\MyForm.dot" 'change the name
         
        On Error GoTo ErrorHandler
         
         'Create a new Word Session
        Set objWord = CreateObject("Word.Application")
         
        On Error GoTo ErrorHandler
         
         'Open document in word
        Set docWord = objWord.Documents.Add(Path)
         
         'Loop through names in the activeworkbook
        For Each xlName In wb.Names
             'if xlName's name is existing in document then put the value in place of the bookmark
            If docWord.Bookmarks.Exists(xlName.Name) Then
                docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
            End If
        Next xlName
         
         'Activate word and display document
        With objWord
            .Visible = True
            .ActiveWindow.WindowState = 0
            .Activate
        End With
         
         'Release the Word object to save memory and exit macro
    ErrorExit:
        Set objWord = Nothing
        Exit Sub
         
         'Error Handling routine
    ErrorHandler:
        If Err Then
            MsgBox "Error No: " & Err.Number & "; There is a problem"
            If Not objWord Is Nothing Then
                objWord.Quit False
            End If
            Resume ErrorExit
        End If
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: putting values from excel into word

    Do not use bookmarks in Word but docvariables instead.
    Put a string into cel A1 of sheets(1)

    sub snb()
      with createobject("Word.document")
        .variables("name")= iif(sheets(1).cells(1)=""," ",sheets(1).cells(1))
        .Fields.Add .Paragraphs.Last.Range, 64, "name", False
        .fields.update
        .saveas "E:\example.doc"
        .close 0
      end with
    End sub
    Open the worddocument 'E:\example.doc'.

    You can consider bookmarks to be a simplified solution in Word's userinterface. Users that are familiar with VBA can benefit the advantages of docvariables.



  4. #4
    Forum Contributor
    Join Date
    05-10-2011
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2016
    Posts
    187

    Re: putting values from excel into word

    Thank you,

    can you pls tell me, what is this line good for?
    .Fields.Add .Paragraphs.Last.Range, 64, "name", False

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: putting values from excel into word

    It inserts a docvariable-field into the document, so you can see the value of the documentvariable.

  6. #6
    Forum Contributor
    Join Date
    05-10-2011
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2016
    Posts
    187

    Re: putting values from excel into word

    Thanks again

  7. #7
    Forum Contributor
    Join Date
    05-10-2011
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2016
    Posts
    187

    Re: putting values from excel into word

    Thanks a lot, I learned a lot from this code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1