使用 (ActiveWorkbook.Path) 的 VBA Excel SetCurrentDirectory 不适用于 64 位 [英] VBA Excel SetCurrentDirectory using (ActiveWorkbook.Path) does not work with 64-bit

查看:27
本文介绍了使用 (ActiveWorkbook.Path) 的 VBA Excel SetCurrentDirectory 不适用于 64 位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前进行了所有必要的更改,以使我们的 VBA 模板能够与 Office 2010 32 位和 64 位一起使用.我们遇到了一个我一直在努力解决的问题.

We currently made all the changes necessary to get our VBA templates to work with Office 2010 32-bit and 64-bit. We are running into one issue that I have been trying to resolve.

这是曾经在 DynamicXLSAppHandler 中仅适用于 32 位的代码:

This is the code that used to work for just 32-bit within the DynamicXLSAppHandler:

Dim L_Return As Long

'Set Current Directory in SaveAs dialog
If ActiveWorkbook.Path <> "" Then
    ChDrive (ActiveWorkbook.Path)
    ChDir (ActiveWorkbook.Path)
    L_Return = SetCurrentDirectory(ActiveWorkbook.Path)
End If

此代码的目的是当用户单击保存"或 Ctrl-S 时,系统会提示他们在最初打开文档/模板的目录(路径)中显示另存为"对话框.如果没有此代码(因为 64 位不兼容),它现在只是默认打开文档",用户需要浏览到原始路径.

The purpose of this code is when the user clicks on Save, or Ctrl-S, they are prompted with a SaveAs dialog in the directory (path) that they originally opened the document/template from. Without this code (because of the 64-bit incompatibility) it now just opens 'Documents' as the default and the users need to browse to the original path.

我想知道是否有针对 64 位执行此操作的新方法,或者我是否必须彻底改变.

I am wondering if there is a new way of doing this for 64-bit, or if I have to change things completely.

推荐答案

要在 64 位中使用 SetCurrentDirectory API,您需要添加 PtrSafe 关键字 到函数声明:

To use the SetCurrentDirectory API in 64-bit, you need to add the PtrSafe keyword to the function declaration:

#If VBA7 Then
    Private Declare PtrSafe Function SetCurrentDirectory Lib "kernel32" _
        Alias "" SetCurrentDirectoryA(ByVal lpPathName As String) As Long 
#Else
    Private Declare Function SetCurrentDirectory Lib "kernel32" _
        Alias "" SetCurrentDirectoryA(ByVal lpPathName As String) As Long 
#End If

顺便说一句:

  • 为什么需要 ChDriveChDir 以及 SetCurrentDirectory?
  • ChDrive 应该只传递一个驱动器号,例如像这样:

  • Why do you need ChDrive and ChDir as well as SetCurrentDirectory?
  • ChDrive should only be passed a drive letter, for example like this:

ChDrive Left(ActiveWorkbook.Path, 1)

这篇关于使用 (ActiveWorkbook.Path) 的 VBA Excel SetCurrentDirectory 不适用于 64 位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆