2013-09-21 20 views
0

我正在做一些.vbs編碼,我想在其中打開現有模板文件,對其進行一些更改,然後用單元格B2中指定的文件名'另存爲'。如何用電子表格中指定的名稱使用.vbs文件保存文件

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 

我知道我已經可以使用宏命名一個單元格中指定值的Excel文件。不過,我打算使用這個.vbs文件來使用電子表格中指定的值命名其他文檔類型。

感謝您的幫助!


顯然,這也許是不可能的:How can I use the common Save As dialog from VBScript?

我也使用的SendKeys選擇「另存爲」,雖然它來命名新的文件時,分崩離析嘗試。

回答

0

如果您不想實際顯示「另存爲」對話框並只想將活動文件另存爲特定文件名,請查看Workbook.SaveCopyAs()函數。這可能對你有用。

0

謝謝你的迴應。我會仔細研究一下。

我已經試過的另一件事是簡單地複製和重命名文件:

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」將前面已經指定。

相關問題