Consulting

Results 1 to 9 of 9

Thread: Macro to export cell values to Word

  1. #1

    Macro to export cell values to Word

    Dear all,

    I am new to this forum so forgive me if this should go in a different forum. Excel is the main or 'host program in what I am trying to achieve so I felt it best fit here.

    I have a spreadsheet which is nothing fancy at all - basic columns with manually entered information. I want to be able to highlight one row across 4 columns and have a macro button which will take the information and insert it into certain locations on a pre-existing Word document. I do not need it to save at this point (though this is functionality I may wish to incorporate later).

    For example, say columns A, B, C, and D hold my data. I then want to highlight A1 : D1 and click the macro button. This will take the cell values and insert them into the word document as follows:

    ----------------
    Dear A1,

    Thank you for your letter dated B1. Your query has been forwarded to our C1 department for further investigation. Should you have any queries please contact your HR representative, D1, who will be happy to assist you.

    Regards,

    Dominic
    ----------------

    Please note this is a very crude and fundamental example of what I am doing, but there is no need to go into great detail and hopefully this gives you the idea of what I am trying to achieve.

    If I need to be clearer on something then please do let me know.

    Any help you can offer greatly appreciated!

    Regards,

    Dominic

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Insert bookmarks in the fieds in the word document that you want to update, then just use Excel to startip Word, open the document, and insert the cell values into the bookmarks.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    xld, wouldn't mail merge be a better option? I mean if he is aware of the positions where the text goes, the form letter can be modified to suit, and later edited as per needs. I don't dispute bookmarks can be used with Excel being the host application, however, using Word as the host and utilizing mail merge is an easier way to go. Chances are that bookmarks can inadvertently be deleted either by creator or end user and this can throw the Excel macro out of whack. With mail merge, there are fewer chances of that happening. Just wondering if it can be done like this. Sorry if I've questioned your methods.

    Also, going by the example provided by Dominic there will have to be a loop that goes through all or some of the records. A mail merge option would allow the flexibility at any time to select which records need to be output and which don't need to be output.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I suppose maile merge is an option, I must admit I don't like mail merge, it is a clumsy implementation, I prefer to control it. Let' see what the OP comes back with.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm sure there are examples of this in the KB.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6

  7. #7
    Thank you for your ideas thus far.

    Looking through other threads I have managed to throw together this code, using the placeholder [REFNO] in my Word document:

    [VBA]Sub automateword()
    Dim WordApp As Object
    Dim DocWord As Word.Document
    Set WordApp = CreateObject("word.Application")
    Set DocWord = WordApp.Documents.Open("P:\Folder\File.doc")
    WordApp.Visible = True
    Set myRange = DocWord.Content
    myRange.Find.Execute FindText:="[REFNO]", ReplaceWith:=Cells(9, 1).Text

    End Sub[/VBA]

    This works great, however I am unable to integrate more find/replaces. I want the following integrated also. Can anyone help me just put these into my code so all 5 placeholders are replaced?

    [VBA]myRange.Find.Execute FindText:="[NAME]", ReplaceWith:=Cells(9, 2).Text
    myRange.Find.Execute FindText:="[DOB]", ReplaceWith:=Cells(9, 3).Text
    myRange.Find.Execute FindText:="[PPNO]", ReplaceWith:=Cells(9, 4).Text
    myRange.Find.Execute FindText:="[DATE]", ReplaceWith:=Cells(9, 5).Text[/VBA]

    Any help would be much appreciated!

    Thanks.

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Dominic,

    Not sure how much help I'd be at Word, but maybe use a template. It appears you are using text to find/replace like I used to in WordPerfect.

    Anyways, with the template in the same folder as the workbook, try:

    Option Explicit
        
    Sub exa()
    Dim WD As Object '<---Word.Application
    Dim DOC As Object '<--- Word.Document
        
        On Error Resume Next
        Set WD = GetObject(, "Word.Application")
        If Err.Number > 0 Then
            Set WD = CreateObject("Word.Application")
        End If
        On Error GoTo 0
        
        With WD
            .Visible = True
            Set DOC = .Documents.Add(Template:=ThisWorkbook.Path & "\MyTemplate.dot", _
                                     NewTemplate:=False, DocumentType:=0)
            With .Selection
                .Find.ClearFormatting
                .Find.Replacement.ClearFormatting
                With .Find
                    .Text = "[REFNO]"
                    .Replacement.Text = Cells(9, 1).Text
                    .Forward = True
                    .Wrap = 1
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                End With
                .Find.Execute Replace:=2
                
                .Find.Execute FindText:="[NAME]", ReplaceWith:=Cells(9, 2).Text, Replace:=2
                .Find.Execute FindText:="[DOB]", ReplaceWith:=Cells(9, 3).Text, Replace:=2
                .Find.Execute FindText:="[PPNO]", ReplaceWith:=Cells(9, 4).Text, Replace:=2
                .Find.Execute FindText:="[DATE]", ReplaceWith:=Cells(9, 5).Text, Replace:=2
            End With
        End With
    End Sub
    Hope that helps,

    Mark

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use bookmarks as I suggested earlier. It is trivial then

    [vba]

    Doc.Bookmarks("bmk_A1").Range.Text = Activesheet.Range("A1").Value
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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