{"id":70673,"date":"2005-01-12T09:55:00","date_gmt":"2005-01-12T09:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/12\/how-can-i-access-a-type-library-from-within-a-script\/"},"modified":"2005-01-12T09:55:00","modified_gmt":"2005-01-12T09:55:00","slug":"how-can-i-access-a-type-library-from-within-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-access-a-type-library-from-within-a-script\/","title":{"rendered":"How Can I Access a Type Library From Within 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! How can I access a type library from within a script?<BR><BR>&#8212; XA<\/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, XA. For those of you unfamiliar with the term, a type library is a file or a component that contains type information for a COM object; type information describes the object and the things you can do with it. Among the information found in a type library are the constants used by that object. For example, the FileSystemObject includes these three constants and their values:<\/P><PRE class=\"codeSample\">ForReading (1)\nForWriting (2)\nForAppending (8)\n<\/PRE>\n<P>So what, you say? Well, any time you access a text file using the FileSystemObject, you need to reference one of these constants. For example, suppose you want to open the file C:\\Scripts\\Test.doc and write the current date and time to that file. To do that, you need a script similar to this:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objTextFile = objFSO.OpenTextFile _\n    (&#8220;c:\\scripts\\test.txt&#8221;, ForWriting)<\/p>\n<p>objTextFile.WriteLine Now\nobjTextFile.Close\n<\/PRE>\n<P>Notice two things here. First, when we open the file (using the OpenTextFile method), we include the constant ForWriting because we have to tell the FileSystemObject whether the file is being opened for reading, for writing, or for appending. Second, notice that &#8212; in the very first line of the script &#8212; we explicitly assign the value 2 to the constant ForWriting. That\u2019s because VBScript doesn\u2019t have access to type libraries; therefore, we have to tell our script that the value of ForWriting is equal to 2.<\/P>\n<P>By contrast, programming languages like Visual Basic can directly access a type library. If we were writing a Visual Basic program, we wouldn\u2019t have to define the constant ForWriting and assign it the value 2. Instead, we could simply use the constant in our code, and Visual Basic would access the type library and look up the value of ForWriting for us.<\/P>\n<P>In other words, programming languages have access to type libraries; therefore, you don\u2019t have to define any of the constants found in those libraries. VBScript does <I>not<\/I> have access to type libraries; consequently, you have to manually define any constant used in your script. (And in some cases &#8211; such as using ADO to write scripts that interact with databases &#8211; you might find yourself using scores of constants, all of which will have to be defined.)<\/P>\n<P>We know: you\u2019re sitting there saying \u201cCome on, come on, give us the workaround, we know you have one.\u201d And you\u2019re right, we do have a workaround; sort of. If you\u2019re writing a \u201ctraditional\u201d VBScript script (that is, something with a .vbs file extension), you\u2019re out of luck. But if you\u2019re willing to write a Windows Script File (something with a .wsf file extension), well, that\u2019s a different story.<\/P>\n<P>WSF files made their debut in WSH 5.6 and &#8211; to be honest &#8211; they haven\u2019t made much of an impact since. (For more information, see the <A href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/script56\/html\/c999e0fa-3986-41be-90a9-3f8ae3a38253.asp\" target=\"_blank\"><B>WSF documentation<\/B><\/A> on MSDN). The problem with WSF files is that the advantages they add (like providing access to a type library) are often outweighed by the disadvantages of having to deal with wrapping your script up in a bunch of XML tags. But if XML doesn\u2019t bother you and if you\u2019d really like to be able to access those type libraries, then you can use code similar to this. (Be sure and save this with a .wsf file extension and not a .vbs file extension.)<\/P><PRE class=\"codeSample\">&lt;package&gt;\n    &lt;job id=&#8221;Test&#8221;&gt;\n        &lt;object id=&#8221;objFSO&#8221; progid=&#8221;Scripting.FileSystemObject&#8221;\/&gt;\n         &lt;reference object=&#8221;Scripting.FileSystemObject&#8221; \/&gt;\n            &lt;script language=&#8221;VBScript&#8221;&gt;\n                Set objTextFile = objFSO.OpenTextFile _\n                    (&#8220;c:\\scripts\\test.txt&#8221;, ForWriting)\n                objTextFile.WriteLine Now\n                objTextFile.Close   \n            &lt;\/script&gt;\n    &lt;\/job&gt;\n&lt;\/package&gt;\n<\/PRE>\n<P>Of <I>course<\/I> it\u2019s crazy looking; that\u2019s why very few people use WSF files. If you look inside the &lt;script&gt; tag, however (which is where you place your VBScript code), you\u2019ll see the script we used earlier, with two exceptions: there\u2019s no code for creating the FileSystemObject, and there\u2019s no code defining the constant ForWriting. Instead, we use the &lt;object&gt; tag and the &lt;reference&gt; tag to take care of those two tasks.<\/P>\n<P>In our regular VBScript script, we used this line of code to create an instance of the FileSystemObject:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n<\/PRE>\n<P>In the WSF file, we use the &lt;object&gt; tag to accomplish this same feat:<\/P><PRE class=\"codeSample\">&lt;object id=&#8221;objFSO&#8221; progid=&#8221;Scripting.FileSystemObject&#8221;\/&gt;\n<\/PRE>\n<P>Note the ID assigned to the object: objFSO, the same object reference we used in our .vbs file.<\/P>\n<P>After we create our object reference, we can then get access to the type library (and all the constants defined within) by using the &lt;reference&gt; tag:<\/P><PRE class=\"codeSample\">&lt;reference object=&#8221;Scripting.FileSystemObject&#8221; \/&gt;\n<\/PRE>\n<P>That\u2019s all there is to it; we can now refer to any constant found in the FileSystemObject without having to define that constant. Cool, huh? Of course, it\u2019s not <I>always<\/I> this easy; sometimes you might have to use a GUID rather than a ProgId (like \u201cScripting.FileSystemObject\u201d) when creating an object and a reference. But this will at least get you started.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I access a type library from within a script?&#8212; XA Hey, XA. For those of you unfamiliar with the term, a type library is a file or a component that contains type information for a COM object; type information describes the object and the things you can do with it. [&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":[22,3,4,5],"class_list":["post-70673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-retrieving-input","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I access a type library from within a script?&#8212; XA Hey, XA. For those of you unfamiliar with the term, a type library is a file or a component that contains type information for a COM object; type information describes the object and the things you can do with it. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70673","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=70673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70673\/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=70673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}