Anyway to run excell commands from matlab? Like draw a boarder range?

I know how excel com commands can be called from matlab with activex com.
How could I draw a border with com?
Sub SetRangeBorder()
With Worksheets("Sheet1").Range("B2").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 3
End With
End Sub
Excel = actxserver('excel.application');
WB = Excel.Workbooks.Open(xlsfile,0,false);
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeLeft').LineStyle = 1;
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeLeft').Weight = -4138;
% Save Workbook
WB.Save();
% Close Workbook
WB.Close();
% Quit Excel
Excel.Quit();
I have tried the above code, it throws an error:
Expected one output from a curly brace or dot indexing expression, but there were 5 results.

Answers (1)

How did you create DataSheetObj and what is its size? Does it have one element or five?

5 Comments

Shouldn't you be using a comment, not an answer for this? I didn't create the object, the object is somwhere in matlabs code. It is created from the COM interface. I can't find the Datasheetobj. I know the code works from this answer, but now it is broken. https://www.mathworks.com/matlabcentral/answers/482541-anyway-to-run-excell-commands-from-matlab-like-draw-a-boarder-range
Generally I use a comment if I'm asking a question that's unlikely to lead directly to an answer. In this case, I was kind of expecting DataSheetObj to be an array that has five elements, which would directly cause this problem and lead you to investigate why.
Your link is a link directly back to this Answers post. Where specifically did you get this code from originally? Did you create it yourself? If so, it can't work as written -- DataSheetObj is undefined. Did you copy it from some documentation example or webpage? If so, where? The line of code that creates DataSheetObj may be present in the original code but been omitted accidentally when copied.
Doing a quick Google search, if the original code you were using as a model was this one, note that it does create DataSheetObj.
AppObj = actxserver('Excel.Application');
AppObj.Visible = true;
WkbkObj = AppObj.Workbooks;
DataWkbkObj = WkbkObj.Add;
DataWkbkObj.Sheets.Add().Name = 'Test';
DataSheetObj = DataWkbkObj.Sheets.Item('Test');
So please show the section of your code corresponding to the last four lines of that snippet, and show us the size of the DataSheetObj created by your code.
That was my entire code, The code was taken from another answer.
Try using the code from the answer to which I linked in the sentence starting with "Doing a quick Google search" as a starting point. You will need DataWkbkObj to be defined if you want to use it to control the borders of a region.
Indeed, this works all together
AppObj = actxserver('Excel.Application');
AppObj.Visible = true;
WkbkObj = AppObj.Workbooks;
DataWkbkObj = WkbkObj.Add;
DataWkbkObj.Sheets.Add().Name = 'Test';
DataSheetObj = DataWkbkObj.Sheets.Item('Test');
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeLeft').LineStyle = 1;
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeLeft').Weight = -4138;
% Other examples of modifying borders, I struggle to find:
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeBottom').Weight = -4138; % quite thick border weight
DataSheetObj.Range('B2:B8').Borders.Item('xlEdgeTop').Weight =2; % default border weight
% To do borders on many cells:
DataSheetObj.Range('B2:E8').Borders.Item('xlInsideVertical').LineStyle = 1; % All inside vertical borders
DataSheetObj.Range('B2:E8').Borders.Item('xlInsideHorizontal').LineStyle = 1; % All inside Horizontal borders
DataSheetObj.Range('B2:E8').Borders.Item('xlInsideVertical').Color =hex2dec('00FF00'); % change color using hexa coded RGB color
DataSheetObj.Range('B2:E8').Borders.Item('xlInsideVertical').ColorIndex =15; % change color using Excel Color Index (here grey) (google to find more colors)

Sign in to comment.

Products

Release

R2019a

Asked:

on 27 Sep 2019

Edited:

on 23 Apr 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!