{"id":67033,"date":"2006-06-26T12:56:00","date_gmt":"2006-06-26T12:56:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/26\/how-can-i-copy-all-the-comments-from-an-excel-worksheet-to-a-word-document\/"},"modified":"2006-06-26T12:56:00","modified_gmt":"2006-06-26T12:56:00","slug":"how-can-i-copy-all-the-comments-from-an-excel-worksheet-to-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-copy-all-the-comments-from-an-excel-worksheet-to-a-word-document\/","title":{"rendered":"How Can I Copy All the Comments From an Excel Worksheet to a Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I copy all the comments from an Excel worksheet to a Word document?<BR><BR>&#8212; ED<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"TechNet Script Center\" border=\"0\" alt=\"TechNet Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, ED. You know, one of the drawbacks to calling yourself a \u201cScripting Guy\u201d is that people assume you\u2019re really more of a Scripting Fanatic. Every now and then we get emails that say, \u201cHey, Scripting Guy! I read your column on <I>X<\/I>. Well, I do <I>X<\/I> all the time, and I can do it faster and easier by using the GUI\/a command line tool\/magic. What do you have to say about <I>that<\/I>?\u201d<\/P>\n<P>Well, to tell you the truth, this is what have to say about that: \u201cGood for you.\u201d Believe it or not, we don\u2019t think you should script <I>everything<\/I> you do; in fact, depending on what it is you do, maybe you shouldn\u2019t even use scripts at all. If you find that the GUI or a command-line tool (or, yes, magic) works better for you, well, then that\u2019s probably the approach you should take. Do whatever works best for you.<\/P>\n<P>Of course, there will be times when there <I>is<\/I> no easy way to carry out a task using the GUI or a command-line tool. (We\u2019re not sure about magic; that falls into kind of a gray area.) <I>Those<\/I> are the times when scripting can be a real life-saver. And, by amazing coincidence, the chore of copying all the comments from an Excel worksheet to a Word document falls into that very category. Short of methodically copying and pasting each individual comment is there any way to accomplish this task using the GUI? Not as far as we know. And don\u2019t even ask about the command-line; needless to say, Microsoft Office doesn\u2019t exactly ship with a boatload of command-line tools. <\/P>\n<TABLE id=\"EPD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In all fairness, we should point out that magic <I>did <\/I>work in this case; however, the spell we cast had the unfortunate effect of turning Peter into a chicken. To put a new spin on an old joke, we\u2019d turn him back, except we need the eggs.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>With a script, however, copying all the comments from an Excel worksheet to a Word document is no more difficult than this:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(1)\nobjWorksheet.Activate<\/p>\n<p>Set colComments = objWorksheet.Comments<\/p>\n<p>For Each objComment in colComments\n    strComments = strComments &amp; objComment.Text &amp; vbCrlf &amp; vbCrLf\nNext<\/p>\n<p>objExcel.Quit<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection<\/p>\n<p>objSelection.TypeText strComments\n<\/PRE>\n<TABLE id=\"E6D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019re wondering, it took us just two minutes to create this script. Granted, we had an advantage because we copied some boilerplate code; in addition we also had a pretty good idea of how to carry out the task before we started. Still, you can see that scripting doesn\u2019t have to be a vast, time-consuming enterprise. After all, we were able to toss this script together in no time, even though Peter ate our first draft.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how does the script work? Well, we begin by creating an instance of the <B>Excel.Application<\/B> object and then set the <B>Visible<\/B> property to True. The latter is actually optional: the script works just fine if Excel is never visible on screen. We like to make the application visible simply so you can see what\u2019s going on. (Of course, if you have only a handful of comments in the spreadsheet then Excel will flash onto and off of the screen so quickly you might miss it anyway.)<\/P>\n<P>After creating an instance of Excel we then use these three lines of code to open the spreadsheet C:\\Scripts\\Test.xls and make Sheet1 (the worksheet where our comments reside) the active worksheet:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(1)\nobjWorksheet.Activate\n<\/PRE>\n<P>All of that is a prelude to this line of code:<\/P><PRE class=\"codeSample\">Set colComments = objWorksheet.Comments\n<\/PRE>\n<P>As it turns out, all the comments for a worksheet are stored in the <B>Comments<\/B> collection. To retrieve this collection all we have to do is create an object reference (here named colComments) to the worksheet\u2019s <B>Comments<\/B> property. It\u2019s that easy.<\/P>\n<P>Of course, that poses a new problem: what do we do with those comments once we have them? Turns out that this is no problem at all: because the comments are stored in a collection, we can set up a For Each loop to walk through all the items in that collection:<\/P><PRE class=\"codeSample\">For Each objComment in colComments\n    strComments = strComments &amp; objComment.Text &amp; vbCrlf &amp; vbCrLf\nNext\n<\/PRE>\n<P>As you can see, we don\u2019t do anything particularly complicated within that For Each loop. In fact, all we do is assign a value to a variable named strComments. The first time we run through the loop strComments gets assigned the existing value of strComments (which is nothing, seeing as how we haven\u2019t assigned a value to it yet) plus the <B>Text<\/B> of the first comment in the collection plus a pair of carriage return-linefeeds (vbCrLf). That\u2019s easy enough, right?<\/P>\n<P>Now, what happens the second time through the loop? Pretty much the same thing: strComments gets assigned the existing value of strComments (which, the second time around, will be the Text of comment 1) plus the Text of the second comment in the collection plus a pair of carriage return-linefeeds (vbCrLf). By the time we finish with the loop the Text of <I>all<\/I> of the comments will be stashed in the variable strComments.<\/P>\n<P>In other words, without resorting to copying, pasting, or magic spells we can easily grab all the comments from the worksheet.<\/P>\n<P>After we\u2019ve grabbed the comments we have no further use for Excel, so we simply call the <B>Quit<\/B> method to dismiss the application.<\/P>\n<P>Now we turn our attention to Microsoft Word. To begin with, we create an instance of the <B>Word.Application<\/B> object and set the <B>Visible <\/B>property to True. We then use the <B>Add<\/B> method to create a new, blank document to work with.<\/P>\n<TABLE id=\"E2F\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We know: if you\u2019ve never written scripts that interact with Microsoft Office applications much of this column likely sounds like gibberish. (Sadly, even if you <I>have<\/I> written scripts that interact with Microsoft Office applications this column occasionally sounds like gibberish. But that\u2019s a different story.) If you\u2019re new to Microsoft Office scripting you might find it useful to take a peek at the articles in our <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\"><B>Office Space archive<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As soon as we have a blank document we create an instance of Word\u2019s <B>Selection<\/B> object, then use this one line of code to write the extracted comments into the Word document:<\/P><PRE class=\"codeSample\">objSelection.TypeText strComments\n<\/PRE>\n<P>Is this really <I>that<\/I> easy? Give the script a try and see for yourself.<\/P>\n<P>Oh: and if anyone needs eggs, please let us know. We always considered Peter a so-so Scripting Guy, but we have to admit, he\u2019s one heckuva chicken!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I copy all the comments from an Excel worksheet to a Word document?&#8212; ED Hey, ED. You know, one of the drawbacks to calling yourself a \u201cScripting Guy\u201d is that people assume you\u2019re really more of a Scripting Fanatic. Every now and then we get emails that say, \u201cHey, Scripting [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,84,49,3,5],"class_list":["post-67033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I copy all the comments from an Excel worksheet to a Word document?&#8212; ED Hey, ED. You know, one of the drawbacks to calling yourself a \u201cScripting Guy\u201d is that people assume you\u2019re really more of a Scripting Fanatic. Every now and then we get emails that say, \u201cHey, Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67033"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67033\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}