{"id":67553,"date":"2006-04-12T09:09:00","date_gmt":"2006-04-12T09:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/12\/how-can-i-create-a-table-and-fill-the-first-column-with-a-range-of-dates\/"},"modified":"2006-04-12T09:09:00","modified_gmt":"2006-04-12T09:09:00","slug":"how-can-i-create-a-table-and-fill-the-first-column-with-a-range-of-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-table-and-fill-the-first-column-with-a-range-of-dates\/","title":{"rendered":"How Can I Create a Table and Fill the First Column With a Range of Dates?"},"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 create a table and automatically fill the first column in that table with a range of dates?<BR><BR>&#8212; DG<\/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=\"Script Center\" border=\"0\" alt=\"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, DG. You know, back in the old days we would have been a bit nervous about answering this question. Why? Well, it seems like Word should have a built-in command for doing something like this; however, we couldn\u2019t find one. (Not that we looked all <I>that<\/I> hard.) However, we did come up with a script that, while maybe not the most elegant solution in the world, will do the trick.<\/P>\n<P>So why does that make us nervous? Well, every now and then we\u2019ve answered a question like this one only to receive a barrage of email from people saying, \u201cYou know, instead of using 100 lines of crazy workaround code you could have done this in just a couple lines by calling the AutoCreateCalendar method.\u201d That was a problem, not only because it hurt our feelings (no, not really; Scripting Guys don\u2019t <I>have<\/I> feelings) but because everyone was right: we <I>could <\/I>havedone things better\/faster\/easier. Unfortunately &#8211; and for reasons we won\u2019t bother discussing &#8211; there wasn\u2019t much we could do with these better\/faster\/easier solutions we received.<\/P>\n<P>Now, of course, we have the <A href=\"http:\/\/null\/technet\/scriptcenter\/csc\/default.mspx\"><B>Community-Submitted Scripts Center<\/B><\/A>. As you read today\u2019s column, if you find yourself thinking, \u201cMan, I have a <I>much<\/I> better way to solve this problem,\u201d then by all means submit your solution to the Community Center. Trust us, the Scripting Guys do <I>not<\/I> believe that we have the answers to everyone\u2019s problems.<\/P>\n<P>Well, at least not to everyone\u2019s <I>scripting<\/I> problems. <\/P>\n<P>So what kind of solution did <I>we<\/I> come up with? This kind of solution:<\/P><PRE class=\"codeSample\">Const NUMBER_OF_ROWS = 1\nConst NUMBER_OF_COLUMNS = 2<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()<\/p>\n<p>Set objRange = objDoc.Range()\nobjDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS\nSet objTable = objDoc.Tables(1)<\/p>\n<p>objTable.Cell(1, 1).Range.Text = &#8220;Date&#8221;\nobjTable.Cell(1, 2).Range.Text = &#8220;Notes&#8221;<\/p>\n<p>dtmDate = #4\/1\/2006#\ndtmMonth = Month(dtmDate)\ni = 2<\/p>\n<p>Do While True\n    objTable.Rows.Add()\n    objTable.Cell(i, 1).Range.Text = dtmDate\n    dtmDate = dtmDate + 1\n    If Month(dtmDate) &lt;&gt; dtmMonth Then\n        Exit Do\n    End If\n    i = i +1\nLoop\n<\/PRE>\n<P>Before we start we should mention that very little of this code has to do with adding dates to a table; the vast majority of it involves creating the table in the first place. We won\u2019t delve into the whys and wherefores of table creation today. But don\u2019t despair: after all, we have an <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/jul05\/tips0726.mspx\"><B>Office Space article<\/B><\/A> that will fill you in on how to create a table in Microsoft Word.<\/P>\n<P>Without going into any detail, the script starts out by creating a new, visible instance of the <B>Word.Application<\/B> object, then uses the <B>Add<\/B> method to open a blank Word document. We then add a new table (one row, two columns) to this document, putting the heading <B>Date<\/B> in column 1 and the heading <B>Notes<\/B> in column 2. (Why? Because DG needs a table that looks like this: the date in one column and space to type notes in the second column. We should also add that DG needs to do this for a month at a time, e.g., he needs a row in the column for each and every day in the month of April.)<\/P>\n<P>That brings us to these three lines of code:<\/P><PRE class=\"codeSample\">dtmDate = #4\/1\/2006#\ndtmMonth = Month(dtmDate)\ni = 2\n<\/PRE>\n<P>In line one we simply assign the first day of the month (4\/1\/2006) to a variable named dtmDate. We then use the <B>Month<\/B> function to assign the month (April) of the starting date to a variable named dtmMonth. As you\u2019ll soon see, we\u2019ll use this variable to help determine when we\u2019ve reached the end of the month. <\/P>\n<P>Oh, yeah: what <I>about<\/I> line 3? In line 3 we assign the value 2 to the counter variable i. We\u2019ll use this variable to keep track of which row number in the table we\u2019re working with. We assign the variable the value 2 because that\u2019s the row where we want to put the first day of the month; remember, we\u2019ve already put headings in the first row of our table.<\/P>\n<P>And now we run headlong into this block of code:<\/P><PRE class=\"codeSample\">Do While True\n    objTable.Rows.Add()\n    objTable.Cell(i, 1).Range.Text = dtmDate\n    dtmDate = dtmDate + 1\n    If Month(dtmDate) &lt;&gt; dtmMonth Then\n        Exit Do\n    End If\n    i = i +1\nLoop\n<\/PRE>\n<P>What we\u2019re doing here is setting up a Do loop that will run until we reach the end of the month. Inside that loop we call the <B>Add<\/B> method to add a new row to the table; we then use this line of code to write the value of our starting date to column 1 of this new row:<\/P><PRE class=\"codeSample\">objTable.Cell(i, 1).Range.Text = dtmDate\n<\/PRE>\n<P>That\u2019s pretty easy; now it gets just a tiny bit trickier. Having written April 1, 2006 to the table we now need to write April 2, 2006 to the next row in the table. To do <I>that<\/I> we start off by adding one day to the variable dtmDate; that makes the value of dtmDate equal to April 2, 2006:<\/P><PRE class=\"codeSample\">dtmDate = dtmDate + 1\n<\/PRE>\n<P>That\u2019s good. But suppose dtmDate was equal to April 30, 2006. In that case, adding a day would make dtmDate equal to May 1, 2006, and we don\u2019t want any May dates in our table. Instead, if we\u2019ve added rows for each day in April we\u2019re done. How do we handle that situation? Here\u2019s how<\/P><PRE class=\"codeSample\">If Month(dtmDate) &lt;&gt; dtmMonth Then\n    Exit Do\nEnd If\n<\/PRE>\n<P>Here we\u2019re checking to see if the month of our current date (dtmDate) is still equal to our starting month of April (which, as you recall, we stashed in the variable dtmMonth). If it is, then we\u2019re going to increment the counter variable i by 1, loop around, and add a new row to the table. If the two months are different, however, that means we\u2019re run out of days in April. In that case, we use the <B>Exit Do<\/B> command to exit the Do loop and the script comes to and end.<\/P>\n<P>Will this <I>really<\/I> give us a table that includes a row for each day in the month of April? Give it a try and see for yourself. And if you can figure out a better way to accomplish this task, then please send your solution to the <A href=\"http:\/\/null\/technet\/scriptcenter\/csc\/default.mspx\"><B>Community-Submitted Scripts Center<\/B><\/A>. By our calculations that would make you one of just 950 million people who\u2019ve figured out how to do something better\/faster\/easier than the Scripting Guys. Select company indeed!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a table and automatically fill the first column in that table with a range of dates?&#8212; DG Hey, DG. You know, back in the old days we would have been a bit nervous about answering this question. Why? Well, it seems like Word should have a built-in command [&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":[84,49,3,5],"class_list":["post-67553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a table and automatically fill the first column in that table with a range of dates?&#8212; DG Hey, DG. You know, back in the old days we would have been a bit nervous about answering this question. Why? Well, it seems like Word should have a built-in command [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67553","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=67553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67553\/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=67553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}