+ Reply to Thread
Results 1 to 18 of 18

Mail Merge from Excel?

  1. #1
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Mail Merge from Excel?

    I'm so close I can taste it. I'm trying to automatically merge this data directly from Excel. I need the code to specific the data source because I won't know where it is saved all the time so can't just setup the link in Word.

    This is my code so far, it opens word but seems to fall down on establishing the link with my spreadsheet and the Class Records$ range (maybe a problem with the SQL statement)

    Please Login or Register  to view this content.
    Can anyone help?
    Last edited by CraigMcKee; 10-16-2011 at 03:37 PM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Mail Merge from Excel?

    I smell moderator's anger because you omitted code tags around the VBA-code. Please read the forum rules.



  3. #3
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Re: Mail Merge from Excel?

    Oh no! What's that?

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Mail Merge from Excel?

    Hi CraigMcKee

    I believe what snb is saying is click on Forum Rules...see rule #3...it tells you how to correct your post.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Mail Merge from Excel?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Mail Merge from Excel?

    Quote Originally Posted by CraigMcKee View Post
    Oh no! What's that?
    Maybe if you read the Forum Rules you would know

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Mail Merge from Excel?

    Craig, welcome to the Forum! It is easy for a beginner to forums not to notice all of the rules. Rule 3 that requires code tags around code makes the forum better and helps us help you more easily. Plus, it just looks better. Some posts have hyperlinks that may not be all that easy to notice that it is a link. Forum Rules is also linked in the next to last tab left of the Log Out tab. The link is: http://www.excelforum.com/forum-rule...rum-rules.html

    I don't normally get that verbose about code tags but thought that it might help you. I normally use the advanced mode so clicking # is easy but then I am lazy.

    For your solution, I will look at it later today. For one thing, I noticed that you are using Word's wd constants. You need to set a Reference to the Word object if you want to use those or in MSWord's VBE Immediate Window type ?wdSendToPrinter and press Enter to get the numerical value and use that directly or define it in a variable.

    The way that I have done this sort of thing in past projects was to record a macro in MSWord and then adapt it to Excel using the prefix objects in a way similar to what you have. Yes, I do think that your SQL string needs work.

    One nice thing about this forum is that it lets you attach files. Simple example files helps those that help you all the better which is good for both parties.

  8. #8
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Re: Mail Merge from Excel?

    So, don't ask RoyUK for help I guess.

    Thank you Kenneth, as I'm sure you can tell I'm fairly new to this and have pieced that code together. I have turned on the Word Reference in VBA. I orginally had code that did what I wanted it to in Word but I was trying to remove that step. I didn't realise I could edit it and use it in Excel.

    I've attached the two files I'm trying to do this with.

    Thanks
    Attached Files Attached Files

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Mail Merge from Excel?

    Roy does a fine job as both moderator and problem solver. His first duty though is to moderate. Some like snb and myself might mention an infraction just to help the moderators out but we don't go into much detail sometimes, just a friendly reminder.

    I will check back late this afternoon or earlier if I get my honey-dos done early and work on the problem if no one has solved it first. This is one of the best forums so a solution is definitely possible and most likely today.

    cheers

  10. #10
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Re: Mail Merge from Excel?

    Kenneth, you are a legend! I went back to my original code in Word, as you suggested, and got it working! Thank you for pointing me in the right direction :D

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Mail Merge from Excel?

    But you shouldn't use this code in the first place.
    If you make a mergedocument in Word that you connect to a certain database (be it txt, Word, Excel or Access) and save that document it will save the connection to the database too.

    The only thing you have to do afterwards is opening the mergedocument-file (manually or 'by hand') and start the merging.

    Using VBA from Excel the only code you need is:

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Re: Mail Merge from Excel?

    The problem with that is word remembers the database explicitly and staff take a copy of these files and move them hence the drawn out process

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Mail Merge from Excel?

    That's primarily a matter of organizing.
    But apart from that you can save the actual workbook as a copy , replacing the file with which the wordmergedocument has been connected to.

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Re: Mail Merge from Excel?

    But, I don't know where they'll save it so it has to be a relative reference to the .xlsm That's why I've had to use the code to specify the link so if other teachers save it on pen drives, network drives, their desktop, at home etc it'll still work.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Mail Merge from Excel?

    If you can code confidently
    Please Login or Register  to view this content.

    you can also code confidently

    Please Login or Register  to view this content.
    Because, if Word can't find the connected file in the location that has been saved in the maindocument, it will take the file that is in the same directory as the maindocument with the specified datasource filename instead.

  16. #16
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Mail Merge from Excel?

    If you don't mind, can you post your code for others to learn?

    A few years ago, I did a macro where I changed the data source but it was all in MSWord VBA.

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Mail Merge from Excel?

    Please Login or Register  to view this content.
    My suggestion is:

    1. design a maindocument merge worddocument
    2. connect a mergedata file (Word, Excel, txt, Access) to it
    3. save that maindocument merge worddocument
    4. replace the mergedata file by a new datafile
    5. open (a copy of) the maindocument
    6. merge it with the attached file (that contains the new data).

  18. #18
    Forum Contributor
    Join Date
    10-14-2011
    Location
    London, England
    MS-Off Ver
    O365
    Posts
    180

    Re: Mail Merge from Excel?

    Please Login or Register  to view this content.
    Thanks snb but it's working fine so I'll leave it the way it is.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1