Hi Paul,
Thanks for the code. It really made my work alot better.
May i ask, is it a MUST to have the (.xls) on the desktop?
i changed the code:
var Excel = File("/c/temp/pmr.xls");
to :
var Excel = File("~/desktop/myfolder/records/"+Number(paper_no)+".xls");
and i got the following error code:
Script: C:\Users\Ando\AppData\Local\Temp\VBS.vbs
Line: 3
Char: 1
Error: Object required: 'objWorkbook'
Code: 800A01A8
Source: Microsoft VBScript runtime error
Would you know what caused this?
Very strange, I created the relevant folders and it all seems to work ok here, so don't know what's gone wrong?
#target photoshop
function main(){
if(!documents.length) return;
var paper_no=900;
var Excel = File("~/desktop/myfolder/records/"+Number(paper_no)+".xls");
var Cell ="C22";
if(!Excel.exists){
alert("SpreadSheet does not exist!");
return;
}
try{
var SB = app.activeDocument.selection.bounds;
}catch(e){return;}
app.activeDocument.selection.copy();
var Width = SB[2].as('px') - SB[0].as('px');
var Height = SB[3].as('px') - SB[1].as('px');
var doc = app.documents.add(UnitValue(Width,'px'),UnitValue(Height,'px'),72);
app.activeDocument.paste();
doc.flatten();
var tmpFile = File(Folder.temp +"/deleteMe.jpg");
if(tmpFile.exists) tmpFile.remove();
SaveForWeb(tmpFile,60);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
var VBS = File(Folder.temp +"/VBS.vbs");
VBS.open('w');
VBS.writeln('Set objExcel = CreateObject("Excel.Application")');
VBS.writeln('Set objWorkbook = objExcel.Workbooks.Open("'+decodeURI(Excel.fsName)+'")');
VBS.writeln('objExcel.Visible = false');
VBS.writeln('Set mypic = objWorkbook.ActiveSheet.Pictures.Insert("'+decodeURI(tmpFile.fsName)+'")');
VBS.writeln('mypic.Top = objWorkbook.ActiveSheet.Range("'+Cell+'").Top');
VBS.writeln('mypic.Left = objWorkbook.ActiveSheet.Range("'+Cell+'").Left');
VBS.writeln('mypic.Width = objWorkbook.ActiveSheet.Range("'+Cell+'").Width');
VBS.writeln('mypic.Height = objWorkbook.ActiveSheet.Range("'+Cell+'").Height');
VBS.writeln('objExcel.ActiveWorkBook.Close True');
VBS.close();
VBS.execute();
}
main();
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}