vba 以编程方式保存 Excel 加载项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4505531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:26:46  来源:igfitidea点击:

Programmatically Save Excel Add-In

excelvbaexcel-vbaadd-inexcel-addins

提问by Tom

I have a worksheet updated occasionally by users that is used to make an Add-In (.XLAM). The Add-In is located on a network share and users link to it. I am looking to enable users to easily update this add-in (ensuring it is marked as read only)

我有一个由用户偶尔更新的工作表,用于制作加载项 (.XLAM)。加载项位于网络共享上,用户链接到它。我希望让用户能够轻松更新此加载项(确保将其标记为只读)

I have seen the article by Ken Puls hereon deploying Excel Add-Ins however the .SaveCopyAsmethod he uses doesn't seem to be able to accept a file type.

在此处看到Ken Puls撰写的关于部署 Excel 加载项的文章,但是.SaveCopyAs他使用的方法似乎无法接受文件类型。

The .SaveAsmethod does, however when this was tried I gota message saying the file format or extension was invalid, I have tried both with .XLAM and .XLA as below.

.SaveAs方法确实如此,但是在尝试此方法时,我收到一条消息,说文件格式或扩展名无效,我已尝试使用 .XLAM 和 .XLA,如下所示。

DeployPath = "C:\Menu.xlam"
.SaveAs Filename:=DeployPath, ReadOnlyRecommended:=True, FileFormat:=xlOpenXMLAddIn

Any help in this regard would be greatly appreciated.

在这方面的任何帮助将不胜感激。

回答by Charles Williams

I think that you need to use (Excel 2003 and earlier)

我认为您需要使用(Excel 2003 及更早版本)

ThisWorkbook.IsAddin = True
ThisWorkbook.SaveAs "fredyy", xlAddIn

For Excel 2007+ use

对于 Excel 2007+ 使用

ThisWorkbook.SaveAs "fredyy", xlOpenXMLAddIn

回答by Yishai

This is the solution that worked for me:

这是对我有用的解决方案:

Dim strRawName As String
strRawName = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
ThisWorkbook.IsAddin = True
ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & strRawName & ".xlam", FileFormat:=xlOpenXMLAddIn