0

I'm doing some .vbs coding in which I want to open an existing template file, make some changes to it, and then 'save as' with the filename specified in cell 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

I realise I can already use Macros to name an Excel file with the value specified in a cell. However, I'm planning on using this .vbs file to name other document types using values specified in a spreadsheet.

Thanks for any help!


Apparently it might not be possible: How can I use the common Save As dialog from VBScript?

I've also tried using sendkeys to select 'Save as', though it falls apart when coming to name the new file.

4

2 に答える 2

0

返信ありがとうございます。私はそれを見ていきます。

私が試したもう1つのことは、ファイルをコピーして名前を変更することです。

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」は以前に指定されていることに注意してください。

于 2013-09-22T11:43:44.070 に答える