antonmihaylov / OpenXmlTemplates

Word .docx templating system that is designer (no scripting tags) and server-friendly (no word installation required)
GNU Lesser General Public License v3.0
92 stars 25 forks source link

v2.0.5 SetImage() broken with the try catch #45

Closed dgwaldo closed 6 months ago

dgwaldo commented 7 months ago

This commit introduced a bug. Getting the variable source on the same line that the cast to (int) is on only works if the variable source returns an index value. I believe it just needs null guarded to work correctly.

After debugging our project locally to see what was going on, I have this relatively simple fix.

 var index = 0;
 try {
     var vsIndex = variableSource.GetVariable("index");
     if (vsIndex != null) {
         index = (int)vsIndex;
     }
 } catch (VariableNotFoundException) {
 }

At first I was confused as to why the try catch, but then I realized that the variable source may not have ThrowIfNotFound set to True. In that case the try catch is very important. However, not sure how, but would be nice to be able to do something like this without the try catch...

In any case, it is a simple fix.

antonmihaylov commented 6 months ago

thanks for the catch and the fix!

However, not sure how, but would be nice to be able to do something like this without the try catch...

I agree, there are better ways to do this especially looking back at it, but at the moment bug fixing is the priority so i merged it and released version 2.0.6 with the fix, it should be available soon in Nuget