The Collings Zone

The home of Adam & Linda Collings

Welcome to Adam and Linda's place. We are a Christian couple from Tasmania. We have two adorable children. Here you will find some of our writings, and a little other material. Make yourself at home.

Home   |   About Us   |   Adam's Writing   |   Software Projects   |   The Blog

Search the Bible with BibleGateway.com
  
  
  
BibleGateway.com is a service of Gospelcom.net

Monday, August 31, 2009

Stack Overflow with Word Automation Replace - SOLVED

When performing Word automation, Word will sometimes throw a System.StackOverflowException, which you cannot catch, while performing a find/replace operation. It is usually the Find.Replacement.ClearFormatting(); call that leads to this exception.

The solution to this is simple, use late binding. In fact, it is recommended that you use late binding whenever performing office automation.



The resulting code would be something like

object Selection = oWord.Selection;
object myFind = oWord.Selection.Find;
object Optional = Type.Missing;
object wd = Word.WdReplace.wdReplaceAll;
object[] Parameters;
object findText = "Findtext";
object replaceText = "Replacetext";

Parameters = new object[15];
Parameters[0] = findText;
Parameters[1] = Optional;
Parameters[2] = Optional;
Parameters[3] = Optional;
Parameters[4] = Optional;
Parameters[5] = Optional;
Parameters[6] = Optional;
Parameters[7] = Optional;
Parameters[8] = Optional;
Parameters[9] = replaceText;
Parameters[10] = wd;
Parameters[11] = Optional;
Parameters[12] = Optional;
Parameters[13] = Optional;
Parameters[14] = Optional;

myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters);

Don't forget to free the objects you have used

System.Runtime.InteropServices.Marshal.ReleaseComObject(oFind);
oFind = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSelection);
oSelection = null;

This succesfully resolved the problem for me.

Sources:

Labels:

Thursday, January 22, 2009

Could not find adofltr.dll - Solved!

Excuse he technical nature of this article, but today I solved an annoying programming problem and in the process was reminded of an important spiritual lesson.

I was using Microsoft ActiveSync to copy data from an access application to a PocketPC CE device. This is done using the external functions DESKTOPTODEVICE and DEVICETODESKTOP. On all computers around my workplace this works with no worries. However, on a client machine I was receiving the error "File not found c:\program files\microsoft activesync\adofltr.dll"

This seemed odd as the file did indeed exist. A search of the web revealed many people with the problem but few solutions. I tried some of the suggestions, most of which involved copying various DLLs from the activsync directory into the application directory of my program and re-registering them. This did not work. One suggestion did work however.

As it turns out, there are two solutions; one of which I have succesfully implemented, and another that I plan to investigate.

1) Add the ActiveSync install directory to your path.
I added c:\program files\microsoft activesync to my path environment variable.
Note you have to do this through the properties of My Computer. If you do it in a command prompt window, it will only apply during that command session.

2) The second solution is to use an API call to determine programatically, where the adofltr.dll file is located. I have not yet tried this but logic suggests it would work - without requiring clients to change their path.
(as I learn more ab out this I will post my findings with a link from this article)


I was reminded through this experience of the value of praying over our work as opposed to trying to solve difficult problems in our own strength.

Labels: , , ,

Wednesday, October 01, 2008

Remove Selection from a ListView Control in eMbedded VB

With The compact .NET framework well established there are probably not many people who are still using eMbedded Visual Basic, but if there are any still working on legacy systems, I have a small tip that helped me today.

I wanted to completely clear the selection on a ListViewCtrl control but there seemed no simple way of doing it. I found that you need to loop through the items and unselect them. Interestingly, it seems that you can't iterate through the ListItems collection using a For Each loop, but a traditional For loop works fine.


Dim lvitem As ListItem
Dim i As Integer

For i = 1 To lstList.ListItems.Count
      Set lvitem = lstList.ListItems(i)
      lvitem.Selected = False
Next i


Theoretically, if you're not using multi-select you should just be able to go

lstList.SelectedItem.Selected = False

but I haven't tested it.

Labels: , ,