I am trying to create an MPP file (MS Project) using VB.NET in a windows forms application using the MS Project Interop. I have been able to, create the project, load the resources and the tasks, but now I am stuck on how to assign the values for a resource to a task. I need to set it at the month level.
Since the values can be different depending on the month, I cant figure out how to even begin.
The months and years could be different. I need to be able to get the month/year and value and assign it to that task and resource.
Any ideas, because I do not know where to start. Since you can zoom in and out in project to view it by day, week, month, qtr, year, it is confusing how to just set it by month. Not sure if TimeScale has anything to do with it.
Any help would be very much appreciated. There isnt much out there in terms of coding for MS Project like there is for excel or other office products.
In order to create periodic assignments (e.g. day, week, month) you will need to use one of the TimeScaleData Methods (assignment, task, resource).
https://learn.microsoft.com/en-us/office/vba/f1/timescaledata-method-project-vbapj-chm131262?f1url=%3FappId%3DDev11IDEF1%26l%3Den-US%26k%3Dk(vbapj.chm131262)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue
There is plenty of reference information about how to use Project's object model. I'm not sure how it might apply when using VB.net, but a good reference for Project VBA is available via the object browser.
John
To set assignment work by month, use the TimeScaleData method using the pjTimescaleMonths TimeScaleUnit parameter.
Here's a vba example of a helper routine that sets work for an assignment for a month:
Sub SetWorkForMonth(asn As Assignment, dteStart As Date, hours As Long)
Dim dteEnd As Date
dteEnd = DateAdd("m", 1, DateSerial(Year(dteStart), Month(dteStart), 1))
Dim tsvs As TimeScaleValues
Set tsvs = asn.TimeScaleData(StartDate:=dteStart, _
EndDate:=dteEnd, _
Type:=PjAssignmentTimescaledData.pjAssignmentTimescaledWork, _
TimeScaleUnit:=PjTimescaleUnit.pjTimescaleMonths)
tsvs(1).Value = hours * 60
End Sub
And here's sample code to demonstrate how it could be used:
Sub SampleAddAssignmentAndCustomTimeScale()
Dim tsk As Task
Set tsk = ActiveProject.Tasks.Add("LX21A...")
tsk.Start = #8/10/2022#
tsk.Duration = "120d"
Dim asn As Assignment
Set asn = tsk.Assignments.Add(, ActiveProject.Resources("Admin Functional Support 3").ID)
SetWorkForMonth asn, #8/1/2022#, 10
SetWorkForMonth asn, #9/1/2022#, 15
SetWorkForMonth asn, #10/1/2022#, 15
SetWorkForMonth asn, #11/1/2022#, 15
SetWorkForMonth asn, #12/1/2022#, 10
SetWorkForMonth asn, #1/1/2023#, 15
SetWorkForMonth asn, #2/1/2023#, 20
End Sub
See also the TimeScaleValues object which is a collection of TimeScaleValue objects.
The example code will easily translate to vb.net
Related
I am trying to set the deadline for a task by taking the amount of slack from the finish date. Thus, I need to be add or take days from the finish and not count any non-working time.
I have made DateAdd work however this includes non-working time, so for instance if I am setting the deadline for 7 days before the finish (11/10/22) I would expect the deadline to be 30/09, giving a slack of -7
However using t.finish = dateadd("d",slack,t.finish) gives 04/10 with a finish of 11/10
I have tried Application.ProjDateAdd(t.Finish, slack, "standard") however VBA doesn't like this.
Any ideas? I know that I am doing something stupid or missing something obvious as MSP is excellent at avoiding non-working time normally so this must be user error :)
Edited to correct 1/10 to 30/09
To:
add or take days from the finish and not count any non-working
time
use the Project Application methods DateAdd and DateSubtract:
Sub SetDeadline()
Dim cal As Calendar
Set cal = ActiveProject.Calendar
Dim t As Task
For Each t In ActiveProject.Tasks
If t.TotalSlack > 0 Then
t.Deadline = Application.DateAdd(t.Finish, t.TotalSlack, cal)
Else
t.Deadline = Application.DateSubtract(t.Finish, -t.TotalSlack + 480, cal)
t.Deadline = Application.DateAdd(t.Deadline, 480, cal)
End If
Next t
End Sub
Also, take a look at Late Finish which is the latest date that a task can finish without delaying the finish of the project.
Im new to VBA, and looking for a simple code to copy say, my current finish and start dates to finish1 and start1
So I can track a shift trend
Here is a simple procedure that loops through the tasks in the active schedule and copies the start/finish dates into start1/finish1.
Sub CopyDates()
Dim t As Task
For Each t In ActiveProject.Tasks
t.Start1 = t.Start
t.Finish1 = t.Finish
Next t
End Sub
In addition to the method Rachel provided, you can use the BaselineSave method of the MS Project Application object. Even though it's called BaselineSave, the method has optional arguments that will allow you to copy current dates into any of the start/finish fields without setting the actual baseline of the tasks. I prefer this method since it can accomplished in a single line.
Sub CopyDates()
Application.BaselineSave All:=True, Copy:=pjCopyCurrent, Into:=pjIntoStart_Finish1
End Sub
Today, I want to write day, month, and year to a datetimepicker in Visual Studio.
cmd.Parameters.AddWithValue("#geboortedag", dtp_geboortedatum.Value.Day)
cmd.Parameters.AddWithValue("#geboortemaand", dtp_geboortedatum.Value.Month)
cmd.Parameters.AddWithValue("#geboortejaar", dtp_geboortedatum.Value.Year)
These work. I save day, month and year separately to three rows in my database.
However, when I want to call these values, I can't even run the thing without getting the following:
BC30068 Visual Basic AND VB.NET Expression is a value and therefore cannot be the target of an assignment.
Here's what I tried.
dtp_geboortedatum.Value.Day = row("geboortedag").ToString
dtp_geboortedatum.Value.Month = row("geboortemaand").ToString
dtp_geboortedatum.Value.Year = row("geboortejaar").ToString
All I want is to put the day, month and year I have in separate cells into the date time picker when I open a record.
PS I also tried like the help page for the error says to write to a variable first but that does nothing to help. Perhaps I did it wrong but, I can't get it to work.
Also, I've been linked to this article but this does not fix the issue. I keep getting errors that integers are strings and cannot be converted to integers, but they're integers! They're integers when they start, they're integers when they're saved into a row for integers that saves integers, they're integers when they come out. Why aren't they integers in the end when nothing special happens to them but being inputted, saved, and called?
(Posted on behalf of the question author).
None of the material I provided in the question is relevant. Turns out I had to use dt.clear(). I'm going to be honest, I don't know how this is the thing that went wrong.
I fixed the rest with cdate() and Option Strict On.
its a very long time since touched VB.NET, but I think what you need to do is pass a DateTime type to the date picker to set its value as below;
dtp_geboortedatum.Value = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
So you will need to pass your year, month and day as integers
Just substitute your row integer values for the literals I used.
Private Sub SetDate()
Dim myDay As Integer = 27
Dim myMonth As Integer = 4
Dim myYear As Integer = 2018
DateTimePicker1.Value = New DateTime(myYear, myMonth, myDay)
End Sub
I am using Microsoft Project 2010 professional and i want to know if it is possible to :
The Question
Use visual basic to call the "Level All" function under the resource tab.
Why would i want that?
I want to do this because we are using various "cadence" in our project. So the same array of tasks might be done in 4 days, or 10 days. Now when there is such a change in "cadence" we will have to increase/decrease the number of employees working on the day/night shifts and reassign tasks to wether day or night shift. This task has to be done manually. And I obviously want to automate it.
So i want to address overallocated tasks and "past deadline" errors. Hence the need to get the indicators columns value.
Project settings
I am using 2 calendars:
"DayShift" everyday from 6:30 AM to 2:30 PM
"AfterDayBeforeNight" everyday from 2:30 PM to 10:30 PM
Assigned to two resource:
The code:
Dim t As Task
For Each t In Application.ActiveProject.Tasks
Dim success As Boolean
Dim r As Resource
success = Application.SelectRow(t.ID, False)
If success Then
Dim posAJ As Integer
Dim posAS As Integer
posAJ = InStr(t.ResourceNames, "A-J")
posAS = InStr(t.ResourceNames, "A-S")
If posAJ <> 0 Then
Font32Ex CellColor:=62207
End If
If posAS <> 0 Then
Font32Ex CellColor:=32207
End If
Dim warn As String
warn = t.Warning
End If
Next
Thank you for your time.
For Q2: You can call from VBA
LevelNow All:=True
See https://msdn.microsoft.com/en-us/library/office/aa195121(v=office.11).aspx
Background, so I need to optimize daily sales without exceeding daily capacity and without going over a End of the period(EOP) total capacity. So I thought the code was working fine then I realized all it's doing is taking daily capacity to 0 until my EOP capacity reaches my target. So it's basically all or nothing so sometimes my target doesn't get hit. i.e your off by 150 units from EOP target but your daily capacity is all 200+ so it never hits the target I want. (I should mention sales could be negative)
So is goal seek what I really want? Can I set Goal to be a range(eg -10 to 10) maybe?
Sub cashPark()
Set enddate = Sheets("Cash").Range("E4")
Set Window = Sheets("Cash").Range("D8")
Set TargetWindow = Sheets("Cash").Range("D14")
Set datecount = Sheets("Cash").Range("E4")
Set cashParkVol = Sheets("Inventory").Range("BW1")
Set Repeate = Sheets("Cash").Range("E5")
cashParkVol.Offset(datecount, -2).GoalSeek _
Goal:=0 And Window.Value = TargetWindow, _
ChangingCell:=cashParkVol.Offset(datecount, 0)
Let x = 0
Do While x < Repeate
cashParkVol.Offset(datecount + x, -2).GoalSeek _
Goal:=0 And Window.Value = TargetWindow, _
ChangingCell:=cashParkVol.Offset(datecount + x, 0)
x = x + 1
Loop
End Sub
Sorry for the constant questions but I'm just getting more involved with VBA and I'm loving it so I'm trying to automate everything I can.
The Solver is the tool of choice for optimizing in Excel and it can be run from VBA. If you are not familiar with the tool, you should probably first figure out how to use it in Excel for your problem and then figure out how to automate it with VBA. Frontline System wrote and maintains the Solver Add-in and their website has some tutorials and examples. There are a number of books (e.g. "Spreadsheet Modeling and Decision Analysis" by Cliff Ragsdale) about using the solver. Ragsdale's book has examples involving clash-flow problems. If you really want to become a power-user with scripting the Solver with VBA, I recommend the book "VBA for Modelers" by S. Christian Albright since it is the only book that I know of that really focuses on solutions built around the Solver.