我正在做一些.vbs编码,我想要打开一个现有的模板文件,对其进行一些更改,然后用单元格B2中指定的文件名“另存为”。
Set objExcel = CreateObject("Excel.Application") FileDirectory = "C:\Users\j128\Desktop\" FileName = "TEMPLATE.xlsx" Set objWorkbook = objExcel.Workbooks.Open(FileDirectory+FileName) objExcel.Visible = True objExcel.Cells(2,2).value = "Variable" 'Changes will occur in this section ActiveWorkbook.SaveAs Filename = cells(2,2).value 'This is where problems occur
我意识到我已经可以使用macros命名一个单元格中指定的值的Excel文件。 不过,我打算使用这个.vbs文件来使用电子表格中指定的值命名其他文档types。
谢谢你的帮助!
显然这可能是不可能的: 我怎样才能使用VBScript的另存为对话框?
我也尝试使用sendkeys来select“另存为”,虽然它分崩离析时,来命名新文件。
如果您不想实际显示“另存为”对话框并只想将活动文件另存为特定文件名,请查看Workbook.SaveCopyAs()函数。 这可能适合你。
感谢您的答复。 我会看看。
我试过的另一件事是简单地复制和重命名文件:
Set WSHShell = CreateObject("Wscript.Shell") CurrentDirectory = WSHShell.CurrentDirectory & "\" Template = "TEMPLATE.xlsx" NewFile = "New File.xlsx" Set objFSO = CreateObject("Scripting.FileSystemObject") ' First parameter: original location&file ' Second parameter: new location&file objFSO.CopyFile CurrentDirectory&Template, CurrentDirectory&NewFile
我现在需要的是能够动态地将“NewFile”设置为等于另一个电子表格中的单元格。
编辑:显然这样的事情将允许我在其他地方的单元格中指定“NewFile”输入:
value1 = xlSht.Cells(2, 1) 'Example of how to specify a value as equal to the value in cell A2
请注意,'xlSht'会在之前被指定。