{"id":11668,"date":"2022-09-27T18:19:16","date_gmt":"2022-09-27T09:19:16","guid":{"rendered":"https:\/\/prodskill.com\/?p=11668"},"modified":"2022-10-04T23:19:20","modified_gmt":"2022-10-04T14:19:20","slug":"excel-vba-coding-pattern-windows-api-ptrsafe","status":"publish","type":"post","link":"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/","title":{"rendered":"Specify PtrSafe to use both 32bit and 64bit Excel when importing Windows API"},"content":{"rendered":"<p>Learn about the Windows API PtrSafe declaration. When coding with Excel VBA, you can check the causes and solutions of PtrSafe-related errors that you occasionally encounter while importing and using Windows APIs.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">&lt;&lt;Table of Contents&gt;&gt;<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#1_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%EB%B0%9C%EC%83%9D_%EC%98%88%EC%8B%9C_%EC%BD%94%EB%93%9C\" >1. Example Code of Windows API PtrSafe Error Occurrence<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#2_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%EB%A9%94%EC%8B%9C%EC%A7%80\" >2. Windows API PtrSafe Error Message<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#3_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%EB%B0%9C%EC%83%9D_%EC%9B%90%EC%9D%B8\" >3. What Causes Windows API PtrSafe Errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#4_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%ED%95%B4%EA%B2%B0_%EB%B0%A9%EB%B2%95\" >4. How to fix Windows API PtrSafe error<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#5_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%ED%95%B4%EA%B2%B0_%EC%98%88%EC%8B%9C_%EC%BD%94%EB%93%9C\" >5. Example code for solving Windows API PtrSafe error<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#51_32bit_64bit_%EC%97%91%EC%85%80%EC%97%90%EC%84%9C_%ED%98%BC%EC%9A%A9%ED%95%98%EC%97%AC_%EC%82%AC%EC%9A%A9%EB%90%98%EB%8A%94_%EA%B2%BD%EC%9A%B0\" >5.1. In case of being mixed and used in 32bit and 64bit Excel<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/#52_64bit_%EC%97%91%EC%85%80%EC%97%90%EC%84%9C%EB%A7%8C_%EC%82%AC%EC%9A%A9%EB%90%98%EB%8A%94_%EA%B2%BD%EC%9A%B0\" >5.2. If used only in 64bit Excel<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1._\uc624\ub958_\ubc1c\uc0dd_\uc608\uc2dc_\ucf54\ub4dc\"><span class=\"ez-toc-section\" id=\"1_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%EB%B0%9C%EC%83%9D_%EC%98%88%EC%8B%9C_%EC%BD%94%EB%93%9C\"><\/span>1. Example Code of Windows API PtrSafe Error Occurrence<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This code imports and declares Windows API Sleep, OutputDebugString.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\" data-no-auto-translation=\"\">Public Declare Sub Sleep Lib \"kernel32\" (ByVal dwMilliseconds As Long)\nPrivate Declare Sub OutputDebugString Lib \"kernel32\" Alias \"OutputDebugStringA\" (ByVal lpOutputString As String)<\/pre>\n\n\n\n<p>(Note: the above code is <a href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-logging-outputdebugstring-debugview\/\">Logging Pattern: Utilizing OutputDebugString and DebugView \u2013 Productivity Skill (prodskill.com)<\/a> This is the code introduced in .)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2._\uc624\ub958_\uba54\uc2dc\uc9c0\"><span class=\"ez-toc-section\" id=\"2_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%EB%A9%94%EC%8B%9C%EC%A7%80\"><\/span>2. Windows API PtrSafe Error Message<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When editing or compiling VBA code, the following error occurs.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png\"><img loading=\"lazy\" decoding=\"async\" width=\"410\" height=\"185\" src=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png\" alt=\"VBA \ucef4\ud30c\uc77c \uc624\ub958: Declare \ubb38 PtrSafe \ud2b9\uc131 \ud45c\uc2dc \ud544\uc694\" class=\"wp-image-11669\" srcset=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png 410w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204-300x135.png 300w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204-18x8.png 18w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204-24x11.png 24w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204-36x16.png 36w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204-48x22.png 48w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/a><figcaption>VBA compilation error: Declare statement PtrSafe attribute needs to be marked<\/figcaption><\/figure>\n<\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br>Microsoft Visual Basic for Applications<br>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<\/p><p>This is a compilation error:<\/p><p>You will need to update the code in this project to use it on 64 bit systems. Review and update the Declare statement and mark it with the PtrSafe attribute.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3._\uc624\ub958_\ubc1c\uc0dd_\uc6d0\uc778\"><span class=\"ez-toc-section\" id=\"3_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%EB%B0%9C%EC%83%9D_%EC%9B%90%EC%9D%B8\"><\/span>3. What Causes Windows API PtrSafe Errors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>64bit has been supported since MS-Office 2010, and it occurs because the PtrSafe keyword must be specified when imprting the Windows API in 64bit Excel after this.<\/p>\n\n\n\n<p>Reference:&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/previous-versions\/office\/troubleshoot\/office-developer\/compile-error-editing-vba-macro\" target=\"_blank\" rel=\"noreferrer noopener\">The code in this project must be updated for use on 64-bit systems \u2013 Office | Microsoft Docs<\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205.png\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"731\" src=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205.png\" alt=\"Compile error when you edit a VBA macro in the 64-bit version of an Office 2010 program\" class=\"wp-image-11670\" srcset=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205.png 720w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205-295x300.png 295w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205-12x12.png 12w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205-80x80.png 80w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205-24x24.png 24w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205-36x36.png 36w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-205-48x48.png 48w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><figcaption>Compile error when you edit a VBA macro in the 64-bit version of an Office 2010 program<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Some excerpts from the above URL are pasted below.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p class=\"has-large-font-size\"><strong>Symptoms<\/strong><\/p>\n\n\n\n<p>Consider the following scenario:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You write a Microsoft Visual Basic for Applications (VBA) macro code that uses Declare statements.<\/li><li>Your VBA macro code uses compilation constants. For example, your macro code uses one the following compilation constants:<ul><li>#If VBA7<\/li><li>#If Win64<\/li><\/ul><\/li><li>You use an #Else block in a conditional block. In the #Else block, you use syntax for a Declare statement designed to run in Microsoft Visual Basic for Applications 6.0.<\/li><li>You edit the code in a 64-bit version of a Microsoft Office 2010 program.<\/li><li>You try to change the Declare statement in the #Else block.<\/li><\/ul>\n\n\n\n<p>In this scenario, you receive the following error message:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\" data-no-auto-translation=\"\">Microsoft Visual Basic for Applications\n\nCompile error:\n\nThe code in this project must be updated for use on 64-bit\nsystems. Please review and update Declare statements and then\nmark them with the PtrSafe attribute.<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4._\uc624\ub958_\ud574\uacb0_\ubc29\ubc95\"><span class=\"ez-toc-section\" id=\"4_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%ED%95%B4%EA%B2%B0_%EB%B0%A9%EB%B2%95\"><\/span>4. How to fix Windows API PtrSafe error<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The VBA version of MS-Office 2010 is 7.0, and the previous one is 6.x. Since the PtrSafe keyword is required from VBA 7.0, the Windows API import code can be divided and declared according to whether the VBA version is later than 7.0 or not.<\/p>\n\n\n\n<p>Reference:&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/ptrsafe-keyword\" target=\"_blank\" rel=\"noreferrer noopener\">PtrSafe keyword (VBA) | Microsoft Docs<\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206.png\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"583\" src=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206.png\" alt=\"PtrSafe keyword\" class=\"wp-image-11671\" srcset=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206.png 718w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206-300x244.png 300w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206-15x12.png 15w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206-24x19.png 24w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206-36x29.png 36w, https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-206-48x39.png 48w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/a><figcaption>PtrSafe keyword<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Some excerpts from the above URL are pasted below.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p class=\"has-large-font-size\"><strong>PtrSafe keyword<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Article<\/li><li>09\/14\/2021<\/li><li>2 minutes to read<\/li><li>5 contributors<\/li><\/ul>\n\n\n\n<p>The\u00a0<strong>PtrSafe<\/strong>\u00a0keyword is used in this context:\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/declare-statement\" target=\"_blank\" rel=\"noreferrer noopener\">Declare statement<\/a>.<\/p>\n\n\n\n<p><strong>Declare<\/strong>\u00a0statements with the\u00a0<strong>PtrSafe<\/strong>\u00a0keyword is the recommended syntax. Declare statements that include\u00a0<strong>PtrSafe<\/strong>\u00a0work correctly in the VBA7 development environment on both 32-bit and 64-bit platforms only after all data types in the\u00a0<strong>Declare<\/strong>\u00a0statement (parameters and return values) that need to store 64-bit quantities are updated to use\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/longlong-data-type\" target=\"_blank\" rel=\"noreferrer noopener\">LongLong<\/a>\u00a0for 64-bit integrals or\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/longptr-data-type\" target=\"_blank\" rel=\"noreferrer noopener\">LongPtr<\/a>\u00a0for pointers and handles.<\/p>\n\n\n\n<p>To ensure backwards compatibility with VBA version 6 and earlier, use the following construct:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\" data-no-auto-translation=\"\">#If VBA7 Then \nDeclare PtrSafe Sub... \n#Else \nDeclare Sub... \n#EndIf\n<\/pre>\n\n\n\n<p>When running in 64-bit versions of Office,&nbsp;<strong>Declare<\/strong>&nbsp;statements must include the&nbsp;<strong>PtrSafe<\/strong>&nbsp;keyword. The&nbsp;<strong>PtrSafe<\/strong>&nbsp;keyword asserts that a&nbsp;<strong>Declare<\/strong>&nbsp;statement is safe to run in 64-bit development environments.<\/p>\n\n\n\n<p>Adding the\u00a0<strong>PtrSafe<\/strong>\u00a0keyword to a\u00a0<strong>Declare<\/strong>\u00a0statement only signifies that the\u00a0<strong>Declare<\/strong>\u00a0The statement explicitly targets 64-bits. All data types within the statement that need to store 64-bits (including return values and parameters) must still be modified to hold 64-bit quantities by using either\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/longlong-data-type\" target=\"_blank\" rel=\"noreferrer noopener\">LongLong<\/a>\u00a0for 64-bit integrals or\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/longptr-data-type\" target=\"_blank\" rel=\"noreferrer noopener\">LongPtr<\/a>\u00a0for pointers and handles.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p>You just need to determine whether a \u201cVBA7\u201d constant or a \u201cWin64\u201d constant is defined. In this case, use the Compiler Directive #If syntax.<\/p>\n\n\n\n<p>The code is structured like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\" data-no-auto-translation=\"\">'\ubc29\ubc951\n#If VBA7 Then\n    '64bit Windows API import\n#Else\n    '32bit Windows API import\n#End If\n\n'\ubc29\ubc952\n#If Win64 Then\n    '64bit Windows API import\n#Else\n    '32bit Windows API import\n#End If\n\n'\ubc29\ubc951, \ubc29\ubc952\ub294 \ub3d9\uc77c\ud55c \ud6a8\uacfc\uac00 \uc788\uc73c\ubbc0\ub85c \ud55c \ubc29\ubc95\uc744 \uc120\ud0dd\ud558\uba74 \ub428<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5._\uc624\ub958_\ud574\uacb0_\uc608\uc2dc_\ucf54\ub4dc\"><span class=\"ez-toc-section\" id=\"5_Windows_API_PtrSafe_%EC%98%A4%EB%A5%98_%ED%95%B4%EA%B2%B0_%EC%98%88%EC%8B%9C_%EC%BD%94%EB%93%9C\"><\/span>5. Example code for solving Windows API PtrSafe error<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5.1._32bit,_64bit_\uc5d1\uc140\uc5d0\uc11c_\ud63c\uc6a9\ud558\uc5ec_\uc0ac\uc6a9\ub418\ub294_\uacbd\uc6b0\"><span class=\"ez-toc-section\" id=\"51_32bit_64bit_%EC%97%91%EC%85%80%EC%97%90%EC%84%9C_%ED%98%BC%EC%9A%A9%ED%95%98%EC%97%AC_%EC%82%AC%EC%9A%A9%EB%90%98%EB%8A%94_%EA%B2%BD%EC%9A%B0\"><\/span>5.1. In case of being mixed and used in 32bit and 64bit Excel<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\" data-no-auto-translation=\"\">#If VBA7 Then 'For 64 Bit Systems\n    Public Declare PtrSafe Sub Sleep Lib \"kernel32\" (ByVal dwMilliseconds As LongPtr)\n    Private Declare PtrSafe Sub OutputDebugString Lib \"kernel32\" Alias \"OutputDebugStringA\" (ByVal lpOutputString As String)\n#Else 'For 32 Bit Systems\n    Public Declare Sub Sleep Lib \"kernel32\" (ByVal dwMilliseconds As Long)\n    Private Declare Sub OutputDebugString Lib \"kernel32\" Alias \"OutputDebugStringA\" (ByVal lpOutputString As String)\n#End If<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5.2._64bit_\uc5d1\uc140\uc5d0\uc11c\ub9cc_\uc0ac\uc6a9\ub418\ub294_\uacbd\uc6b0\"><span class=\"ez-toc-section\" id=\"52_64bit_%EC%97%91%EC%85%80%EC%97%90%EC%84%9C%EB%A7%8C_%EC%82%AC%EC%9A%A9%EB%90%98%EB%8A%94_%EA%B2%BD%EC%9A%B0\"><\/span>5.2. If used only in 64bit Excel<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you are guaranteed that it will only be used in 64-bit Excel, use only one statement that specifies the PtrSafe keyword.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\" data-no-auto-translation=\"\">Public Declare PtrSafe Sub Sleep Lib \"kernel32\" (ByVal dwMilliseconds As LongPtr)\nPrivate Declare PtrSafe Sub OutputDebugString Lib \"kernel32\" Alias \"OutputDebugStringA\" (ByVal lpOutputString As String)<\/pre>","protected":false},"excerpt":{"rendered":"<p>Learn about the Windows API PtrSafe declaration. When coding with Excel VBA, you can check the causes and solutions of PtrSafe-related errors that you occasionally encounter while importing and using Windows APIs. 1. Example code for Windows API PtrSafe error This is the code that imports and declares Windows API Sleep, OutputDebugString. (reference:...<\/p>","protected":false},"author":1,"featured_media":11669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[17,74,75,104,105,108],"class_list":["post-11668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-vba-coding-pattern","tag-vba","tag-74","tag-codding-pattern","tag-32bit","tag-64bit","tag-ptrsafe"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815 - \uc0dd\uc0b0\uc131 Skill<\/title>\n<meta name=\"description\" content=\"Windows API PtrSafe \uc120\uc5b8\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf8\ub2e4. \uc5d1\uc140 VBA\ub85c \ucf54\ub529\ud558\ub2e4 \ubcf4\uba74 Windows API\ub97c Import\ud558\uc5ec \uc0ac\uc6a9\ud558\ub2e4\uac00 \uac04\ud639 \ub9cc\ub098\ub294 PtrSafe \uad00\ub828 \uc624\ub958\uc758 \uc6d0\uc778\uacfc \ud574\uacb0\ubc29\ubc95\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815 - \uc0dd\uc0b0\uc131 Skill\" \/>\n<meta property=\"og:description\" content=\"Windows API PtrSafe \uc120\uc5b8\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf8\ub2e4. \uc5d1\uc140 VBA\ub85c \ucf54\ub529\ud558\ub2e4 \ubcf4\uba74 Windows API\ub97c Import\ud558\uc5ec \uc0ac\uc6a9\ud558\ub2e4\uac00 \uac04\ud639 \ub9cc\ub098\ub294 PtrSafe \uad00\ub828 \uc624\ub958\uc758 \uc6d0\uc778\uacfc \ud574\uacb0\ubc29\ubc95\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/\" \/>\n<meta property=\"og:site_name\" content=\"\uc0dd\uc0b0\uc131 Skill\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-27T09:19:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-04T14:19:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png\" \/>\n\t<meta property=\"og:image:width\" content=\"410\" \/>\n\t<meta property=\"og:image:height\" content=\"185\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Zerom\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Zerom\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/\"},\"author\":{\"name\":\"Zerom\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/#\\\/schema\\\/person\\\/bbad0870c78008c82edbe0960fe768bd\"},\"headline\":\"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815\",\"datePublished\":\"2022-09-27T09:19:16+00:00\",\"dateModified\":\"2022-10-04T14:19:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/\"},\"wordCount\":411,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/#\\\/schema\\\/person\\\/bbad0870c78008c82edbe0960fe768bd\"},\"image\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/image-204.png\",\"keywords\":[\"\uc5d1\uc140VBA\",\"\ucf54\ub529\ud328\ud134\",\"Codding Pattern\",\"32bit\",\"64bit\",\"PtrSafe\"],\"articleSection\":[\"\uc5d1\uc140 VBA \ucf54\ub529\ud328\ud134\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/\",\"url\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/\",\"name\":\"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815 - \uc0dd\uc0b0\uc131 Skill\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/image-204.png\",\"datePublished\":\"2022-09-27T09:19:16+00:00\",\"dateModified\":\"2022-10-04T14:19:20+00:00\",\"description\":\"Windows API PtrSafe \uc120\uc5b8\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf8\ub2e4. \uc5d1\uc140 VBA\ub85c \ucf54\ub529\ud558\ub2e4 \ubcf4\uba74 Windows API\ub97c Import\ud558\uc5ec \uc0ac\uc6a9\ud558\ub2e4\uac00 \uac04\ud639 \ub9cc\ub098\ub294 PtrSafe \uad00\ub828 \uc624\ub958\uc758 \uc6d0\uc778\uacfc \ud574\uacb0\ubc29\ubc95\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#primaryimage\",\"url\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/image-204.png\",\"contentUrl\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/image-204.png\",\"width\":410,\"height\":185},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/excel-vba-coding-pattern-windows-api-ptrsafe\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/#website\",\"url\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/\",\"name\":\"\uc0dd\uc0b0\uc131 Skill\",\"description\":\"Meta Thinking, Meta Working\",\"publisher\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/#\\\/schema\\\/person\\\/bbad0870c78008c82edbe0960fe768bd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/prodskill.com\\\/ko\\\/#\\\/schema\\\/person\\\/bbad0870c78008c82edbe0960fe768bd\",\"name\":\"Zerom\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/productivity_clockgear.png\",\"url\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/productivity_clockgear.png\",\"contentUrl\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/productivity_clockgear.png\",\"width\":512,\"height\":512,\"caption\":\"Zerom\"},\"logo\":{\"@id\":\"https:\\\/\\\/prodskill.com\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/productivity_clockgear.png\"},\"url\":\"https:\\\/\\\/prodskill.com\\\/en\\\/author\\\/proda\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Specify PtrSafe to use both 32-bit and 64-bit Excel when importing Windows API - Productivity Skill","description":"Learn about the Windows API PtrSafe declaration. When coding with Excel VBA, you can check the causes and solutions of PtrSafe-related errors that you occasionally encounter while importing and using Windows APIs.","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:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/","og_locale":"en_US","og_type":"article","og_title":"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815 - \uc0dd\uc0b0\uc131 Skill","og_description":"Windows API PtrSafe \uc120\uc5b8\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf8\ub2e4. \uc5d1\uc140 VBA\ub85c \ucf54\ub529\ud558\ub2e4 \ubcf4\uba74 Windows API\ub97c Import\ud558\uc5ec \uc0ac\uc6a9\ud558\ub2e4\uac00 \uac04\ud639 \ub9cc\ub098\ub294 PtrSafe \uad00\ub828 \uc624\ub958\uc758 \uc6d0\uc778\uacfc \ud574\uacb0\ubc29\ubc95\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4.","og_url":"https:\/\/prodskill.com\/en\/excel-vba-coding-pattern-windows-api-ptrsafe\/","og_site_name":"\uc0dd\uc0b0\uc131 Skill","article_published_time":"2022-09-27T09:19:16+00:00","article_modified_time":"2022-10-04T14:19:20+00:00","og_image":[{"width":410,"height":185,"url":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png","type":"image\/png"}],"author":"Zerom","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Zerom","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#article","isPartOf":{"@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/"},"author":{"name":"Zerom","@id":"https:\/\/prodskill.com\/ko\/#\/schema\/person\/bbad0870c78008c82edbe0960fe768bd"},"headline":"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815","datePublished":"2022-09-27T09:19:16+00:00","dateModified":"2022-10-04T14:19:20+00:00","mainEntityOfPage":{"@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/"},"wordCount":411,"commentCount":0,"publisher":{"@id":"https:\/\/prodskill.com\/ko\/#\/schema\/person\/bbad0870c78008c82edbe0960fe768bd"},"image":{"@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#primaryimage"},"thumbnailUrl":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png","keywords":["\uc5d1\uc140VBA","\ucf54\ub529\ud328\ud134","Codding Pattern","32bit","64bit","PtrSafe"],"articleSection":["\uc5d1\uc140 VBA \ucf54\ub529\ud328\ud134"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/","url":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/","name":"Specify PtrSafe to use both 32-bit and 64-bit Excel when importing Windows API - Productivity Skill","isPartOf":{"@id":"https:\/\/prodskill.com\/ko\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#primaryimage"},"image":{"@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#primaryimage"},"thumbnailUrl":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png","datePublished":"2022-09-27T09:19:16+00:00","dateModified":"2022-10-04T14:19:20+00:00","description":"Learn about the Windows API PtrSafe declaration. When coding with Excel VBA, you can check the causes and solutions of PtrSafe-related errors that you occasionally encounter while importing and using Windows APIs.","breadcrumb":{"@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#primaryimage","url":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png","contentUrl":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/image-204.png","width":410,"height":185},{"@type":"BreadcrumbList","@id":"https:\/\/prodskill.com\/excel-vba-coding-pattern-windows-api-ptrsafe\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prodskill.com\/ko\/"},{"@type":"ListItem","position":2,"name":"Windows API Import\uc2dc 32bit, 64bit Excel \ubaa8\ub450 \uc0ac\uc6a9\ud558\ub3c4\ub85d PtrSafe \uc9c0\uc815"}]},{"@type":"WebSite","@id":"https:\/\/prodskill.com\/ko\/#website","url":"https:\/\/prodskill.com\/ko\/","name":"Productivity Skills","description":"Meta Thinking, Meta Working","publisher":{"@id":"https:\/\/prodskill.com\/ko\/#\/schema\/person\/bbad0870c78008c82edbe0960fe768bd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prodskill.com\/ko\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/prodskill.com\/ko\/#\/schema\/person\/bbad0870c78008c82edbe0960fe768bd","name":"Zerom","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/productivity_clockgear.png","url":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/productivity_clockgear.png","contentUrl":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/productivity_clockgear.png","width":512,"height":512,"caption":"Zerom"},"logo":{"@id":"https:\/\/prodskill.com\/wp-content\/uploads\/2022\/09\/productivity_clockgear.png"},"url":"https:\/\/prodskill.com\/en\/author\/proda\/"}]}},"_links":{"self":[{"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/posts\/11668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/comments?post=11668"}],"version-history":[{"count":0,"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/posts\/11668\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/media\/11669"}],"wp:attachment":[{"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/media?parent=11668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/categories?post=11668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prodskill.com\/en\/wp-json\/wp\/v2\/tags?post=11668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}