{"id":1855,"date":"2014-07-04T14:36:22","date_gmt":"2014-07-04T14:36:22","guid":{"rendered":"http:\/\/galhano.com\/blog\/?p=1855"},"modified":"2014-07-04T15:30:23","modified_gmt":"2014-07-04T15:30:23","slug":"vba-environment-variables-environ","status":"publish","type":"post","link":"http:\/\/galhano.com\/blog\/?p=1855","title":{"rendered":"VBA environment variables (ENVIRON)"},"content":{"rendered":"<p>To see a list of variables, create a sub:<br \/>\n[vb]<\/p>\n<p>Sub ListEnvironmentVariables()<\/p>\n<p>    &#8216;each environment variable in turn<br \/>\n    Dim EnvironmentVariable As String<\/p>\n<p>    &#8216;the number of each environment variable<br \/>\n    Dim EnvironmentVariableIndex As Integer<\/p>\n<p>    &#8216;get first environment variables<br \/>\n    EnvironmentVariableIndex = 1<br \/>\n    EnvironmentVariable = Environ(EnvironmentVariableIndex)<\/p>\n<p>    &#8216;loop over all environment variables till there are no more<br \/>\n    Do Until EnvironmentVariable = &quot;&quot;<\/p>\n<p>    &#8216;get next e.v. and print out its value<br \/>\n    Debug.Print EnvironmentVariableIndex, EnvironmentVariable<\/p>\n<p>    &#8216;go on to next one<br \/>\n    EnvironmentVariableIndex = EnvironmentVariableIndex + 1<br \/>\n    EnvironmentVariable = Environ(EnvironmentVariableIndex)<\/p>\n<p>    Loop<\/p>\n<p>End Sub<br \/>\n [\/vb]<\/p>\n<p>click menu &#8220;View&#8221;  > &#8220;Immediate Window&#8221; and run it<br \/>\nDoesn&#8217;t work on macintosh<\/p>\n<p>[vb]<br \/>\nPrivate Sub Workbook_Open()<br \/>\n&#8216;Generates a GUID:<br \/>\nSheet1.Cells(8, 3).Value = Mid$(CreateObject(&quot;Scriptlet.TypeLib&quot;).GUID, 2, 36)  <\/p>\n<p>&#8216;environment variables:<br \/>\nSheet1.Cells(11, 3).Value = Environ$(&quot;computername&quot;)<br \/>\nSheet1.Cells(12, 3).Value = Environ$(&quot;username&quot;)<br \/>\nSheet1.Cells(13, 3).Value = Environ$(&quot;USERPROFILE&quot;)<\/p>\n<p>MsgBox &quot;Hello &quot; &amp; Environ$(&quot;username&quot;)<\/p>\n<p>End Sub<br \/>\n[\/vb]<\/p>\n<p>on Macintosh Excel you can try a AppleScript, something like:<br \/>\n[vb]<br \/>\nFunction GetUserNameMac() As String<br \/>\n    Dim sMyScript As String<\/p>\n<p>    sMyScript = &quot;set userName to short user name of (system info)&quot; &amp; vbNewLine &amp; &quot;return userName&quot;<\/p>\n<p>    GetUserNameMac = MacScript(sMyScript)<br \/>\nEnd Function<br \/>\n[\/vb]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To see a list of variables, create a sub: [vb] Sub ListEnvironmentVariables() &#8216;each environment variable in turn Dim EnvironmentVariable As String &#8216;the number of each environment variable Dim EnvironmentVariableIndex As Integer &#8216;get first environment variables EnvironmentVariableIndex = 1 EnvironmentVariable = Environ(EnvironmentVariableIndex) &#8216;loop over all environment variables till there are no more Do Until EnvironmentVariable = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[162],"tags":[],"class_list":["post-1855","post","type-post","status-publish","format-standard","hentry","category-vba","author-admin"],"_links":{"self":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1855"}],"version-history":[{"count":10,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1855\/revisions"}],"predecessor-version":[{"id":1869,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1855\/revisions\/1869"}],"wp:attachment":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1855"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}