使用 IP 地址保存在网络中时,宏另存为文件名不起作用

Macro Save As filename not working when saving in the network using IP address

我之前在 excel 中为我的 VBA 脚本设置了另存为行,文件正在保存到网络文件夹中。之前,服务器有名称,现在我们使用IP(192.168.20.212)访问服务器文件夹,因此我使用IP更改了代码中的地址。

现在,问题是我设置的文件命名不起作用。当对话框出现时,文件名是空的,需要用户手动输入文件名。但是,如果我输入服务器名称或使用本地地址,则文件命名有效。没办法只能用IP存文件了

以下为文件命名行;

    filenme = "PENDING CLAIMS_" + szNextDatereformat

下面是之前保存文件的行;

Dim sFileSaveName As String
        sFileSaveName = Application.GetSaveAsFilename _
                                     (InitialFileName:="\SERVERNAME\excel_files\" & filenme & sTargetFile, _
                                      FileFilter:="Excel Files (*.xlsx), *.xlsx")

        If sFileSaveName <> "False" Then
             '-- Savethe file --
             Application.DisplayAlerts = False
             ActiveWorkbook.SaveAs FileName:=sFileSaveName, _
                                   FileFormat:=51
            Application.DisplayAlerts = True
        Else
             '-- Popup message --
             MsgBox "Template not saved!", vbExclamation, "Warning"
        End If

新的应该是;

Dim sFileSaveName As String
        sFileSaveName = Application.GetSaveAsFilename _
                                     (InitialFileName:="\192.168.20.212\excel_files\" & filenme & sTargetFile, _
                                      FileFilter:="Excel Files (*.xlsx), *.xlsx")

        If sFileSaveName <> "False" Then
             '-- Savethe file --
             Application.DisplayAlerts = False
             ActiveWorkbook.SaveAs FileName:=sFileSaveName, _
                                   FileFormat:=51
            Application.DisplayAlerts = True
        Else
             '-- Popup message --
             MsgBox "Template not saved!", vbExclamation, "Warning"
        End If

像这样的东西对我有用:

Dim txtFileName As String
Dim finalPath As String

finalPath = "\10.10.10.11\PUBLIC\SOMETHING\"
finalPath = finalPath & "myWorkbookName.xlsx"

txtFileName = Application.GetSaveAsFilename(finalPath, "Excel (*.xlsx), *.xlsx", , "Excel network save")
        If txtFileName = "False" Then
            MsgBox ("We could not save Excel.")
            Exit Sub
        End If

根据我的观察,问题出现在: - 计算机无法访问网络共享, - 建议的工作簿名称与保存的 "rules" 不匹配,例如。 G。太长或有相同的奇怪字符。

您应该尝试:

1) 检查计算机是否可以访问此网络驱动器

2) 检查IP地址是否正确

3) 检查文件名是否正确

感谢 Mikisz 对他的代码做了一些修改,下面的代码对我有用;

Dim txtFileName As String
Dim finalPath As String

finalPath = "\192.168.20.212\networkfolder\"
finalPath = finalPath & filenme & ".xlsx"

txtFileName = Application.GetSaveAsFilename(finalPath, "Excel (*.xlsx), *.xlsx", , "Template saved on the Network")

        If txtFileName <> "False" Then
         '-- Savethe file --
         Application.DisplayAlerts = False
         ActiveWorkbook.SaveAs FileName:=txtFileName, _
                               FileFormat:=51
        Application.DisplayAlerts = True
    Else
         '-- Popup message --
         MsgBox "Canceled saving the template!", vbExclamation, "Warning"
         'Exit Sub
    End If