{"id":1691,"date":"2016-03-17T07:57:39","date_gmt":"2016-03-17T06:57:39","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=1691"},"modified":"2016-03-17T08:14:30","modified_gmt":"2016-03-17T07:14:30","slug":"excel-vba-kommandozeile-command-line-dos-parameter-an-excel-uebergeben","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=1691","title":{"rendered":"Excel VBA: Kommandozeile \/ Command Line \/ DOS Parameter an Excel \u00fcbergeben"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>An eine Excel-Datei soll ein Kommandozeilenparameter \u00fcbergeben werden, der in VBA weitergenutzt werden kann.<\/p>\n<h1>Ansatz &#8211; Approach<\/h1>\n<ul>\n<li>Nutzung der Kernel32.dll-Bibliothek<\/li>\n<li>Deklaration von Kernel32-Funktionen\n<ul>\n<li>GetCommandLineW<\/li>\n<li>lpString<\/li>\n<li>RtlMoveMemory<\/li>\n<\/ul>\n<\/li>\n<li>Erstellung einer Funktion f\u00fcr die Verwendeung<\/li>\n<li>Beispielaufruf<\/li>\n<\/ul>\n<h1>L\u00f6sung &#8211; Solution<\/h1>\n<p>Im Modulkopf von Modul1.bas (oder auch in der Arbeitsmappe) werden Funktionen deklariert:<\/p>\n<p><a href=\"https:\/\/www.capri-soft.de\/blog\/?attachment_id=1693\" rel=\"attachment wp-att-1693\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1693\" data-permalink=\"https:\/\/www.capri-soft.de\/blog\/?attachment_id=1693\" data-orig-file=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2016\/03\/Modulkopf.png?fit=692%2C334&amp;ssl=1\" data-orig-size=\"692,334\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Modulkopf\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2016\/03\/Modulkopf.png?fit=474%2C229&amp;ssl=1\" class=\"alignnone size-full wp-image-1693\" src=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2016\/03\/Modulkopf.png?resize=474%2C229&#038;ssl=1\" alt=\"Modulkopf\" width=\"474\" height=\"229\" srcset=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2016\/03\/Modulkopf.png?w=692&amp;ssl=1 692w, https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2016\/03\/Modulkopf.png?resize=300%2C145&amp;ssl=1 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>1.) Modulkopf (Modul1.bas): Deklaration der Kernel Funktionen f\u00fcr die Ausf\u00fchrung von Kommandozeilenparametern:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&#039; Used at module level to declare the default \r\n&#039; lower bound for array subscripts (Array starts with 0)\r\nOption Base 0\r\n\r\n&#039; Erzwingt die explizite Deklaration aller \r\n&#039; Variablen in einer Datei \r\nOption Explicit\r\n\r\n&#039; Deklariert die Funktionen aus kernel32.dll \r\nDeclare Function GetCommandLine Lib &quot;kernel32&quot; Alias &quot;GetCommandLineW&quot; () As Long\r\nDeclare Function lstrlenW Lib &quot;kernel32&quot; (ByVal lpString As Long) As Long\r\nDeclare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; (MyDest As Any, MySource As Any, ByVal MySize As Long)\r\n<\/pre>\n<p>2.) Erstellen einer einfachen Funktion, die die Ausf\u00fchrung von Kommandos anhand der kernel32.dll-Funktionen erlaubt.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nFunction CmdToSTr(Cmd As Long) As String\r\nDim Buffer() As Byte\r\nDim StrLen As Long\r\n   If Cmd Then\r\n      StrLen = lstrlenW(Cmd) * 2\r\n      If StrLen Then\r\n         ReDim Buffer(0 To (StrLen - 1)) As Byte\r\n         CopyMemory Buffer(0), ByVal Cmd, StrLen\r\n         CmdToSTr = Buffer\r\n      End If\r\n   End If\r\nEnd Function\r\n<\/pre>\n<p>3.) Wenn das Workbook (die Arbeitsmappe) ge\u00f6ffnet wird, soll der Parameter (hier \/cs:irgendwas ) zur Weiterverwendung genutzt werden k\u00f6nnen<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nPrivate Sub Workbook_Open()\r\n    Dim CmdRaw As Long\r\n\r\n    Dim CmdLine As String\r\n    Dim start As Integer\r\n    &#039;The return value is a pointer to the command-line string for the current process.\r\n    CmdRaw = GetCommandLine\r\n    CmdLine = CmdToSTr(CmdRaw)\r\n    \r\n    start = InStr(CmdLine, &quot;\/cs:&quot;)\r\n    ende = Len(CmdLine) - start - 3\r\n    Tabelle1.Cells(1, 1) = Right(CmdLine, ende)\r\n\r\n    mainForm.Show\r\nEnd Sub\r\n<\/pre>\n<p>4.) Aufruf der Datei<\/p>\n<p>C:\\Program Files (x86)\\Microsoft Office\\Office12\\EXCEL.exe C:\\tracematrix.xlsm \/cs:meinParameter<\/p>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D1691&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem An eine Excel-Datei soll ein Kommandozeilenparameter \u00fcbergeben werden, der in VBA weitergenutzt werden kann. Ansatz &#8211; Approach Nutzung der Kernel32.dll-Bibliothek Deklaration von Kernel32-Funktionen GetCommandLineW lpString RtlMoveMemory Erstellung einer Funktion f\u00fcr die Verwendeung Beispielaufruf L\u00f6sung &#8211; Solution Im Modulkopf von Modul1.bas (oder auch in der Arbeitsmappe) werden Funktionen deklariert: 1.) Modulkopf (Modul1.bas): Deklaration der Kernel &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=1691\" class=\"more-link\"><span class=\"screen-reader-text\">Excel VBA: Kommandozeile \/ Command Line \/ DOS Parameter an Excel \u00fcbergeben<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,12],"tags":[],"class_list":["post-1691","post","type-post","status-publish","format-standard","hentry","category-programmierung","category-vba"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-rh","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1691"}],"version-history":[{"count":5,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions"}],"predecessor-version":[{"id":1698,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions\/1698"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}