VBA Copy tables from excel to Wd

Fane1981
Mesaje: 6
Membru din: Mie Ian 23, 2019 2:16 pm

VBA Copy tables from excel to Wd

Mesaj de Fane1981 » Mie Ian 23, 2019 3:22 pm

Salut,
Nu am experiente in VBA, dar am un cod prin care doresc sa copiez tabele din excel in word (fiecare tabel din cate un sheet este adus intr-un bookmark din Wd)(capul de tabel este in A1:F1), singura problema a codului este ca atunci cand tabelul nu contine informatii imi aduce tot tabelul gol in word, iar eu doresc in aceasta situatie sa-mi aduca doar capul de tabel.
Ma puteti ajuta cu modificarea codului a.i sa respecte cerintele? Multumesc


Indigo-ONE
Mesaje: 435
Membru din: Mar Dec 11, 2018 8:54 pm

Re: VBA Copy tables from excel to Wd

Mesaj de Indigo-ONE » Mie Ian 23, 2019 9:17 pm

Poate am putea sa te ajutam daca pui si codul VBA, sa il putem eventual modifica.


"I fear the day that technology will surpass our human interaction. The world will have a generation of idiots."
Albert Einstein

Fane1981
Mesaje: 6
Membru din: Mie Ian 23, 2019 2:16 pm

Re: VBA Copy tables from excel to Wd

Mesaj de Fane1981 » Mie Ian 23, 2019 11:56 pm

Imi cer scuze, am crezut ca s.a atasat codul, cel putin daca nu ma insel i.am dat previzualizare si era atasat...cu scuzele de rigoare, am sa revin cu informatia, vineri, deoarece nu mai am acces la statia de lucru. Multumesc

Fane1981
Mesaje: 6
Membru din: Mie Ian 23, 2019 2:16 pm

Re: VBA Copy tables from excel to Wd

Mesaj de Fane1981 » Vin Ian 25, 2019 12:50 pm

<t>Sub ExportExcelDataToWordDocument()<br/>
‘Dim wdExcelApp As Application ‘Excel is the default library (optional)<br/>
Dim wdWordApp As Word.Application ‘Word app<br/>
Application.ScreenUpdating = False<br/>
‘ Creating a new instance of Word<br/>
Set wdWordApp = New Word.Application ‘instantiate a new instance of Word 2010<br/>
With wdWordApp<br/>
‘ Making Word Visible on the screen<br/>
.Visible = True ‘iff false, document is invisible.<br/>
.Activate ‘ make it the top pane, bring it to the front.<br/>
””””””””””””””””””””””””””””””””””””””””””””””’<br/>
‘ create a new Word Document based on the specified template<br/>
””””””””””””””””””””””””””””””””””””””””””””””’<br/>
.Documents.Add “C:Usersstefan.georgescuDesktopTemplate fisa de esantionare – ver. 5-2019.dotm”<br/>
‘as before, copy the whole table from sheet to clipboard.<br/>
Worksheets(“GestiuneSSC”).Activate<br/>
Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy<br/>
.Selection.GoTo what:=-1, Name:=”bookmark1” ‘ -1 means “wdgotobookmark”<br/>
.Selection.Paste ‘paste from the clipboard to the Word Doc.<br/>
‘****************<br/>
‘as before, copy the whole table from sheet to clipboard.<br/>
Worksheets(“AlimATM”).Activate<br/>
Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy<br/>
.Selection.GoTo what:=-1, Name:=”bookmark2” ‘ -1 means “wdgotobookmark”<br/>
.Selection.Paste ‘paste from the clipboard to the Word Doc.<br/>
‘****************<br/>
‘as before, copy the whole table from sheet to clipboard.<br/>
Worksheets(“DepRidAngajati”).Activate<br/>
Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy<br/>
.Selection.GoTo what:=-1, Name:=”bookmark3” ‘ -1 means “wdgotobookmark”<br/>
.Selection.Paste ‘paste from the clipboard to the Word Doc.<br/>
”””””””””””””””””””””””””””’<br/>
‘ Save WORD Document<br/>
”””””””””””””””””””””””””””’<br/>
Dim TheFileName As String<br/>
TheFileName = “C:Usersstefan.georgescuDesktopFisa.docx”<br/>
‘(SaveAs is for Office 2003 and earlier – deprecated)<br/>
.ActiveDocument.SaveAs2 TheFileName<br/>
‘replaces existing .doc iff exists<br/>
‘ Close Documents and Quit Word<br/>
.ActiveDocument.Close ‘close .DOCx<br/>
.Quit ‘exit Word<br/>
End With<br/>
Application.ScreenUpdating = True<br/>
‘MEMORY CLEANUP<br/>
Set wdWordApp = Nothing ‘garbage collection<br/>
‘Set wdExcelApp = Nothing ‘OPTIONAL<br/>
End Sub</t>

Indigo-ONE
Mesaje: 435
Membru din: Mar Dec 11, 2018 8:54 pm

Re: VBA Copy tables from excel to Wd

Mesaj de Indigo-ONE » Vin Ian 25, 2019 6:02 pm

Te rog sa atasezi codul, curatat de acele ramasite de html si sa folosesti iconul </> pentru a-l posta aici.
doresc sa copiez tabele din excel in word (fiecare tabel din cate un sheet este adus intr-un bookmark din Wd)(capul de tabel este in A1:F1),
Am cateva intrebari:
1. sunt mai multe tabele intr-o foaie sau numai tabelul care trebuie copiat?
2. In Word acele bookmark-uri sunt in pagini diferite? sau cum?
3. Acele tabele sunt create (cu Insert Table) sau sunt denumiri generice "tabele"?


"I fear the day that technology will surpass our human interaction. The world will have a generation of idiots."
Albert Einstein

Fane1981
Mesaje: 6
Membru din: Mie Ian 23, 2019 2:16 pm

Re: VBA Copy tables from excel to Wd

Mesaj de Fane1981 » Vin Ian 25, 2019 7:19 pm

Salut, Indigo, intre timp am reusit sa fac macro.ul sa mearga. Multumesc mult

Scrie răspuns

Înapoi la “Visual Basic for Application (VBA) cu Excel - Intrebari tehnice”