PDA

View Full Version : Need vba code to export rows from excel to msword with image



jackhack99
06-17-2022, 07:48 PM
HI experts

i have set of data in excel start from A7 with header, i want copy data one by one into word file with image where stored in desktop

In column images saved path contain

please find the attachment and expected result in word

Regards
jackh

macropod
06-17-2022, 09:47 PM
Use a mailmerge. No VBA code required. See:
How to use the Mail Merge feature in Word to create and to print form letters that use the data from an Excel worksheet (microsoft.com) (https://support.microsoft.com/en-au/topic/how-to-use-the-mail-merge-feature-in-word-to-create-and-to-print-form-letters-that-use-the-data-from-an-excel-worksheet-d8709e29-c106-2348-7e38-13eecc338679)
Use mail merge for bulk email, letters, labels, and envelopes (microsoft.com) (https://support.microsoft.com/en-us/office/use-mail-merge-for-bulk-email-letters-labels-and-envelopes-f488ed5b-b849-4c11-9cff-932c49474705)
See also the Managing Mailmerge Graphics topic in the in the Mailmerge Tips and Tricks page at:
https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html

snb
06-18-2022, 05:20 AM
Prerequisite: an empty document in Word is open.


Sub M_snb()
sn = Sheet1.Cells(7, 1).CurrentRegion

With GetObject(, "word.application").ActiveDocument
For j = 2 To UBound(sn)
With .Tables.Add(.Paragraphs.last.Range, 15, 2)
For jj = 1 To UBound(sn, 2)
.Cell(jj, 1) = sn(1, jj)
.Cell(jj, 2) = sn(j, jj)
Next
End With
.Content.InsertAfter vbCr & vbCr & vbCr
Next
End With
End Sub

jackhack99
06-18-2022, 06:14 AM
i got error

snb
06-18-2022, 07:04 AM
VBA lesson 1.

Aussiebear
06-18-2022, 02:39 PM
@jackhack99. What snb meant to say was that because you used the term "Option Explicit" at the start of your section of code you need to define sn as range.

@snb Those of us who have been around this forum for a while recognise that you dislike (amongst other things) using "Option Explicit". Your types of coding doesn't help those who are new to VBA.

snb
06-20-2022, 12:54 AM
Liking isn't an issue in VBA. Redundancy is as in all forms of automation.
What is helpful will be determined by TS's and visitors, not by moderators (whose task isn't to criticize helpers; that doesn't help anyone).

georgiboy
06-20-2022, 01:59 AM
VBA lesson 1.
= Declare variables

macropod
06-20-2022, 02:11 AM
Liking isn't an issue in VBA. Redundancy is as in all forms of automation.
Variable declaration is hardly a redundancy...

snb
06-20-2022, 05:49 AM
Show me 1 macro in VBA that doesn't run correctly because of not declaring variables.
Have a look at https://www.snb-vba.eu/index_en.html

georgiboy
06-20-2022, 06:41 AM
Attached is an example of why I prefer to declare variables, another is being able to declare a variable that uses the least memory like an integer instead of a long etc... There are too many other reasons to list.

Attached is a silly sample of error handling a Boolean value without needing a separate If to handle values other than true, false. May not be the best example but it makes use of the Err.Number due to the declaration.

macropod
06-20-2022, 03:21 PM
Show me 1 macro in VBA that doesn't run correctly because of not declaring variables.
Have a look at https://www.snb-vba.eu/index_en.html
Clearly, in this case your own macro didn't. Need anyone say more? Or are you going to blame the OP for having 'Option Explicit' set?

As for practical proof of macros that won't run correctly without declaring variables, see for example:
http://www.vbaexpress.com/forum/showthread.php?69827-VBA-Automated-extraction-of-tables-from-word-file-and-table-processing&p=414547&viewfull=1#post414547
http://www.vbaexpress.com/forum/showthread.php?59909-Word-Content-Control-Data-Import-to-Excel-Table&p=363960&viewfull=1#post363960
etc.

Delete the variable declarations there and the macros won't run...

Aussiebear
06-21-2022, 01:55 AM
What is helpful will be determined by TS's and visitors, not by moderators (whose task isn't to criticize helpers; that doesn't help anyone).

Well I guess you are in for a rough night then snb