热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

另存为使用变量名称给出错误-SaveAsUsingVariableNamesGivesError

Tryingtosaveanew,notyetnamed,workbook(thatIaddedusinganothersub)withvariablesforth

Trying to save a new, not yet named, workbook (that I added using another sub) with variables for the name. When I place everything within the quotation marks it works to save (not with variables). But this code constantly gives the run-time errror '1004': Method'SaveAs'of object '_workbook' failed.

尝试使用名称的变量保存一个新的,尚未命名的工作簿(我使用另一个子程序添加)。当我将所有内容放在引号内时,它可以保存(而不是使用变量)。但是这段代码不断给出运行时错误'1004':Method'SaveAs'对象'_workbook'失败了。

Sub SalvarTabela30()
Application.DisplayAlerts = False
Dim CompanyNameSave As String
Dim VencimentoSave As Date
Dim ContractNumberSave As String
Dim LastrowForSave As Long
LastrowForSave = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
CompanyNameSave = Range("A2")
VencimentoSave = Range("C" & LastrowForSave)
COntractNumberSave= Range("E2")
ActiveWorkbook.SaveAs _
   Filename:="\\dsapc429pfs.pactual.net\homefolder02$\wellsty\Desktop\" & _ 
      "Projeto - Marina\" & CompanyNameSave & " - " & ContractNumberSave & _
      " - Vencimento" & "(" & VencimentoSave & ")", FileFormat:=52

Application.DisplayAlerts = True
End Sub

1 个解决方案

#1


3  

There are a number of things that might cause an error with the Save operation but probably the most common is when we attempt to automate the filename creation, and don't account for illegal characters, which can't be part of a filename.

有许多事情可能会导致保存操作出错,但最常见的是当我们尝试自动创建文件名时,并且不考虑非法字符,这些字符不能是文件名的一部分。

In your case, since you are using date values, the presence of either a \ or / in the date format would cause an error.

在您的情况下,由于您使用的是日期值,因此日期格式中存在\或/会导致错误。

This lists the reserved characters:

这列出了保留字符:

  • < (less than)
  • <(小于)

  • > (greater than)
  • >(大于)

  • : (colon)
  • " (double quote)
  • “(双引号)

  • / (forward slash)
  • /(正斜杠)

  • \ (backslash)
  • | (vertical bar or pipe)
  • | (竖杆或管道)

  • ? (question mark)
  • ? (问号)

  • * (asterisk)

For your case, you may simply use a double replace:

对于您的情况,您可以简单地使用双重替换:

CompanyNameSave = Replace(Replace(CompanyNameSave, "\", "-"), "/", "-")

However, that doesn't account for all illegal/reserved characters. It may be helpful to write a custom function that cleans filename strings:

但是,这并不能解释所有非法/保留字符。编写清除文件名字符串的自定义函数可能会有所帮助:

Function CleanFileName(name As String, Optional replaceBy As String = "_")
Const reservedChars As String = "<>:""""/\|?*"
Dim i As Integer
Dim ch As String

For i = 1 To Len(reservedChars)
    ch = Mid(reservedChars, i, 1)

    name = Replace(name, ch, replaceBy)
Next

CleanFileName = name


End Function

You could then call this function, immediately before trying to save the file, like:

然后,您可以在尝试保存文件之前立即调用此函数,例如:

CompanyNameSave = CleanFileName(CompanyNameSave, "-")

Note: If you omit the optional replaceBy argument, it will default to using an underscore _ to replace the reserved characters.

注意:如果省略可选的replaceBy参数,则默认使用下划线_来替换保留字符。

Note also: This does not take in to account whether the file already exists.

另请注意:这不会考虑文件是否已存在。


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • moment 国际化设置中文语言 (全局) 及使用示例 ... [详细]
  • 在多线程编程环境中,线程之间共享全局变量可能导致数据竞争和不一致性。为了解决这一问题,Linux提供了线程局部存储(TLS),使每个线程可以拥有独立的变量副本,确保线程间的数据隔离与安全。 ... [详细]
  • 本文详细介绍了如何在 Windows 环境下使用 node-gyp 工具进行 Node.js 本地扩展的编译和配置,涵盖从环境搭建到代码实现的全过程。 ... [详细]
  • 在 Flutter 开发过程中,开发者经常会遇到 Widget 构造函数中的可选参数 Key。对于初学者来说,理解 Key 的作用和使用场景可能是一个挑战。本文将详细探讨 Key 的概念及其应用场景,并通过实例帮助你更好地掌握这一重要工具。 ... [详细]
author-avatar
拥有勒幷不代表幸福_563
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有