Office - Infopath and Excel formulas
Asked By Ala on 07-Jan-08 09:00 PM
I have an excel formula that calulates the second tuesday of the month based
on the user selecting the month (numericly) and the year from a drop down
Use the WEEKDAY and DATE functions as shown in the following formula:
I have tried to get the formula to work without success. Does anyone know
of a way to do this.
SYMWongATo replied on 07-Jan-08 11:42 PM
Excel is "smart"; InfoPath is not that far yet. It is best to re-create this
formula by writing code, because there is no weekday function in InfoPath and
trying to recreate this formula with what's currently available in InfoPath
would become too complex. Alternatively, if you do not want to write code,
and you have MOSS with Excel Services, and want to re-use formulas in Excel
workbooks, you can do something like I did in this article:
Ala replied on 08-Jan-08 07:04 AM
I already have the calulation working in a SP list. The dropdown list is
pulling it's values from a SP list. In the SP list there are month, year and
a calulated date columns. The calulated date column contains the formula. So
the SP list contains two years worth of calulated dates.
So what I need is to populate a field on the infopath form (or a column in
the form library), with the calulated date, associated with the row
month-year row that the user selected. Any suggestions?
SYMWongATo replied on 08-Jan-08 10:21 PM
Someone in the group once proposed a trick to get calculated fields appear in
InfoPath. I myself have never tried it out, but you can give it a go. Read
Ala replied on 10-Jan-08 10:21 AM
I've decided the best route is to use event handler to perform the
calculation that the SP list is being used for. I have a excel formula that
calculates the second tuesday of the month when the user enters the month
(numeric) and the year.
Do you know the best way to convert this formula to c# with the "Year" and
SYMWongATo replied on 11-Jan-08 01:49 AM
First you need to retrieve the values from the month and year fields. Check
out this article
if you need code to do it. Here field1 would be either the month or year
field. You can quickly find out what the XPath expression for the month or
year is by going to the "Data Source" pane, right-clicking on the month or
year node, and selecting "Copy XPath".
I don't know if this is the best way, but what I would do is use the values
from the month and year fields to determine how many days are within the
month. You can use DateTime.DaysInMonth (see
for this. Once you know how many days are within the month, you can create a
for-loop that loops over the amount of days in the month. For each day in the
loop you can then construct a DateTime object and use its DayOfWeek property
to check whether the day is a Tuesday. If it is, you can increase a counter
(declare and initialize the counter outside the loop). Once the counter has
been increased twice, you must save the last date you checked, break the
loop, and then you have the date for the second Tuesday of the month.
Again, there might be a better/quicker way of calculating it. It is best to
post your question in one of the .NET development newsgroups if you want to
know what the best way is.
Ala replied on 11-Jan-08 07:14 AM
Thanks. You are a great help. I will digest this and let you know how it
SYMWongATo replied on 12-Jan-08 06:58 PM
No worries. I will monitor this thread until you solve your issue.
Ala replied on 17-Jan-08 06:33 AM
Here is the code that i used:
EventManager.XmlEvents["/my:ExpRFR/my:Contract/my:BrdMtgDate"].Changed += new
public void BrdMtgYear_Changed(object sender, XmlEventArgs e)
//// calculate the 2nd Tuesday of the month
// based on the user entering the month and year numerically
//These variables will be input from the form
XPathNavigator BrdMtgMonth =
XPathNavigator BrdMtgYear =
XPathNavigator BrdMtgDate =
//int intMonth = 6;
//int intYear = 2008;
//These variables are used in the code
int intDay = 1;
int tuesdayCount = 0;
DateTime scndTuesdayDate = DateTime.MinValue;
//Loop until we have found the second Tuesday
while (tuesdayCount < 2)
//Create our test date -- this starts at the beginning of
Convert.ToDateTime(Convert.ToString(BrdMtgMonth) + "/" +
Convert.ToString(intDay) + "/" + Convert.ToString(BrdMtgYear));
//See if our test date is a Tuesday
if (scndTuesdayDate.DayOfWeek == DayOfWeek.Tuesday)
//If our test date is a Tuesday,
//then up the count of the number of Tuesdays we have
tuesdayCount = tuesdayCount + 1;
// and increase the date by a week to find the next
intDay = intDay + 7;
//If out test date is not a Tuesday,
//then increase the date by a day and check it again
//in order to find the first tuesday
intDay = intDay + 1;
//scndTuesdayDate is your return value for the second tuesday of
public void BrdMtgDate_Changed(object sender, XmlEventArgs e)
// Write your code here to change the main data source.
Ala replied on 17-Jan-08 12:29 PM
I have a new issue. As you can see I set value of he field BrdMtgDate to the
scndTuesdayDate which is a string. I want the BrdMtgDate to be formated as a
date. Any suggestions?
SYMWongATo replied on 17-Jan-08 09:01 PM
If the data type of BrdMtgDate is date, use
If the data type of BrdMtgDate is dateTime, use
For an explanation, see
Ala replied on 18-Jan-08 07:09 AM
My next challange is to populate a new instance of the from with data from
the SP list. Then allow the user to update the data and save the updates as
a new record.
SYMWongATo replied on 19-Jan-08 12:03 AM
Great! Glad it worked.
If you have issues with your new challenge, it's best to create a new
thread, since it's unrelated to your Excel formula issue, which I assume has
now been resolved. Good luck!
Ala replied on 19-Jan-08 07:37 AM
Ala replied on 20-Jan-08 08:37 AM
I thought this form was finished. However I had not tested the form in MOSS.
When I did the I noticed that the event changed code does not run the same.
I the browser when the user selects the year from the dropdown the event
changed code does not run. But if you select the submit button a dialob box
displaying the message "You have made changes to the form that have not been
processed. We will process the changes and show the form again. After that
you have to click the "Submit" again." The event changed then code runs.
When the "submit" button is selected again an error message displays "An
error occurred while the from was being submitted"
SYMWongATo replied on 20-Jan-08 05:38 PM
To make the changed event always run: On the Properties dialog box for the
year dropdown, go to the Browser Forms tab and select "Always" under the
postback options. To get a little bit wiser about what kind of error is
taking place, go to your SharePoint server and look into the log files (under
the 12 hive) for any errors that may have occurred after you tried submitting
the form; the error might not be related to the date calculation, but
Ala replied on 20-Jan-08 07:04 PM
I found the problem. The form was created by someone else and there was a
rule running under the Submit button. The rule submitted to a different
Ala replied on 04-Feb-08 08:22 PM
Can you look at my post titled Sharepoint list data source?