Tuesday, February 22, 2011

Error executing code: The method has been called with an invalid number of parameters. - Microsoft Dynamics AX 2009

When trying to use the COM object to attach a file into Outlook I got the following error message:

Error executing code: The method has been called with an invalid number of parameters.

After setting a breakpoint into the SysInetOutlookMail class AddAttachment method, the code broke in the following line:

_outlookMailAttachments = _outlookMail.Attachments();

One of the reasons for getting error is when the Windows Operating System switches the Dynamics AX window by a windows ghost window.

Actually, when Dynamics AX goes into a complex COM operation, windows thinks that Dynamics AX has stopped running. Therefore, the Dynamics AX application window is replaced by this ghost window until Dynamics AX resumes.

At least in my case, this is happening only in my windows server 2008, as in a windows 2003 server this code runs well. I guess it would be nice to know how to deactivate this ghost window functionality, but so far I haven't found that answer yet.

A work around to this problem is to add a Try and Catch statement with a Retry within the Catch as shown below:

void AddAttachment(str fullFilePath,str displayName="")
{
    str newFilepath;
    container tempCont;
    int retryCount = 3;
    ;

    if(_outlookMailAttachments==null)
    {
        //_outlookMailAttachments = new com();
        //_outlookMail = new com();
        //_outlookMailAttachments=_outlookMail.Attachments();

        try
        {
            _outlookMailAttachments = _outlookMail.Attachments();
        }
        catch(Exception::Error)
        {
            retryCount--;
            if(retryCount > 0)
                retry;
            else
                throw(Exception::Error);
        }
    }

    if(fullFilePath!="" && displayName!="")
    {

        tempCont=global::fileNameSplit(fullFilePath);
        newFilepath=any2str(conpeek(tempCont,1))+displayName;

            _outlookMailAttachments.Add(fullFilePath);
            WinApi::deleteFile(newFilePath);
    }
    else
        _outlookMailAttachments.Add(fullFilePath);
}

In my case, the retry runs only once and then it goes through with no problems.

No comments:

Post a Comment

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!