{"id":70753,"date":"2004-12-22T10:11:00","date_gmt":"2004-12-22T10:11:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/22\/how-can-i-save-word-documents-as-text-files-by-using-a-script\/"},"modified":"2004-12-22T10:11:00","modified_gmt":"2004-12-22T10:11:00","slug":"how-can-i-save-word-documents-as-text-files-by-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-word-documents-as-text-files-by-using-a-script\/","title":{"rendered":"How Can I Save Word Documents as Text Files By Using a Script?"},"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! I have a series of Word files. I would like to open each of these files and save them as plain-text files. Is there a way to do that using a script?<BR><BR>&#8212; CG<\/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, CG. Yes, you can do this quite easily with a script; in fact, with very few exceptions anything you can do within a Microsoft Office program you can do by using a script. Within Word, you can do a <B>File &#8211; Save As<\/B> and save a Word document as a text file. And you can do the very same thing using a script. In fact, you can do it using this script:<\/P><PRE class=\"codeSample\">Const wdFormatText = 2<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nSet objDoc = objWord.Documents.Open(&#8220;c:\\scripts\\mylog.doc&#8221;)\nobjDoc.SaveAs &#8220;c:\\scripts\\mylog.txt&#8221;, wdFormatText<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>We start off by creating a constant named <B>wdFormatText<\/B> and assigning it the value 2; this constant will be used to tell Word we want our new file saved as a text file. We then create an instance of Microsoft Word, and use the Open method to open the file C:\\Scripts\\MyLog.doc. After the document is open, we need just one line of code to save the Word document as a text file:<\/P><PRE class=\"codeSample\">objDoc.SaveAs &#8220;c:\\scripts\\mylog.txt&#8221;, wdFormatText\n<\/PRE>\n<P>As you can see, we call the SaveAs method, and we pass it two parameters: 1) <B>C:\\Scripts\\MyLog.txt<\/B>, which is the path for our new text file; and, 2) <B>wdFormatText<\/B>, which tells Word to save the file as plain text. That\u2019s it. We then use the Quit method to close our instance of Word.<\/P>\n<P>Two quick notes. First, you won\u2019t see any of this happening on screen; that\u2019s because, by default, Word runs in an invisible window any time you call it from a script. If you\u2019d prefer to see it pop up on the screen, save the file, and then disappear, use this code instead:<\/P><PRE class=\"codeSample\">Const wdFormatText = 2<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = TRUE\nSet objDoc = objWord.Documents.Open(&#8220;c:\\scripts\\mylog.doc&#8221;)\nobjDoc.SaveAs &#8220;c:\\scripts\\mylog.txt&#8221;, wdFormatText<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>The only difference here is that we\u2019ve set the Visible property to TRUE.<\/P>\n<P>Second, you can save your Word documents in formats other than plain text. For example, to save a Word document as an HTML file, use the constant wdFormatHTML (value = 8); to save a Word document as XML, use the constant wdFormatXML (value = 11).<\/P>\n<P>And at the risk of sounding like an informercial, if you\u2019d like to know more about scripting and Microsoft Office you might want to check out the Scripting Week 2 webcast <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=39644\"><B>If You Want Something Done Right, Let Microsoft Office Do It For You<\/B><\/A>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a series of Word files. I would like to open each of these files and save them as plain-text files. Is there a way to do that using a script?&#8212; CG Hey, CG. Yes, you can do this quite easily with a script; in fact, with very few exceptions anything [&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,395],"class_list":["post-70753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a series of Word files. I would like to open each of these files and save them as plain-text files. Is there a way to do that using a script?&#8212; CG Hey, CG. Yes, you can do this quite easily with a script; in fact, with very few exceptions anything [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70753","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=70753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70753\/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=70753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}