{"id":470,"date":"2013-08-08T22:53:26","date_gmt":"2013-08-08T22:53:26","guid":{"rendered":"http:\/\/elbsolutions.com\/projects\/?p=470"},"modified":"2022-12-18T19:42:19","modified_gmt":"2022-12-19T01:42:19","slug":"vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename","status":"publish","type":"post","link":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/","title":{"rendered":"VBA for AutoCAD: getting a command line filename or a dialog window filename"},"content":{"rendered":"<p>The following code prompts for a full path or press E to invoke a dialog window typical to Microsoft to choose a &#8220;Save As&#8221; style windows path. Eventually an Excel file path that is fully valid and vetted will result.<\/p>\n<p>Here are 2 basic ideas wrapped within eachother<\/p>\n<p>a) Outer: Is there a valid filename (valid string) or did the user cancel (False),<br \/>\nb) Inner: Go get the filename via command line or optional dialog window \u2013 this might give garbage- hence, the above line has \u201cvalid\u201d<br \/>\nNote we can ONLY do this because you already using the Excel libraries as you are writing an Excel file with this path. There is equiv. commands without this cheat.<\/p>\n<p>You want to make your code contain TestVettedExcelName style code \u2013 then you can use the Functions with no issues.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n\nSub TestVettedExcelName()\nDim name As Variant\n\nname = GetVettedExcelName\n&lt;!--more--&gt;\nIf name = False Then\n' something went wrong\nMsgBox (\"No file selected or path is not valid\")\nElse\nMsgBox (\"the name is: \" &amp; name &amp; \"and is a valid string\")\nEnd If\n\nEnd Sub\n<\/pre>\n<p><!--more--><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\nFunction GetVettedExcelName() As Variant\n'will return a valid path OR false if the user cancelled\n\nDim FinalAnswer As Variant\nFinalAnswer = False\n\nDim name As Variant\nDim fso As Object\nDim MyFile As String\nDim fp As String\nDim this As String\nDim stoop As Boolean\nDim Continue As Boolean\nContinue = True\n\nname = \"\" ' a starting point\nWhile name &lt;&gt; False And Continue\nname = GetExcelName()\nIf name = False Then\n'user cancelled\nElseIf name &lt;&gt; \"\" Then\nSet fso = CreateObject(\"Scripting.FileSystemObject\")\n\nfp = StripFilename(CStr(name))\n\nthis = Dir(fp, vbDirectory)\nIf this = \"\" Then\nMsgBox (\"Path '\" &amp; name &amp; \"' invalid - specify whole path with excel filename\")\nBeep\nFinalAnswer = False\n\nElse\n'Hey - all is OK.\nBeep\nFinalAnswer = name\nContinue = False\nEnd If\nEnd If\n\nWend\nGetVettedExcelName = FinalAnswer\nEnd Function\n\nFunction StripFilename(sPathFile As String) As String\n\n'given a full path and file, strip the filename off the end and return the path\nDim fso As Object\nSet fso = CreateObject(\"Scripting.FileSystemObject\")\n\nStripFilename = fso.GetParentFolderName(sPathFile) &amp; \"\\\"\n\nExit Function\n\nEnd Function\nFunction GetExcelName() As Variant\n'will return a fully vetted path if it is realistic\n'else will return a an empty string if the user aborts (\"E\")\n\nDim FullPath As String\nDim stoop As Boolean\nDim existing As String\nDim FileToSave As Variant\n'try to get the path from the command line\nFullPath = ThisDrawing.Utility.GetString _\n(1, \"Enter the full windows file path &#x5B;E to exit, &lt;enter&gt; to use a dialog window]: &lt;enter&gt;\")\n\nFullPath = Replace(FullPath, \"\/\", \"\\\")\nIf UCase(FullPath) = \"E\" Then\nFullPath = \"&lt;none&gt;\"\nElse\n' The path could be a complete bunch of nonsense and not even be a valid directory or existing directory ... check that later\nIf FullPath = \"\" Then\nFileToSave = Excel.Application.GetSaveAsFilename(\"temp.xlsx\", \"Excel *.xls*(*.xls*),All *.*(*.*),\", 1, \"Please choose a file location to save\", \"Save\")\nIf FileToSave = False Then\n'user cancelled\nFullPath = \"&lt;none&gt;\"\nElse\n'user picked something and is likely NOT nonsense\nFullPath = FileToSave\nEnd If\nstoop = True\nElse\nIf InStr(1, FullPath, \"\\\") = 0 Then\n' no path was entered so assume my docs directory\nFullPath = Environ(\"USERPROFILE\") &amp; \"\\Documents\\\" &amp; FullPath\nEnd If\nEnd If\nEnd If\n\nIf FullPath = \"&lt;none&gt;\" Then\n'CANCELLED\nGetExcelName = False\n\nElseIf FullPath &lt;&gt; \"\" Then\nIf Len(FullPath) &gt; 3 Then\nIf LCase(Right(FullPath, 4)) = \".xls\" Then\n' all is ok\nElse\nIf Len(FullPath) &gt; 4 Then\nIf LCase(Right(FullPath, 5)) = \".xlsx\" Then\n' all is ok\nElse\nIf (Right(FullPath, 1) &lt;&gt; \".\") Then\nFullPath = FullPath + \".\"\nEnd If\nFullPath = FullPath + \"xlsx\"\nEnd If\nEnd If\n\nEnd If\nElse\nIf (Right(FullPath, 1) &lt;&gt; \".\") Then\nFullPath = FullPath + \".\"\nEnd If\nFullPath = FullPath + \"xlsx\"\nEnd If\nGetExcelName = FullPath\nEnd If\n\n<\/pre>\n<p>End Function<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following code prompts for a full path or press E to invoke a dialog window typical to Microsoft to choose a &#8220;Save As&#8221; style windows path. Eventually an Excel file path that is fully valid and vetted will result. Here are 2 basic ideas wrapped within eachother a) Outer: Is there a valid filename [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-general"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>VBA for AutoCAD: getting a command line filename or a dialog window filename - ELB Solutions.com Inc.<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VBA for AutoCAD: getting a command line filename or a dialog window filename - ELB Solutions.com Inc.\" \/>\n<meta property=\"og:description\" content=\"The following code prompts for a full path or press E to invoke a dialog window typical to Microsoft to choose a &#8220;Save As&#8221; style windows path. Eventually an Excel file path that is fully valid and vetted will result. Here are 2 basic ideas wrapped within eachother a) Outer: Is there a valid filename [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/\" \/>\n<meta property=\"og:site_name\" content=\"ELB Solutions.com Inc.\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-08T22:53:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-19T01:42:19+00:00\" \/>\n<meta name=\"author\" content=\"Etienne Bley\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Etienne Bley\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/\"},\"author\":{\"name\":\"Etienne Bley\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"headline\":\"VBA for AutoCAD: getting a command line filename or a dialog window filename\",\"datePublished\":\"2013-08-08T22:53:26+00:00\",\"dateModified\":\"2022-12-19T01:42:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/\"},\"wordCount\":631,\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/\",\"name\":\"VBA for AutoCAD: getting a command line filename or a dialog window filename - ELB Solutions.com Inc.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\"},\"datePublished\":\"2013-08-08T22:53:26+00:00\",\"dateModified\":\"2022-12-19T01:42:19+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"VBA for AutoCAD: getting a command line filename or a dialog window filename\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\",\"name\":\"ELB Solutions.com Inc.\",\"description\":\"Bringing all your IT Pieces together\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\",\"name\":\"Etienne Bley\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"caption\":\"Etienne Bley\"},\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/author\\\/etienne-bley\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"VBA for AutoCAD: getting a command line filename or a dialog window filename - ELB Solutions.com Inc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/","og_locale":"en_US","og_type":"article","og_title":"VBA for AutoCAD: getting a command line filename or a dialog window filename - ELB Solutions.com Inc.","og_description":"The following code prompts for a full path or press E to invoke a dialog window typical to Microsoft to choose a &#8220;Save As&#8221; style windows path. Eventually an Excel file path that is fully valid and vetted will result. Here are 2 basic ideas wrapped within eachother a) Outer: Is there a valid filename [&hellip;]","og_url":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/","og_site_name":"ELB Solutions.com Inc.","article_published_time":"2013-08-08T22:53:26+00:00","article_modified_time":"2022-12-19T01:42:19+00:00","author":"Etienne Bley","twitter_misc":{"Written by":"Etienne Bley","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/#article","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/"},"author":{"name":"Etienne Bley","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"headline":"VBA for AutoCAD: getting a command line filename or a dialog window filename","datePublished":"2013-08-08T22:53:26+00:00","dateModified":"2022-12-19T01:42:19+00:00","mainEntityOfPage":{"@id":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/"},"wordCount":631,"articleSection":["General"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/","url":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/","name":"VBA for AutoCAD: getting a command line filename or a dialog window filename - ELB Solutions.com Inc.","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/#website"},"datePublished":"2013-08-08T22:53:26+00:00","dateModified":"2022-12-19T01:42:19+00:00","author":{"@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"breadcrumb":{"@id":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/elbsolutions.com\/projects\/vba-for-autocad-getting-a-command-line-filename-or-a-dialog-window-filename\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/elbsolutions.com\/projects\/"},{"@type":"ListItem","position":2,"name":"VBA for AutoCAD: getting a command line filename or a dialog window filename"}]},{"@type":"WebSite","@id":"https:\/\/elbsolutions.com\/projects\/#website","url":"https:\/\/elbsolutions.com\/projects\/","name":"ELB Solutions.com Inc.","description":"Bringing all your IT Pieces together","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/elbsolutions.com\/projects\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39","name":"Etienne Bley","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","caption":"Etienne Bley"},"url":"https:\/\/elbsolutions.com\/projects\/author\/etienne-bley\/"}]}},"_links":{"self":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":5,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":3205,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/470\/revisions\/3205"}],"wp:attachment":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}