Consulting

Results 1 to 13 of 13

Thread: Need vba code to export rows from excel to msword with image

  1. #1
    Banned VBAX Newbie
    Joined
    Jun 2022
    Posts
    2
    Location

    Need vba code to export rows from excel to msword with image

    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
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    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

  4. #4
    Banned VBAX Newbie
    Joined
    Jun 2022
    Posts
    2
    Location
    i got error
    Attached Images Attached Images
    Last edited by jackhack99; 06-18-2022 at 06:14 AM. Reason: got error

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    VBA lesson 1.

  6. #6
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    @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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    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).

  8. #8
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,158
    Location
    Quote Originally Posted by snb View Post
    VBA lesson 1.
    = Declare variables
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2401, Build 17231.20084

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by snb View Post
    Liking isn't an issue in VBA. Redundancy is as in all forms of automation.
    Variable declaration is hardly a redundancy...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    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

  11. #11
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,158
    Location
    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.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2401, Build 17231.20084

  12. #12
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by snb View Post
    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/show...l=1#post414547
    http://www.vbaexpress.com/forum/show...l=1#post363960
    etc.

    Delete the variable declarations there and the macros won't run...
    Last edited by macropod; 07-07-2022 at 12:13 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  13. #13
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Quote Originally Posted by snb View Post
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

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