{"id":406,"date":"2005-11-09T19:30:10","date_gmt":"2005-11-09T17:30:10","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2005\/11\/09\/have-fun-with-excel-log-accesses-to-a-file\/"},"modified":"2007-05-08T16:09:33","modified_gmt":"2007-05-08T14:09:33","slug":"have-fun-with-excel-log-accesses-to-a-file","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2005\/11\/have-fun-with-excel-log-accesses-to-a-file\/","title":{"rendered":"Have Fun with Excel : Log Accesses to a File"},"content":{"rendered":"<p>At work, colleagues and I are working on network shared directories. Just like everybody else I guess : you have a personal directory bound to your login username, and you are granted access to various shared directories where various people also have read \/ write permissions.<\/p>\n<p>I&#39;ve been doing some fun experiment these past few days at work : creating an Excel document with an appealing name (say, &quot;<em>salary_augmentation.xls<\/em>&quot;, burried deep into &quot;<em>Confidential\\HR\\Budget\\<\/em>&quot;) and silently logging usernames everytime someone loads this file.<\/p>\n<p>Here is how I did this :<br \/>\n<!--more--><\/p>\n<h2>Create a silly file<\/h2>\n<p>That&#39;s the easy step : create a file with a name so sexy that you&#39;re sure anyone seeing it will compulsively double-click it everytime they spot it. Any filename related to human resources and budget will surely trawl some fishes.<\/p>\n<p>Then add some irrelevant content. A flashy red box reading something like &quot;Did you really think I was going to tell you something secret&quot; will do the job : you don&#39;t need more than people going &quot;pfff moron&quot; and closing the file.<\/p>\n<h2>Add the following Visual Basic<\/h2>\n<p>Add the following Visual Basic code to your excel document (the menu should be something like Tools \/ Macro \/ Visual Basic Editor)<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">vb<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"vb\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #E56717;font-weight: bold\">Private<\/span> <span style=\"color: #E56717;font-weight: bold\">Sub<\/span> Workbook_Open()<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #151B8D;font-weight: bold\">Dim<\/span> filenum <span style=\"color: #151B8D;font-weight: bold\">As<\/span> <span style=\"color: #F660AB;font-weight: bold\">Integer<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #151B8D;font-weight: bold\">Dim<\/span> i <span style=\"color: #151B8D;font-weight: bold\">As<\/span> <span style=\"color: #F660AB;font-weight: bold\">Integer<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #151B8D;font-weight: bold\">Dim<\/span> fic <span style=\"color: #151B8D;font-weight: bold\">As<\/span> <span style=\"color: #F660AB;font-weight: bold\">String<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; filenum = FreeFile<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008000\">'\/\/ Get a free file number<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; logfile = <span style=\"color: #800000\">&quot;Z:\/path-to\/non-appealing\/directory\/sql.log&quot;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008000\">'\/\/ A text file located and named so that it won't be looked at<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #151B8D;font-weight: bold\">Open<\/span> logfile <span style=\"color: #8D38C9;font-weight: bold\">For<\/span> Append <span style=\"color: #151B8D;font-weight: bold\">As<\/span> filenum<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008000\">'\/\/ open the file<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #151B8D;font-weight: bold\">Print<\/span> #filenum, Environ(<span style=\"color: #800000\">&quot;UserName&quot;<\/span>) &amp; Chr(13)<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008000\">'\/\/ write username to the file<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #8D38C9;font-weight: bold\">Close<\/span> filenum<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008000\">'\/\/ Close the file<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #8D38C9;font-weight: bold\">End<\/span> <span style=\"color: #E56717;font-weight: bold\">Sub<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Be sure to add this macro code to your Excel document and not in your personal Macros (<em>perso.xls<\/em>). To do so, add the Visual Basic code to <em>ThisWorkBook<\/em> of your current document.<\/p>\n<h2>Create a log text file<\/h2>\n<p>Create a blank text file, named and located so that it won&#39;t be interesting. Something like <em>sql.log<\/em> in the most boring directory you&#39;ll find. Modify the code above so that <strong>logfile<\/strong> points to this file.<\/p>\n<p>Even better, make this file hidden if possible (right click, Properties, check &quot;Hidden file&quot;)<\/p>\n<h2>Wait.<\/h2>\n<p>From now on, everytime someone loads your sexy Excel document, their username will be appended to your log file. Just check back from time to time and just be amazed by the number of employees who just seem to wander from directory to directory, looking for stuff that are normally not of their business :-)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Curious about who&#39;s looking into your Excel documents ?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,120,87,121,102],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","tag-code","tag-excel","tag-microsoft","tag-visual-basic","tag-work"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}