Read/get the current value of an object in Project VBA - vba

I'm struggling to save the current value of an object (at least I think it's an object) in MS Project VBA.
Here's what I'm trying to do, save the value of my current header text.
dim a As String (or Variant/Object?)
a = Application.FilePageSetupHeader.Text.Value
This obviously doesn't work, but gets the point across.
The syntax for this is:
expression. FilePageSetupHeader( ** Name, ** Alignment, ** Text** )
with Name and Text being passed as a string.
If I get this to work, I'll also be interested in extracting the NormalType value from this object. Notice I'll have to specify the item:
GridlinesEditEx Item:=4, NormalType:=1
Any help would be appreciated!

FilePageSetupHeader is a method of the application object, not a property (see MSDN). The MS Project object model does not have a header object to interrogate, so unfortunately the headers (and footers) are write-only.

Related

Change selected ComboBox item, from text file string not working

Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic
To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.

Inconsistent recognition of ranges (No errors thrown)

This is code from Excel 2010. All of the code resides within the workbook itself. Information is gathered using internal forms and the code is run.
I have code that retrieves data from a spreadsheet and populates an object with that data. The row number is dynamic as it is dependent on the form input. The column is by the header, not the column number. The following code works perfectly but for two anomalies:
cTank.RowForTankSpecs = rNum
cTank.MP = .Cells(rNum, Range("MP").Column).Value
cTank.Form = .Cells(rNum, Range("formName").Column).Value
cTank.TankProcess = .Cells(rNum, Range("Process").Column).Value
cTank.Location = .Cells(rNum, Range("Location").Column).Value
cTank.TankName = .Cells(rNum, Range("Tanks").Column).Value
cTank.tankID = .Cells(rNum, Range("TankID").Column).Value
First:
The cTank.TankName is retrieving information from a column named "Tanks". That column does not exist. The actual column header is "Tank". But, it is retrieving the correct information. If I change the name to what it really is (Tank), it does not work.
Second:
When the cTank.TankID line is executed, I get the following error on the Range("TankID"):
Runtime Error 1004: Method 'Range' of object '_Global' failed
This one has the appropriate header (column header), but it is not recognizing the range.
I have tried simple things such as changing the order of the code, but it doesn't help. As earlier stated, the other lines work. Later in the program, information is gathered in the same manner but using another worksheet from the same workbook, and none of them are working. I've double checked that strings are strings and integers are integers, etc. I've double checked the column headers match the range names. Nothing seems to jump out at me.
I would appreciate any input you may have on the situation.
Thanks in advance.
Steve
Ok. Being pretty sure my code was correct, I went to the spreadsheet itself. For some reason it was recognizing only certain columns and it was recognizing one of them incorrectly. So I started highlighting the columns that worked and also the columns that didn't. What I noticed was that on the columns that were being recognized, that column header was displayed where the cell location is normally displayed whereas on the columns that were not being recognized, the cell location (i.e. A1, A2, etc.) for the header was being displayed and not the header title itself. The incorrect label was showing up for one of them. As it turns out, the mislabeled column was one that I had used for a form dropdown menu. So, I checked the name manager, and the ones that were working were listed. So anyway, using the name manager, I added named ranges using the headers. Now, when I select the columns, the column header(named range) appears in that window and now, the code works.
Thanks guys for your input. I really appreciate it.
Two things you can do:
Do not use use Range, but as it seems you are using names, use Names("Yourname").Referstorange.
OR
Make sure your names are set up correctly using the Name Manager in Data Ribbon.

VB.NET determine which property is not yet assigned a value

I'm doing a visual studio 2010 project in VB.NET to import an ASCII file into a databae based on given fields in the ASCII file. Sometimes, there can be some optional fields missing from the ASCII file while importing.
How can I find out which property of a big class has not yet been assigned a value? Based on this, I want to show a form to request for the missing properties at run time!
Someone correct me if there is a faster way,
Dim myClassInstance As new MyClass();
If myClassInstance.SomeProperties is Nothing Then
'I'm null
End If
Do this for all properties that needs to be checked.

Having a variable reference an object in VBA

I'm writing some VBA code to dynamically position some elements on a form. What I would like to do is create a two dimensional array of references to 11 rows of label and text box objects, then step through the array and position each "row" differently based on the properties of the above rows.
Unfortunately, I cannot seem to find a way to store the references! The following code
produces the error: "Object doesn't support this property or method"
Dim fields(10,10)
fields(0,0) = txtFirstName
If I assign an object to a single variable like so
Dim field
field = txtFirstName
MsgBox field.Top
Then I get no error message from the assignment, but the reference to field.Top gives me the error "Object Required".
Please, what am I doing wrong here. I'm new to stack overflow and VBA coding so forgive me if this is an easy fix, but I've leveraged my google-fu and I'm just not finding a solution. Many thanks to anyone who can help.
You need to add the Set keyword, since you are dealing with object references.
Set fields(0,0) = txtFirstName
The reason you get the error, is because VBA will default to the text property of the textbox, giving you a string instead of the textbox object.

An item with the same key has already been added - csvreader.fieldcount

I'm trying to create an import program from CSV.
My code is
csv = New CsvReader(New StreamReader("CSVFileLocation"), True)
Dim fieldCount As Integer = csv.FieldCount
The error message "An item with the same key has already been added." on the second line.
If I changed "HasReaders" to "False", there's no such error. But, I'm not able to get the Headers.
Could somebody help me on this, please?
FYI: I'm using Visual Studio 2010 version.
Regards,
Richard
Check that your CSV file may have duplicate column names, or multiple empty cells, in the header row?
If that's the case, try to loop through your csv object, and try rename the headers in code before calling the property FieldCount.
My guess is that the CsvReader class is going through the first row adding strings to a dictionary, and the header row has two cells with the same value (so two identically named fields). Take a look at your data and see if this is the case. Alternately, if you have access to the source code for CsvReader, you could have it handle this case by naming the second field something slightly different (e.g., by appending a "1" onto the end of its name).