PDA

View Full Version : Export field in excel to word document



YasserKhalil
07-17-2017, 01:58 PM
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

Kenneth Hobs
07-17-2017, 04:34 PM
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/index.php?option=com_content&view=article&id=169:automate-microsoft-word-from-excel-using-vba&catid=79&Itemid=475


'TypeText method
' http://www.excelforum.com/excel-programming/650672-populate-word-document-from-excel.html#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-programming/791302-excel-to-word-paragraph-and-page-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-programming/799070-import-text-fields-from-word.html
' Content Controls
' http://www.vbaexpress.com/forum/showthread.php?t=39654


'Add Hyperlink to Bookmark
' http://www.excelforum.com/excel-programming/664078-use-excel-vba-to-add-a-hyperlink-to-a-word-document.html#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-programming-vba-macros/940687-excel-export-to-ole-object-with-user-prompted-saveas-help.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/showthread.php?48227-Macro-to-copy-multiple-tables-in-a-single-sheet-from-excel-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-programming/682014-replace-word-in-ms-word-with-varable-from-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_Generating_a_Count_of_Word_Occurrences.html


' http://www.excelforum.com/excel-programming/794297-struggling-with-a-find-replace-macro-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-programming/796614-mail-merge-from-excel.html
' http://www.excelforum.com/excel-programming/798299-print-mail-merge-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/index.php?showtopic=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/fx4r06xq4mnlan6/ElephantsRusDocs.zip?dl=0


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

YasserKhalil
07-17-2017, 04:46 PM
Thanks a lot Mr. Kenneth for all these links. Hope to find solution among them
I am sure I will be drown :)

snb
07-18-2017, 04:06 AM
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

YasserKhalil
07-18-2017, 05:28 AM
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?

mdmackillop
07-18-2017, 06:03 AM
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

YasserKhalil
07-18-2017, 06:15 AM
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

snb
07-18-2017, 06:33 AM
@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.

mdmackillop
07-18-2017, 08:12 AM
Why adding redundancies ?
Didn't have Word open so I couldn't see a result from your code.

snb
07-18-2017, 09:08 AM
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

mdmackillop
07-18-2017, 09:43 AM
@ snb
Thanks