This.. turned out to be quite complicated.. but a nice challenge :)
It is possible but hella ugly. First: Credits. Those two questions here and here got me started, the rest is through trial and error. (Note: All code samples follow in succession, a complete sample is at the end, as it is quite long)
First step: Finding the CheckBox
All ActiveX controls are stored as a Control, we need to find the control in question.
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fileName, true))
{
foreach (var control in doc.WorkbookPart.WorksheetParts.First().Worksheet.Descendants<Control>())
{
Console.WriteLine();
Console.WriteLine("Control {0}:", control.Name);
Console.WriteLine("Id: {0}", control.Id);
You can then find the specific control you look for by filtering the name.
Bonus: Identifying the type
Based on one of the linked questions, we can identify the type of control as follows:
var part = doc.WorkbookPart.WorksheetParts.First().GetPartById(control.Id) as EmbeddedControlPersistencePart;
var xreader = OpenXmlReader.Create(part.GetStream());
xreader.Read();
var xml = xreader.LoadCurrentElement();
var classID = xml.GetAttribute("classid", xml.NamespaceUri).Value;
if (classID == "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}") // it is a checkbox
Second Step: Getting those Attributes
All ActiveX controls store their attributes as a binary file. So you'll need to parse and edit this binary file to change the tick mark. So lets first find the binary data
var binary = part.EmbeddedControlPersistenceBinaryDataParts.First();
Easy. Now we need to get the binary stream and make it editable by pulling it into a memory stream
using (var source = binary.GetStream())
{
using (var reader = new BinaryReader(source))
{
using (var stream = new MemoryStream(reader.ReadBytes((int)source.Length)))
{
Neat. Now some stupid binary parsing. I found the layout of the binary data by trial and error using HxD and manipulating attributes one by one. I've added my analysis at the end of this question. As xlsx-Files are just zip-files, you can find the binary relatively easily yourself in the path "xl\activeX"
First, lets skip to the desired position where the checkmark is saved
// Skip random junk
stream.Seek(20, SeekOrigin.Begin);
// read additional data flags & how many additional bytes are added
var flags = (byte) stream.ReadByte();
var skip = 0;
if ((flags & 0x01) != 0) // Attribute Block
skip++;
if ((flags & 0x02) != 0) // BackColor Block
skip++;
if ((flags & 0x04) != 0) // ForeColor Block
skip++;
// skip some bytes
stream.Seek(2, SeekOrigin.Current);
flags = (byte) stream.ReadByte();
if ((flags & 0x01) != 0) // Special Effect Block
skip++;
if ((flags & 0x04) != 0) // PicturePosition Block
skip++;
// skip some flags
stream.Seek(4, SeekOrigin.Current);
// skip attribute blocks & some data blocks
stream.Seek(skip * 4 + 6 * 4, SeekOrigin.Current);
Then parse the active checkmark, invert it and save it back into the stream
var check = (byte) stream.ReadByte();
// check = 0x30: False
// check = 0x31: True
// check = 0x32: Undefined
bool? boolCheck = (check == 0x31);
if (check == 0x32) boolCheck = null;
// invert checkmark
boolCheck = !boolCheck;
if (boolCheck == null)
check = 0x32;
if (boolCheck == true)
check = 0x31;
if (boolCheck == false)
check = 0x30;
stream.Seek(-1, SeekOrigin.Current);
stream.WriteByte(check);
Now write the new stream back and save everything. Done.
// ignore all the other stuff
stream.Seek(0, SeekOrigin.Begin);
binary.FeedData(stream);
doc.Save();
}
}
}
break;
}
}
}
Console.Read();
Note: the break is in there because the checkbox was found twice and I was too lazy to figure out why.
Conclusio
And this is how you invert the checkmark of an activeX CheckBox in a spreadsheet using OpenXML.
Addendum 1
Here is the full code I came up with. The sample xlsm has just one activeX checkbox placed in it:
static void Main(string[] args)
{
string fileName = @"C:\Users\***\Desktop\Test.xlsm";
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fileName, true))
{
foreach (var control in doc.WorkbookPart.WorksheetParts.First().Worksheet.Descendants<Control>())
{
Console.WriteLine();
Console.WriteLine("Control {0}:", control.Name);
Console.WriteLine("Id: {0}", control.Id);
var part = doc.WorkbookPart.WorksheetParts.First().GetPartById(control.Id) as EmbeddedControlPersistencePart;
var xreader = OpenXmlReader.Create(part.GetStream());
xreader.Read();
var xml = xreader.LoadCurrentElement();
var classID = xml.GetAttribute("classid", xml.NamespaceUri).Value;
if (classID == "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}") // it is a checkbox
{
var binary = part.EmbeddedControlPersistenceBinaryDataParts.First();
using (var source = binary.GetStream())
{
using (var reader = new BinaryReader(source))
{
using (var stream = new MemoryStream(reader.ReadBytes((int)source.Length)))
{
// Skip random junk
stream.Seek(20, SeekOrigin.Begin);
// read additional data flags & how many additional bytes are added
var flags = (byte) stream.ReadByte();
var skip = 0;
if ((flags & 0x01) != 0) // Attribute Block
skip++;
if ((flags & 0x02) != 0) // BackColor Block
skip++;
if ((flags & 0x04) != 0) // ForeColor Block
skip++;
// skip some bytes
stream.Seek(2, SeekOrigin.Current);
flags = (byte) stream.ReadByte();
if ((flags & 0x01) != 0) // Special Effect Block
skip++;
if ((flags & 0x04) != 0) // PicturePosition Block
skip++;
// skip some flags
stream.Seek(4, SeekOrigin.Current);
// skip attribute blocks & some data blocks
stream.Seek(skip * 4 + 6 * 4, SeekOrigin.Current);
var check = (byte) stream.ReadByte();
// check = 0x30: False
// check = 0x31: True
// check = 0x32: Undefined
bool? boolCheck = (check == 0x31);
if (check == 0x32) boolCheck = null;
// invert checkmark
boolCheck = !boolCheck;
if (boolCheck == null)
check = 0x32;
if (boolCheck == true)
check = 0x31;
if (boolCheck == false)
check = 0x30;
stream.Seek(-1, SeekOrigin.Current);
stream.WriteByte(check);
// ignore all the other stuff
stream.Seek(0, SeekOrigin.Begin);
binary.FeedData(stream);
doc.Save();
}
}
}
break;
}
}
}
Console.Read();
}
Addendum 2
For anyone interested, here is the analysis of the binary format so far. Some Blocks will only be written, if some Flag is set (IIF), and all strings are padded to a multiple of 4 Bytes:
ClassID (first half is reverse byte order in a block)
40 1D D2 8B-42 EC-CE 11-9E 0D-00 AA 00 60 02 F3
Random Junk
00 02 38 00
Attribute Flags
TrippleState: C0=F, E0=T
40 01 C0 80 01 00 00 00
Cursor Flag
| ForeColor Flag
| |BackColor Flag
| ||Attribute Block
|??? ?|||
[0100 0000] 01 ...
Accelerator Flag
| SpecialEffect Flag
| | Picture Position Flag
??|? ?|?|
... C0 [1000 0000] ...
Attributes (IIF Attribute Flag)
1B 08 80 2C
BackStyle
| Enable
| | Alignment
| | | WordWrap
| | | | AutoSize
???? |?|? ??|? ???? |??? ???? ???| ????
[0001 1011 0000 1000 1000 0000 0010 1100]
Background Color (IIF BackColor Flag)
04 00 00 80 (Reverse order)
Foreground Color (IIF ForeColor Flag)
04 00 00 80 (Reverse order)
Random Junk
Mouse Cursor
40 00 00 00
Length: Value
01 00 00 80
Length: Caption
09 00 00 80
Picture Position (IIF Picture Position Flag)
00 00 02 00 = 0, Left Top
03 00 05 00 = 1, Left Center
06 00 08 00 = 2, Left Bot
02 00 00 00 = 3, Right Top
....
Special Effect (IIF Special Effect Flag)
00 00 00 00
Accelerator Key (IIF Accelerator Flag)
61 00 00 00
Length: Group
06 00 00 80
Width (unknown Units)
B7 09 00 00
Height (unknown Units)
E5 02 00 00
Value (Padded to multiple of 4 Bytes)
30 = False
31 = True
32 = Undefined
30 xx xx xx
Caption (Padded to multiple of 4 Bytes)
Group Name (Padded to multiple of 4 Bytes)
Random Junk
00 02 18 00 35 00 00 00
Length: Font
07 00 00 80
Random Junk (Variable length? not deciphered)
E1 00 00 00 00 02 00 00
Font Name (Padded to multiple of 4 Bytes)
I think it is fairly complete, I covered most of the Properties exposed by the ActiveX, skipping the Bitmaps & Icons. But maybe there is some more; I wasn't able to decipher the last piece of junk at the end; it has variable length but I didn't really bother because it was after the checkmark.