I wrote a C# Add-in for the VBA-Editor of ms-access. See: How to write a Add-in for the development enviroment: "Microsoft Visual Basic for Applications"
My add-in searches for code, marks it and - after a second click - deletes it.
The active module has three lines of code in it:
Option Compare Database
'Delete This
Option Explicit
This is the code I use (of course reduced to the problem):
int m_called = 0;
protected override void button_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
if (m_called == 0)
{
m_VBE.ActiveCodePane.SetSelection(2, 1, 2, 13);
}
else
{
m_VBE.ActiveCodePane.CodeModule.DeleteLines(2, 1);
m_VBE.ActiveCodePane.SetSelection(2, 2, 2, 2);
}
m_called++;
}
After running this code the Letters: "Option Expli" are selected.
This happens only when setting the first selection via "SetSelection" (The first line). Instead selecting exactly the same part manually results in the correct position of the selection (= as a line-cursor at the start of the second line before "Option Explicit").
Setting the cursor to line 1 will work fine. Setting it to the second character of line 2 (2, 2, 2, 2; Between the "O" and the "p") also works. Literally every other position other than the start of the second line works just fine!
So is there a workaround for this weird bug?
This is what I tried:
ok...you have to set the Selection to another place before setting it to the beginnig of the second line.
So this code works for me:
int m_called = 0;
protected override void button_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
if (m_called == 0)
{
m_VBE.ActiveCodePane.SetSelection(2, 1, 2, 13);
}
else
{
m_VBE.ActiveCodePane.CodeModule.DeleteLines(2, 1);
m_VBE.ActiveCodePane.SetSelection(1, 1, 1, 1);
m_VBE.ActiveCodePane.SetSelection(2, 1, 2, 1);
}
m_called++;
}