Consulting

Results 1 to 11 of 11

Thread: Export field in excel to word document

  1. #1

    Export field in excel to word document

    Hello everyone

    I have Template word document and I need to export specific cells from excel to the word document
    For example : range("C2") would be put in the Template document in <Formal> .. D2 would be put in the <Address>

    Can it be done easily or it is difficult?
    Thanks advanced for help
    Attached Files Attached Files

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    A DOCX file is not a DOTX file. I guess you would do a find and then a replace. That is a pretty unreliable method. Mail Merges, Form Fields, Content Controls, and Bookmarks are better ways to go.

    'Automate MSWord from another VBA Application like Excel
    ' http://www.globaliconnect.com/excel/...=79&Itemid=475


    'TypeText method
    ' http://www.excelforum.com/excel-prog...ml#post1946784
    ' http://www.excelforum.com/showthread.php?p=1946784
    ' http://vbaexpress.com/forum/showthread.php?p=169877
    ' http://vbaexpress.com/forum/showthread.php?t=24693
    ' http://www.excelforum.com/excel-prog...age-setup.html
    ' http://www.vbaexpress.com/forum/showthread.php?50947


    'Ger Plante, Get All Paragraphs Text
    'http://www.ozgrid.com/forum/showthread.php?t=200524


    'Copy from Excel, paste to Word
    'Lucas, http://vbaexpress.com/forum/showthread.php?p=178364
    'http://www.vbaexpress.com/forum/showthread.php?50947


    'FormFields
    ' http://www.mrexcel.com/forum/showthread.php?p=1639696
    ' http://www.mrexcel.com/forum/showthread.php?t=333200
    ' http://www.excelforum.com/excel-prog...from-word.html
    ' Content Controls
    ' http://www.vbaexpress.com/forum/showthread.php?t=39654


    'Add Hyperlink to Bookmark
    ' http://www.excelforum.com/excel-prog...ml#post2006430
    'Steiner, http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
    'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054


    'Save OLEObject as MSWord Document
    ' http://vbaexpress.com/forum/showthread.php?t=21619
    ' http://vbaexpress.com/forum/showthread.php?t=24292
    ' http://www.excelforum.com/excel-prog...html?p=3336342


    'Add Table to MSWord
    ' http://vbaexpress.com/forum/showthread.php?t=23975
    ' http://vbaexpress.com/forum/showthread.php?p=168731
    ' http://www.vbaexpress.com/forum/show...el-to-word-doc


    'Import Word Tables
    'vog, http://www.mrexcel.com/forum/showthread.php?t=382541
    'Ruddles, http://www.mrexcel.com/forum/showthread.php?t=524091


    'snb, Word Tables
    ' http://www.vbaexpress.com/forum/showthread.php?t=45520
    ' http://www.vbaexpress.com/forum/showthread.php?t=46472




    'Get Optionbutton info from MSWord DOC
    ' http://vbaexpress.com/forum/showthread.php?t=22454


    'FindReplace Text
    ' http://www.excelforum.com/excel-prog...-ms-excel.html
    ' http://www.vbaexpress.com/forum/showthread.php?t=38958
    ' http://www.vbaexpress.com/forum/showthread.php?p=250215
    ' http://www.vbaexpress.com/forum/showthread.php?t=42833
    ' http://support.microsoft.com/kb/240157
    ' http://word.tips.net/T001833_Generat...currences.html


    ' http://www.excelforum.com/excel-prog...o-to-word.html


    'Bookmarks
    ' http://vbaexpress.com/forum/showthread.php?p=185718
    'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054
    ' http://www.vbaexpress.com/forum/showthread.php?p=253277


    'Mail Merge
    ' http://www.excelforum.com/excel-prog...rom-excel.html
    ' http://www.excelforum.com/excel-prog...-document.html
    'Word 's Catalogue/Directory Mailmerge facility (the terminology depends on the Word version). _
    To see how to group records with any mailmerge data source supported by Word, _
    check out my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
    ' http://lounge.windowssecrets.com/ind...owtopic=731107
    ' or
    ' http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip
    ' Mail Merge from Excel to MSWord: ElephantsRus
    ' https://app.box.com/s/0zlydxinl10t23mifkrr
    ' and, https://www.dropbox.com/s/fx4r06xq4m...sDocs.zip?dl=0


    ' Control Word from Excel
    ' http://word.mvps.org/faqs/InterDev/C...WordFromXL.htm
    ' http://word.mvps.org/FAQs/InterDev/MakeAppInvisible.htm

  3. #3
    Thanks a lot Mr. Kenneth for all these links. Hope to find solution among them
    I am sure I will be drown

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,640
    How simply it can be done with docvariables:
    Run In Excel:

    Sub M_snb()
       For j = 1 To 6
          ThisWorkbook.Sheets(1).Cells(1, j) = "aa " & j ^ 2
       Next
    
       With CreateObject("Word.document")
          .Content.InsertAfter String(30, vbCr)
          For j = 1 To 6
             .Fields.Add .Paragraphs(3 * j).Range, 64, "snb_" & Format(j, "000"), 0
          Next
          For j = 1 To 6
             .variables("snb_" & Format(j, "000")).Value = ThisWorkbook.Sheets(1).Cells(1, j)
          Next
          .Fields.Update
       End With
    End Sub

  5. #5
    Thanks a lot Mr. Snb for the code but I didn't get it .. Can you attach a sample file .. May be I can't adopt it properly?

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub M_snb_MD()
        For j = 1 To 6
            ThisWorkbook.Sheets(1).Cells(1, j) = "aa " & j ^ 2
        Next
        Set wrdApp = CreateObject("Word.Application")
        Set wrddoc = wrdApp.Documents.Add
        wrdApp.Visible = True
        wrdApp.Activate
        With wrddoc
            .Content.InsertAfter String(30, vbCr)
            For j = 1 To 6
                .Fields.Add .Paragraphs(3 * j).Range, 64, "snb_" & Format(j, "000"), 0
            Next
            For j = 1 To 6
                .variables("snb_" & Format(j, "000")).Value = ThisWorkbook.Sheets(1).Cells(1, j)
            Next
            .Fields.Update
        End With
    End Sub
    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'

  7. #7
    Thanks a lot
    The code creates new document .. and I need to take a copy of the Template document and put some fields in proper place then save the new document with any name I can define by cell value for example

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,640
    @md

    Why adding redundancies ?

    @ Yssr

    Take your time; analyse the code instead of asking questions.
    If the task is beyond your capabilities you should tell your boss and return the assignment.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Why adding redundancies ?
    Didn't have Word open so I couldn't see a result from your code.
    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'

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,640
    Adding 1 line suffices. (the code is much faster in 'invisible' mode)

    Sub M_snb() 
        For j = 1 To 6 
            ThisWorkbook.Sheets(1).Cells(1, j) = "aa " & j ^ 2 
        Next 
         
        With CreateObject("Word.document") 
            .Content.InsertAfter String(30, vbCr) 
            For j = 1 To 6 
                .Fields.Add .Paragraphs(3 * j).Range, 64, "snb_" & Format(j, "000"), 0 
            Next 
            For j = 1 To 6 
                .variables("snb_" & Format(j, "000")).Value = ThisWorkbook.Sheets(1).Cells(1, j) 
            Next 
            .Fields.Update
            .windows(1).visible=true
        End With 
    End Sub

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    @ snb
    Thanks
    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'

Posting Permissions

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