+ Reply to Thread
Results 1 to 9 of 9

Copying data from excel into Word

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Copying data from excel into Word

    Hello all,

    I have a code that copies values from specific cells in a specific sheet to the relevant bookmarks of a word document. The code is shown below:

    Sub Procedure1()
    Dim objWord As Object
    Dim ws As Worksheet
    
        Set ws = ThisWorkbook.Sheets("Sheet6")
    
        Set objWord = CreateObject("Word.Application")
    
        objWord.Visible = True
    
        objWord.Documents.Open "C:\Users\Christopher.Ellis\Test1.docx" ' change as required
    
        With objWord.ActiveDocument
            .Bookmarks("CN1").Range.Text = ws.Range("C25").Value
            .Bookmarks("CN2").Range.Text = ws.Range("C25").Value
            .Bookmarks("CNo1").Range.Text = ws.Range("C26").Value
            .Bookmarks("Cl1").Range.Text = ws.Range("C27").Value
            .Bookmarks("Ex_1").Range.Text = ws.Range("C28").Value
            .Bookmarks("Ex_2").Range.Text = ws.Range("C28").Value
    
    
    .Save
        .Close
            
        End With
     
        Set objWord = Nothing
    
    
    End Sub
    The code works fine, however when posting one of the values the font changes size, as opposed to what size it is on excel or on the Word document. In addition to this, some percentages that are shown in cells as 15.6% for example are appearing in the word document as 15.5742341313232%.

    My question is, how do I amend my code so that the font appears how I want it to and so that the percentages appear to 2 decimal places?


    Any assistance appreciated.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Copying data from excel into Word

    Hi !

    Use Text property instead of Value

  3. #3
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copying data from excel into Word

    I've tried replacing the .Value for the relevant row and then added in something for the Font, but I am getting an Object doesn't support this property error

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,831

    Re: Copying data from excel into Word

    Without seeing your updated code - especially for the font - it's impossible to know what the issue is. That said, the way you're adding content to the bookmarks means that you can't format the added content. For code to update a bookmarked range so that the update is accessible via the bookmark, try:
    Sub Procedure1()
    Dim objWord As Object, objDoc As Object
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet6")
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Open("C:\Users\Christopher.Ellis\Test1.docx") ' change as required
    Call UpdateBookmark(objDoc, "CN1", ws.Range("C25").Text)
    Call UpdateBookmark(objDoc, "CN2", ws.Range("C25").Text)
    Call UpdateBookmark(objDoc, "CNo1", ws.Range("C26").Text)
    Call UpdateBookmark(objDoc, "Cl1", ws.Range("C27").Text)
    Call UpdateBookmark(objDoc, "Ex_1", ws.Range("C28").Text)
    Call UpdateBookmark(objDoc, "Ex_2", ws.Range("C28").Text)
    objDoc.Close True
    Set objDoc = Nothing: Set objWord = Nothing: Set ws = Nothing
    End Sub
    
    Sub UpdateBookmark(objDoc As Object, StrBkMk As String, StrTxt As String)
    Dim BkMkRng As Object
    With objDoc
      If .Bookmarks.Exists(StrBkMk) Then
        Set BkMkRng = .Bookmarks(StrBkMk).Range
        BkMkRng.Text = StrTxt
        .Bookmarks.Add StrBkMk, BkMkRng
      End If
    End With
    Set BkMkRng = Nothing
    End Sub
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copying data from excel into Word

    Thanks Paul. Then can I add in Rows such as .Font=Tahoma and .Font=9 etc?

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,831

    Re: Copying data from excel into Word

    If you apply the desired font characteristics to the bookmarks before updating them, they should apply to whatever's inserted there. Otherwise, after updating the content, you could use code like:
    With objDoc.Bookmarks("CN2").Range.Font
      .Name = "Tahoma"
      .Size = 9
    End With

  7. #7
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copying data from excel into Word

    Hi Paul. My word template has text in the bookmarks, but when my code runs it doesn't keep the font the same as in the template. Where in the code that you have posted do I put the code to set the font?

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,831

    Re: Copying data from excel into Word

    Quote Originally Posted by chrisellis250 View Post
    My word template has text in the bookmarks, but when my code runs it doesn't keep the font the same as in the template.
    When you're writing a string to a document, regardless of the source of that string, it takes on the formatting characteristics of the range it's written to. That said, you're also not creating a document from a Word template - you're merely opening an existing Word document.
    Quote Originally Posted by chrisellis250 View Post
    Where in the code that you have posted do I put the code to set the font?
    You might put it immediately after the corresponding Call statement, or anywhere between there and:
    objDoc.Close True

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,831

    Re: Copying data from excel into Word

    Cross-posted at: https://stackoverflow.com/questions/...35704#56435704 without even the courtesy of acknowledging the help - and comprehensive answer - you've already received here.
    Kindly read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copying data from excel to Word
    By chrisellis250 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2019, 12:30 PM
  2. [SOLVED] Excel - Word Automation - Copying data
    By Michael Staveley in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-03-2017, 04:30 AM
  3. [SOLVED] Copying data from Excel to Word
    By mango2015 in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 03-23-2016, 12:07 PM
  4. Copying data from excel to word
    By tessda in forum Word Formatting & General
    Replies: 1
    Last Post: 03-12-2016, 10:41 PM
  5. Replies: 3
    Last Post: 12-10-2014, 10:38 AM
  6. Copying data from multiple word docs into one excel sheet
    By X82 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-15-2013, 07:24 AM
  7. Copying Excel Data to Word
    By Flower R in forum Excel General
    Replies: 2
    Last Post: 12-28-2008, 07:20 PM

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