VSTO get left and top coordinates of pointer postion - vsto

i want to get the left and top coordinates of the Active Cell in Excel and the same for the pointer position in Word so that i can insert image in that position specified by the user.

ActiveSheet.Pictures.Insert() in excel and Application.Selection.InlineShapes.AddPicture in word will insert picture at selection and active cell.
However to get x,y information you can use following api
word
wordAppliaction.Selection.get_Information(WdInformation.wdVerticalPositionRelativeToPage)
wordAppliaction.Selection.get_Information(WdInformation.wdHorizontalPositionRelativeToPage)
In excel
it can be tricky to get coordinates precisely you can try with
Range.Top and Range.Left

Related

Excel Formula Cell Based on Background color

I need a formula in EXCEL that place a number 1 in the cell next to the cell where the cell background is RED. See example below.
Is this possible at all without VBA?
This can be done from Name Manager this can be accessed by pressing Ctrl+F3.
You will want to create a named reference (i called this "color") and have it refer to =GET.CELL(63,OFFSET(INDIRECT("RC",FALSE),0,-1)) in the formula bar.
Now you can use this 1 cell to the right to determine the color index number of a cell:
So as red is color index 3 in the cell next to it you can apply the formula:
=IF(color=3,1,0)
Open the VBA editor and add a new module. Do this by going to the Developer tab and clicking Visual Basic. If you don't have the developer tab on the ribbon you will need to add it (do a quick Google search). Once the VBA editor is open, right click on the VBA project which has your workbook name on the left and insert a module.
Place the following code into the new module:
Function IsRed(rng As Range) As Integer
IsRed = (rng.Interior.Color = vbRed) * -1
End Function
then you can use the formula =IsRed(A1) to determine if A1 has a red background
note: this uses the default red in the standard colours
You can achieve it manually without VBA using an autofilter:
Make sure you have a title above the column with colours and above the column where you want the value 1 placed
Add an Autofilter (Select both columns, click the Filter button on the Data tab of the ribbon)
Click the drop down filter on the column with colours, then click on Filter by Colour, the choose the Red colour
In your second column, enter a 1 in every visible cell. (Enter 1 in the first cell, then fill down. Or, select all cells, type 1 then press ctrl-enter)

Insert a text value from Excel cell at a specific location in the Word document in VBA

I currently have a variable say x which stores a cell value from Excel and I am trying to place x at (a,b) in the Word document.
I have used:
With objword.Selection
.Rows.HorizontalPosition=a
.Rows.VerticalPosition=b
.TypeText(x)
End With
But this doesn't work - I get this error:
Application defined or object defined error
I have a table that is at a different position on the table and they shouldn't be overlapping, but could this be the issue? I tried using .ParagraphFormat instead but that doesn't really have a method for the coordinates

Is it posible to get the position of point on an excel graph axis

I'm not sure this is possible but thought this was the best place to ask.
Is it posible to get the position of a series value on a graph in excel?
For example, if I have a line graph in excel that has time along the x axis, is it possible to (using VBA) get the position of a specific point on that axis.
What I am trying to do is have a vertical line that is can be positioned based on a date entered by the user.
like this
Where the green line could be positioned by entering in a date (rather than just being manually moved) (or also it could be set to automatically move to the current date etc).
I was then thinking that if the position is on the graph is queryable, then I can just access the line object and move it to any position I wanted through VBA.
Any Ideas? or is this just not possible?
The "cleanest" way to do this is to add the line to the chart as a new series. In that way, Excel handles all of the positioning and your work is simplified. To get a vertical line on a chart, there are a number of options. I prefer this route:
Create a small 2x2 area with two dates and two values
Add in the date or x-axis value you want the line at (E3 in image). You can use =TODAY() here or some manually entered value.
Set the second x-axis value equal to the first
Use MAX and MIN on the data to get the values for each date. You can also use 0 and 1 and a secondary axis, but I think MAX/MIN is easier.
Add the data to the chart and format as a marker with straight line.
Formulas
E3: =TODAY()
E4: =E3
F3: =MIN(C3:C27)
F4: =MAX(C3:C27)
Result and chart data series for vertical line

Forming a specific colour filled shape in powerpoint when importing a data from excel cell

I am a beginner in programming. I know how to import a data from excel to powerpoint in such a way that when I change the data in the cells in excel, the corresponding datas in the powerpoint changes as well.
But is there a way to create a specific shape (with specific colours) in powerpoint automatically for a range of data imported from excel.
For example, if I type the number 1 in excel, it should go to powerpoint as well and a square box with the colour green should be formed in power point slide with the number 1 in it.
If number 2, then yellow
and number 3 red
Any help would be deeply appreciated! Thanks in advance!
Without your code it is not possible to give you an example. I would look at
Add shape:
http://msdn.microsoft.com/en-us/library/office/ff744336%28v=office.15%29.aspx
Once you have the shape, you can color it, add text etc, see:
http://msdn.microsoft.com/en-us/library/office/ff744177%28v=office.15%29.aspx
Regards,
HP

ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor returns -1

I have a Word 2007 doc with a lot of tables. Each table has certain cells filled with 2 custom colors. I created a macro which takes for input 3 values to feed the RGB function, creating one of the colors, matches it against each of the colors, and changes the match with a new color.
I used an If statement that compares the color of the filled cell with the color returned by the RGB function, feeding the function with the input given by the user.
If ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor = RGB(inputRed, inputGreen, inputBlue) Then
'code
end if
As I was looking through the document to see the results, I noticed that a few cells from 3 tables were left with the old color, so apparently the macro could not recognize it.
In Word 2007 I selected the cell that was supposed to have its color changed. I went to Home -> Shading -> More colors -> Custom and saw the 3 values that matched perfectly with the 3 values given as input by the user.
After 30 minutes of staring at the monitor, I thought about going to the doctor to have my eyes examined. :)
Before doing that though, I started to debug the app. on each of the cells that were not changed. Upon examining this line:
ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor
I saw that it returned -1.
I am thinking -1 is the equivalent of null or nil, meaning that either the cell is not filled, but if so, why can I see the RGB values? Or perhaps the system can't read the RGB values, but who is this system exactly?
Do you have a link where the Shading.BackgroundPatternColor method is well explained?
I don't know about a value of -1, but colours in Word 2007 changed slightly so that the values were no longer necessarily RGB values. I have written extensively on this - please see http://www.wordarticles.com/Articles/Colours/2007.php