how to edit TimescaleStart of MS Project Using C#.net - c#

I have a requirement to modify the ms project file (.mpp) using C#.net.
I have done with all the things, the only thing remaining is to modify the TimescaleStart date of MPP file using C#.net. I need to set the user defined date.
How can i do that?
Following is my code:
Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application();
app.DisplayAlerts = false;
app.AskToUpdateLinks = false;
app.FileOpenEx(
strFilePath + "test.mpp",
false,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
PjPoolOpen.pjPoolReadWrite, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.MSProject.Project pj = app.ActiveProject;
object objDate = dt.Date;
app.TimescaleStart = objDate;
Got Error as
Type mismatch. (Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))
on Following line:
app.TimescaleStart = objDate;

Could it be that you're actually trying to change the project start date?
If that's the case, try using the "ProjectMove" method.
reference here and here.
If you really really want to change TimescaleStart, it looks like you are out of luck.

TimescaleStart is a read-only property which returns the date that the timescale starts in the current view.
If you want to scroll the view so that it starts at a certain date, find a task with a start date on or close to your target date, select it and invoke the GotoTaskDates method of the application object. For example:
app.Find "Start", "is greater than or equal to", "1/1/2014", Type.Missing, Type.Missing, Type.Missing, Type.Missing
app.GotoTaskDates
Update:
If you are using Project 2010 or later, you can also use this method:
app.PanZoomPanTo (objDate)

Related

Multiple Filter conditions in MPP thru Project Interop

I am using MS Project interop to automate reading MPP. Implementing single column filter is working however just wondering, how do I implement multiple column filter with or/add condition? I am using below code to filter single column which works:
public string Load(string fileName)
{
MSProject.ApplicationClass app = null;
try
{
app = new MSProject.ApplicationClass();
app.Visible = false;
if (app.FileOpen(fileName, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
{
app.FilterEdit("My New Filter", true, true, true, Type.Missing, Type.Missing, "Resource Names", Type.Missing, "equals", "ResourceNameHere1", Type.Missing, true, true);
//app.FilterApply("Using Resource...", "false", "ResourceNameHere1");
app.SelectAll();
MSProject.Tasks tsk = (MSProject.Tasks)app.ActiveSelection.Tasks;
}
}
catch(Exception e){}
}
Q:
How to add one more resource name in condition? Like to pull ResourceName1 along with ResourceName2 with and/or condition or add condition to filter by % complete ?
Update
Since you want summary tasks, creating a filter is a good option. Here's how to create a filter with 3 criteria--2 resources and % Complete:
app.FilterEdit("My New Filter", true, true, true, Type.Missing, Type.Missing, "Resource Names", Type.Missing, "contains", "ResourceNameHere1", Type.Missing, true, true);
app.FilterEdit("My New Filter", true, false, true, Type.Missing, Type.Missing, Type.Missing, "Resource Names", "contains", "ResourceNameHere1", "Or", true, true);
app.FilterEdit("My New Filter", true, false, true, Type.Missing, Type.Missing, Type.Missing, "% Complete", "is less than", "100", "And", true, true);
Previous answer
If the goal is to loop through a set of tasks that meet certain criteria, there is no need to bother with creating a filter, setting it, then selecting all visible tasks. Here's pseudo code to get you started:
For Each t In ActiveProject.Tasks
If (t.ResourceNames Like "*Name1*" Or t.ResourceNames Like "*Name2*") And t.PercentComplete < 100 Then
' do something
End If
Next t
FYI: To get a reference to a task's summary task, use the OutlineParent property.

Reference not valid error in excel hyperlink

I am using the below piece of code to add hyperlink to a given cell("A1" here):
Workbook workbook = _excelApp.Workbooks.Open("C:\\temp\\test1.xlsx",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "www.bing.com";
Range excelRange = worksheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
worksheet.Hyperlinks.Add(
rangeToHoldHyperlink,
hyperlinkTargetAddress,
string.Empty,
string.Empty,
valueArray[1, 1].ToString());
It adds the hyperlink. But, on clicking it, it says:
Reference not valid
On hovering the mouse over cell text, it displays the hyperlink as:
excel_file_path\hyperlinkTargetAddress
Why it is doing so?
How this can be overcome?
You can add different types of hyperlinks. By default, Excel assumes that you want to link a file in your hard drive (in the folder where the spreadsheet it). To tell Excel that you want a website, you have to write the full address (with the starting http:// bit).
string hyperlinkTargetAddress = "http://www.bing.com";

C# Office interop Excel - out of boundary

I have a program that I need to maintain ( was not written by me)
What the actual function is doing is that it gets an excel file from the internet and then convert it to html (I don't even know why) then it tries to save it into a new csv file...
Long story short, my problem in the whole code resides in these 2 parts
xl = new Microsoft.Office.Interop.Excel.Application();
xl.Visible = false;
//p_sUBKPath is the path of the HTML file that has the converted excel file
Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(p_sUBKPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;
object[,] values = (object[,])range.Value2;
The code has an if statement that try to ensure that every values from the first row in the html matches values required
if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "DOMAIN NAME" && values[1, 2]).ToUpper().Trim() == "SHORT NAME" && values[1,3]).ToUpper().Trim() == "LONG NAME" )
{
//do something
}
The if statement is returning true and jumping to else statement, after few debugging and trying some lines of code it turned out that values[1,2] and values[1,3] are returning out of array boundary error.
and the value of values[1,1] is the whole row which contains DOMAIN NAME, SHORT NAME, AND LONG NAME, so even this statement returns flase
if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "DOMAIN NAME")
{
//do something
}
Any idea what is happening in the code?

Dynamically change range of FormatCondition

I have added a Format Condition on a range with some formatting using the below code.
Microsoft.Office.Interop.Excel.FormatCondition formatConditionObj = null;
formatConditionObj = (Microsoft.Office.Interop.Excel.FormatCondition)myRange
.FormatConditions.Add(Excel.XlFormatConditionType.xlExpression,
Type.Missing, true, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
formatConditionObj.Interior.ColorIndex = 5;
Now my range changes dynamically.I want that using the same FormatCondition object i just change the range where this formats are applied. For e.g at the first instance it might be just "$A$1" later it might be "$A$2,$D$5" or "$A$3:$A$20".
This can be done using excel directly. Contitional Formatting -> Manage Rules -> Applies To(Edit this). How can be this acheived using C#.
This works for me. I find putting the "using" line below at the top saves a good deal of effort.
using Microsoft.Office.Interop.Excel;
<...>
formatConditionObj = (FormatCondition)myRange.FormatConditions
.Add(XlFormatConditionType.xlExpression,
Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
formatConditionObj.Interior.ColorIndex = 5;
Range myNewRange = ws.Range["a10:a15"];
formatConditionObj.ModifyAppliesToRange(myNewRange);
<...>

Open a Read Only file as non read only and save/overwrite

I have a number of word documents I am converting. Everything is going great until I get a file that is read only. In this case I get a Save As prompt.
Is there any way to open the file in read/write format? I should have admin privileges so access isn't an issue.
I'm using VB.net to open the files. More specifically
doc = word.Documents.Open(path, Type.Missing, False, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
To open a read-only file you need to set that attribute to false:
string path = "C:\\test.txt";
FileInfo info = new FileInfo(path);
info.IsReadOnly = false;
StreamWriter writer = new StreamWriter(path);
writer.WriteLine("This is an example.");
writer.Close();
info.IsReadOnly=true;
This was an example but I'm sure it will work with word files.
EDIT:
VB.NET equivalent:
Dim path As String = "C:\test.txt"
Dim info As FileInfo = New FileInfo(path)
info.IsReadOnly = False
Dim writer As StreamWriter = New StreamWriter(path)
writer.WriteLine("This is an example.")
writer.Close()
info.IsReadOnly = True
Before you open the file, check its Attributes with a FileInfo class.
If the Attributes property contains FileAttributes.ReadOnly, change it and the file will no longer be read-only.

Categories