Set domSession = CreateObject("Notes.NotesSession")
Dim domDatabase
Set domDatabase = domSession.GetDatabase("server Name", "xyz.nsf")
Set domView = domDatabase.GetView("All Projects")
Up to here it's ok. But I try lot but unable to read the document values.
I am looking for the vbscript code to read the document data.
Add this:
Dim i%
Dim s$
Dim doc As Object
For i% = 1 to domView.EntryCount
Set doc = domView.GetNthDocument(i%)
s$ = doc.GetItemValue("itemname")(0)
' Do whatever you want with the string value which is now stored in s$
Next
This code assumes the items are text type. You could use simply Dim s to get any item type (number, date).
To get column values from the view you can use doc.ColumnValues(2). This returns the value of 3rd column (0 is first).
Related
I'm writing a macro to set categories/series names. New labels are taken from some dictionaries. My first step however was to get those names from each chart. So, in order to get it, I used the code below:
'works fine
'x - any integer
'whole sentence
Application.ActivePresentation.Slides(x).Shapes(x).Chart.ChartGroups(x).CategoryCollection(x).Name
'with variables
Dim objChart As Chart
Dim objChartGroups As ChartGroups
Dim objChartGroup As ChartGroup
Dim objCategoriesColl As CategoryCollection
Dim objCategory As ChartCategory
Dim strCategory As String
Set objChart = ActivePresentation.Slides(x).Shapes(x).Chart
Set objChartGroups = objChart.ChartGroups(x)
Set objChartGroup = objChartGroups.Item(x)
Set objCategoriesColl = objChartGroup.CategoryCollection
Set objCategory = objCategoriesColl(x)
strCategory = objCategory.Name
I checked this property (Name) many times on Microsoft Docs (https://learn.microsoft.com/office/vba/api/powerpoint.chartcategory.name) and it is stated that it is a read/write property. However, when I try to set a new name (in the code below) I receive an error. Is it possible there's a mistake in Docs?
Public Sub EnterNewChartCats( _
lngCategoriesCount As Long, _
objCategoriesColl As CategoryCollection, _
dctDT As Dictionary, _
dctConsts As Dictionary, _
dctRegexes As Dictionary, _
dctUniques As Dictionary)
'lngCategoriesCount - number of categories in a chart
'objCategoriesColl - CategoryCollection object (a list of categories)
'dctDT - a dictionary for names where there's no need to change the label
'dctConsts - a dictionary for non-unique names which appear many times in a presentation
'dctRegexes - a dictionary for names which can be replaced using regular expressions
'dctUniques - a dictionary for names taken from an Excel file (already done)
'booleans for check if a name appears in a dictionary
Dim blnInDT As Boolean
Dim blnInConsts As Boolean
Dim blnInRegexes As Boolean
Dim blnInUniques As Boolean
Dim blnAdd As Boolean
'creating standard variables
Dim objCategory As ChartCategory
Dim i As Long
Dim strCategory As String
Dim strUCaseToCheck As String
For i = 1 To lngCategoriesCount
Set objCategory = objCategoriesColl(i)
strCategory = Trim(objCategory.Name)
'trimmed and upper-case category string to compare with dictionary keys
strUCaseToCheck = UCase(strCategory)
'checking if a category appears in a dictionary
blnInDT = blnInDct(dctDT, strUCaseToCheck)
blnInConsts = blnInDct(dctConsts, strUCaseToCheck)
blnInRegexes = blnAnyValueLikeDctRegex(strUCaseToCheck, dctRegexes)
blnInUniques = blnInDct(dctUniques, strUCaseToCheck)
'checking if a category meets a condition - at least 1 occurrence in any dictionary
blnAdd = blnAnyValue(True, blnInConsts, blnInRegexes, blnInUniques)
'blnAlphabeticChars - a function to check if a category has any letters because
'there's no need to change such labels as '2021' etc.
If Not blnInDT And blnAdd And blnAlphabeticChars(strCategory) Then
'ERROR (objCategory.Name): Can't assign to read-only property
If blnInConsts Then
objCategory.Name = dctConsts(strUCaseToCheck)
ElseIf blnInRegexes Then
objCategory.Name = dctRegexes(strUCaseToCheck)
ElseIf blnInUniques Then
objCategory.Name = dctUniques(strUCaseToCheck)
End If
End If
Next i
Set objCategory = Nothing
End Sub
I've found a workaround (SeriesCollection(x).Values or .XValues) but it's not convenient to use. So, my question is: does exist any way to use Name to set new labels? If no - do you know other workarounds?
EDIT: I've finally managed to change labels via objChart.ChartData.Workbook but it's very slow and, more importantly, a workbook has to be activated (opened) in order to refresh categories/series names. That influenced performance and time of execution greatly so I'm still looking for any other way to do so.
Your first listing is trying to set the name of .CategoryCollection(1). But the help page that you linked to is about .ChartCategory.
In any case, here's how to set the category names:
ActivePresentation.Slides(1).Shapes(1).Chart.Axes(xlCategory).CategoryNames = Array("Test1", "Test2", "Test3", "Test4")
I have a need to refer to a seriescollection by name. The MSDN object model page for seriescollection states either an index or a name can be used. However when I try to use a string variable I get error 1004 'Invalid parameter':
Dim sSeriesName As String
dim aRootCats() as string
Dim cSeriesCol As SeriesCollection
For x = 0 To UBound(aRootCats)
sSeriesName = aRootCats(x)
Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(sSeriesName)
Next x
I've tried the following too:
sSeriesName = CStr(aRootCats(x)) ' just in case, because I filled this array with variant data type earlier
Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(sSeriesName)
and
Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(aRootCats(x))
and
Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection("Product Support")
UPDATE: I'm also experiencing the same error when looping on the seriescollection:
For c = 1 To cChart.ChartGroups(1).SeriesCollection.Count
Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(c)
Next c
Any ideas?
Many thanks.
Because you are using a ChartGroup not a Chart, you have to use the Item property of the SeriesCollection and you have to pass a variant. Use either:
cChart.ChartGroups(1).SeriesCollection.Item(1)
or:
cChart.ChartGroups(1).SeriesCollection.Item(CVar(sSeriesName))
(or declare the variable as Variant rather than String)
Attempting to split and store strings from a listbox and search then search the contents of the text file I have stored them to, hopefully sorting them in different categories,
firstly I am getting an "Object reference not set to an instance of an object." error with this
Dim variable As String = Nothing
If listArray.SelectedIndex > 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
Dim part As String() = variable.Split(New Char() {","c})
Dim line As String
For Each line In part
MessageBox.Show(line)
and secondly, would this be the right code to use for searching those separated strings?
For count As Integer = 0 To Logbook.listArray.Items.Count - 1
Dim searchIndex As String = Logbook.listArray.Items(count).ToString
If searchIndex.Contains(indexSearch.Text) Then
Logbook.listArray.SetSelected(0, True)
End If
Next
I'm pretty new to StackOverflow, my apologies if i'm not up to date with the website etiquette.
am getting an "Object reference not set to an instance of an object."
I guess you don't know that the first item is at index 0 and that SelectedIndex returns -1 if there is no item selected. That's why following code throws that exception:
Dim variable As String = Nothing
If listArray.SelectedIndex > 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
Dim part As String() = variable.Split(New Char() {","c}) ' <--- variable Nothing if first item selected
Then you just have to use <> -1 or >= 0:
Dim variable As String = Nothing
If listArray.SelectedIndex >= 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
According to the second part of the question(always ask only once), you haven't provided enough informations to understand what you want and what doesn't work with your code.
I'm new to VBA and i need help.
I want to create vba function which takes table name as input, and distinct specific field from that table. I created function, and it works when i run it in vba immediate window (when i use debug.print command to display results). But when i call this function in sql, instead whole field values, it returns just last one. I'm not good at vba syntax so i need help to understand. Does function can return more than one value? If can, how, and if not what else to use? Here's my code:
Public Function TableInfo(tabela As String)
Dim db As Database
Dim rec As Recordset
Dim polje1 As Field, polje2 As Field
Dim sifMat As Field, pogon As Field, tipVred As Field
Set db = CurrentDb()
Set rec = db.OpenRecordset(tabela)
Set sifMat = rec.Fields("Field1")
Set pogon = rec.Fields("Field2")
Set tipVred = rec.Fields("Field3")
For Each polje1 In rec.Fields
For Each polje2 In rec.Fields
TableInfo = pogon.Value
rec.MoveNext
Next
Next
End Function
Any help is appreciated.
The problem is with this line probably:
TableInfo = pogon.Value
It runs inside the loop and returns the last value of the loop.
Instead of returning one value TableInfo, you may try to return something similar to a Collection or an Array.
Inside the loop, append values in the Collection and after the loop, return the Collection back from the function.
Edit:
I have re-written the code shared by you:
Public Function TableInfo(tabela As String) as String()
Dim db As Database
Dim rec As Recordset
Dim polje1 As Field, polje2 As Field
Dim sifMat As Field, pogon As Field, tipVred As Field
Dim returnValue() As String
Dim i as Integer
Set db = CurrentDb()
Set rec = db.OpenRecordset(tabela)
Set sifMat = rec.Fields("Field1")
Set pogon = rec.Fields("Field2")
Set tipVred = rec.Fields("Field3")
' I am not going to modify this but I think we can do away with two For Each loops.
' Just iterate over rec like
' For Each r In rec -> please use proper naming conventions and best practices
' and access each field as r("Field1") and r("Field2")
For Each polje1 In rec.Fields
For Each polje2 In rec.Fields
returnValue(i) = pogon.Value
i = i + 1
rec.MoveNext
Next
Next
TableInfo = returnValue
End Function
Please note: I have not tested this code but I assume this should work for you. Also, I have assumed that you want to return String() array. Please change the data type if you want to return some other type.
When you call the array (as posted in theghostofc's answer), you will need to do something like this:
Dim TableInfo() As String
For i = LBound(TableInfo) To UBound(TableInfo)
YourValue = TableInfo(i)
... Process some code that uses YourValue
Next i
If you're not looping through your array, you're not going to get each individual value out of it.
So I have a view (object name is 'view') in a Lotus Domino db from which I want to grab the column names and put them in an array:
Dim view As Domino.NotesView
Set view = db.GetView(viewScen)
//viewScen is a string containing the actual view's name
//db is a string containing the actual db name
These declarations work fine, but when I try to assign those values to an array using the method called 'getColumnNames', the VBA editor tells me that the method is not supported for that object:
Dim someArray() As String
//I tried both of the following with no sucess...
someArray = view.getColumnNames
someArray() = view.getColumnNames
What am I doing wrong?
I think you can do a For..Each with columns
dim idx as integer
dim OneCol as Column
redim someArray(view.Columns.Count)
For idx = 0 to view.Columns.Count - 1
someArray(idx) = view.Columns(idx).name
Next
According to the 8.0 help files, the getColumnNames method is not supported in COM/OLE. However, the attribute ColumnNames is supported. This is VB code from the Help file:
Private Sub ViewColumnNames_Click()
Dim s As New NotesSession
s.Initialize
Dim dir As NotesDbDirectory
Dim db As NotesDatabase
Dim v As NotesView
Dim cns As String
Set dir = s.GetDbDirectory("")
Set db = dir.OpenDatabase("Web test")
Set v = db.GetView("Main View")
For Each cn In v.ColumnNames
cns = cns + cn + Chr(10)
Next
MsgBox cns, , "Columns in Main View"
End Sub