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

时间:2019-04-10 06:40:30

标签: excel vba network-programming

我以前在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

2 个答案:

答案 0 :(得分:0)

类似的东西对我有用:

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

根据我的观察,问题在于: -计算机无法访问网络共享, -建议的工作簿名称与保存的“规则”不符,例如G。太长或具有相同的奇怪字符。

您应该尝试:

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

2)检查IP地址是否正确

3)检查文件名是否正确。

答案 1 :(得分:0)

感谢米奇斯(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