Get the range of the whole word document including all stories - ms-word

How can I get the range of the 'whole' document including all stories as wdMainTextStory, wdFootNotesStory, wdEndNotesStory etc. And also I need to get the range of wdFootNotesStory.
I have tried sevaral ways.
ActiveDocument.Range(0, 0).Select
Selection.WholeStory
This only select the whole wdMainTextStory. If anybody could, please help me to go through this.

I have found a way to get the range of FootNotes.
dim objRange As Word.Range
For Each storyRange In ActiveDocument.StoryRanges
If storyRange.StoryType = wdFootnotesStory Then
objRange = storyRange
End If
Next storyRange

Related

How to retrieve a count of all records where a Date field is within the next year, and store it in a variable?

I am fairly new to MS Access, but have a decent understanding of databases, with some knowledge of SQL.
I am creating a database in Access. On a main form that users will see first, I need to display a count of all records from my Case table, which have a StatuteOfLimitation date that is within the next year.
My goal was to create a label describing the information, with a button below it. The button will open a report of all of the records (this part is working fine), and I wanted the caption for the button to display the total count of how many records meet the criteria.
The only way I can think to do it, is to retrieve the count and store it into a variable. From there, I should be able to set the caption to the variable value.
I have seen a few methods of retrieving a count and storing it in a variable, but all that I found only stored a count of EVERY record, without filtering for the date range.
This was the best that I could think of, but it is not working:
Private Sub Form_Load()
Dim oneYearFromToday As TempVars
SET TempVars!oneYearFromToday = (SELECT COUNT(StatuteOfLimitation) FROM Case
WHERE StatuteOfLimitation <= DateAdd("yyyy", 1, Date());
End Sub
DCount() provides a simple approach for "How to retrieve a count of all records where a Date field is within the next year"
Dim lngCount As Long
lngCount = DCount("*", "Case", "[StatuteOfLimitation] <= DateAdd('yyyy', 1, Date())")
Then you can use that count in your command button's Caption property. Say the button is named cmdOpenReport ...
Me!cmdOpenReport.Caption = "Report " & lngCount & " cases"
If you want the count in a TempVar instead of a regular Long variable, declare it As TempVar (just one) instead of As TempVars (a collection). And when you assign the value to it, don't use Set.
Dim oneYearFromToday As TempVar
TempVars!oneYearFromToday = DCount("*", "Case", "[StatuteOfLimitation] <= DateAdd('yyyy', 1, Date())")
I probably wouldn't use a tempvar to store your variable. you can try something like below using the DAO.
Private sub Form_Load()
dim rst as dao.recordset
dim strSQL as string
'Creates query string
strsql = "SELECT Count(StatueOfLimitation) as RecordCount " & _
"FROM Case " & _
"WHERE (((StatueOfLimitation) <= DateAdd('yyyy',1,date())));"
'Opens the query string into a recordset
set rst = currentdb.openrecordset(strsql)
'Change Labelnamehere isnto the name of the label control on your form
'Change what ever saying you want here to something you want the label to display
me.labelnamehere.caption = "What ever saying you want here " & rst![RecordCount] 'Don't need a variable storage is you can use the result here
rst.close 'closes recordset
set rst = nothing 'Clears memory
EndCode: 'Ensures clean up is successful
If not rst is nothing then
rst.close
set rst = nothing
end if
end sub
If this doesn't work for you, please let me know and I'll do some more digging.
I'm not able to comment on HansUp's answer, but to change a label caption in VBA you need to open the form in Design view. It's not something I typically do, my personal preference is to use an unbound text box with labels that don't change, but I have done it in one database to update the time/date and user for the last email sent.
The code would look like this:
DoCmd.OpenForm "yourformname", acDesign, , , , acHidden
Forms![yourformname]![yourlabelname].Caption = "There are " & TempVars!onYearFromToday & " cases to view."
DoCmd.Close acForm, "yourformname", acSaveYes
DoCmd.OpenForm "yourformname", acNormal
It's a bad idea to use a button for info display. That's not the purpose of a button.
Use a textbox and set its ControlSource:
=DCount("*","[Case]","[StatuteOfLimitation]<=DateAdd("yyyy",1,Date()))
It will fill by itself, and the button is free to open the report at any time.

How to run a spell check on a string without user input?

I'm trying to run a spell check on a string using word, but I don't want to have to go through each word manually and determine the correct spelling. Is there a way to do this automatically, so that it corrects all the misspelled words on its own? Here is my code so far, this works but I have to go through each word and hit change...
If Address.Length > 0 Then
Dim word_server As New Word.Application
word_server.Visible = False
Dim doc As Word.Document = word_server.Documents.Add()
Dim rng As Word.Range
rng = doc.Range()
rng.Text = Address
doc.Activate()
doc.CheckSpelling()
Address = doc.Range().Text
doc.Close(SaveChanges:=False)
word_server.Quit()
doc = Nothing
rCell.Value = Address
End If
Use the GetSpellingSuggestions function to bypass the GUI. to see if there are any spelling suggestions, and then you can set it to the first suggestion (which could be a bad idea).
How do you want to determine what the correct spelling is? Should "spon" be spoon, span, spin, spun, or son? This code optimistically assumes that the first suggestion (if one exists) is the correct solution.
Declare:
Dim oError As Word.Range
And then replace:
doc.Activate()
doc.CheckSpelling()
with:
For Each oError In rng.SpellingErrors
If oError.GetSpellingSuggestions.Count > 0 Then
oError.Text = oError.GetSpellingSuggestions().Item(1).Name
Else
'Uh oh, no suggestions, do something here.
End If
Next
These websites might help. The example code in the website shows how to call Word to spell check. You should be able to modify it, to use with your code.
http://www.vb-helper.com/howto_net_spellcheck.html
http://www.vbforums.com/showthread.php?307151-SPELL-CHECK-and-WORD

(Vba) Ms Word: Work around the 255 characters limit

I'm new to programming and I'm trying to copy the content of a form field to another form field in the same Word document like this:
Sub Copyfield()
Dim Temp As String
Temp = ActiveDocument.FormFields("Field1").Result
ActiveDocument.FormFields("Field2").Result = Temp
End Sub
My problem is that my "Field1" is a piece of text of more than 255 characters which seems to be a problem with "Result". I know there is a very similar topic here: Passing MS-Access string >255 characters to MS-Word field but I still don't have the 50 reputation to comment on that thread.
Could anyone please help me understand how to implement the changes in my code?
Well, here's one possibility. Since I don't have your environment it was easier for me to test text in the document rather than another form field with so much content. You'll need to adjust the code accordingly.
The key is to get the Selection "inside" the form field so that it doesn't hit the "protection barrier". Just using FormField.Select puts the focus at the beginning of the field, which VBA is seeing as "protected". Moving one character to the right corrects that and long text can then be assigned to the Selection. But the field needs to have content.
So what my code is doing is "slicing off" the first word of the text to go into the form field. That's short enough to assign to the Result property and lets the Selection move to its right. Then the rest - the long text - can be assigned to the Selection.
You'll probably want to assign the entire FormField.Result to a string variable, then manipulate that string.
Sub WriteLongTextToFormField()
Dim ffld As word.FormField
Dim doc As word.Document
Dim rng As word.Range
Dim s1 As String, s2 As String
Set doc = ActiveDocument
'Get the long text
Set rng = doc.Range(doc.Paragraphs(1).Range.Start, doc.Paragraphs(6).Range.End)
'Split off a bit to go into FormField.Result
s1 = rng.Words(1)
rng.MoveStart wdWord, 1
'The rest of the long text, to be assigned to Selection.Text
s2 = rng.Text
Set ffld = doc.FormFields("Text1")
ffld.result = s1
ffld.Select
Selection.MoveRight wdCharacter, 1
Selection.Text = s2
End Sub
Ok, after 3 days at the border of madness, finally thanks to the help of #Cindy Meister (and some serious personal digging), I made it work. Maybe it's not a big deal for you geniouses out there but believe me for me it was like seeing everything in Matrix code (from the movie guys, the movie).
I want to post it and share because I tried to find it in every corner of our Internet and part of the extraterrestrial one and I couldn't. So hopefully it will be useful for another programming illiterate / dumb person (as myself).
Here is the code:
Sub CopyField()
Dim ffld As Word.FormField
Dim doc As Word.Document
Dim rng As String
Dim s1 As String, s2 As String
Set doc = ActiveDocument
'Get the long text
rng = ActiveDocument.FormFields("Field1").Result
'Split off a bit to go into FormField.Result
s1 = Left(rng, 4) 'Keeps the first 4 characters of the rng string starting from left to right this can be adapted
'The rest of the long text, to be assigned to Selection.Text
s2 = Mid(rng, 5) 'Starting from the 5th character from the left keeps the rest of the string
Set ffld = doc.FormFields("Field2")
ffld.Result = s1
ffld.Select
Selection.MoveRight wdCharacter, 1
ActiveDocument.Unprotect 'Unprotects the document!
Selection.Text = s2
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True 'Protects the document again!
ActiveDocument.Bookmarks("Field1").Select ' "Sends cursor" back to Field1
End Sub
Big part of the code is originally by #Cindy Meister... I just adapted it to my situation where I had 2 form fields instead of paragraphs. I also had to add some lines to unprotect the document at a certain point in order to make it work (ask the Pros for the reason) and a final instruction to come back to "Field1" (which is some pages up) after the process. Finally just a note for my dumb fellows: I added the macro "on exit" on the "Field1" properties to automatize the process.
Huge thanks Cindy again and I hope you help in my dark programming moments again ! (please do)
:)

Utilizing Access and VBA: copying information between forms

Searched around a bit and could not find an existing post with my exact question.
I am creating an Access (2003) database utilizing multiple forms and tables that need to communicate and share information with one another (i.e. - first/last name, etc.). The main form/table, "Personnel", will hold a majority of the information, yet the other forms/tables will only hold information pertinent to certain situations/circumstances, so not every entry in "Personnel" will have a concurrent entry in "Hired", for example. If a record with the same name already exists, I would like a MsgBox to tell me so and open that entry (in the form, for editing), if not a new entry would be created, auto-populated with pre-selected fields (i.e. - first/last name).
After an option is selected from a pull-down menu, the appropriate form is accessed/opened, "Hired", for example (This part works).
The copying information across forms does not work.
Dim rstPers, rstHired As DAO.recordset
Dim LastName As String
If status = "hired" Then
DoCmd.OpenForm "Hired Information"
Set rstPers Forms!Personnel.RecordsetClone
Set rstHired Forms![Hired Information].RecordsetClone
????
...
End If
I have tried to do this multiple ways but nothing seems to work. Nothing populates in the new Form or table. Am I looking at it the wrong way? Should I try a different approach?
Hope my explanation makes sense. Thanks for any help.
-Charles
You have a bad approach indeed.
Your forms are linked to a table. So you should avoid as much as possible to manipulate or retrieve a form's data directly using a recordsetclone, instead try to retrieve this data directly from the table.
So if you want to open your "hired information" form:
Dim RS As Recordset
Dim IDperson As String
IDperson = me.ID ' or whatever
Set RS = CurrentDb.OpenRecordset("SELECT ID FROM TableHired WHERE ID='" & IDperson & "'")
If RS.BOF Then
' No record exist - Open form in record creation
DoCmd.OpenForm "FormHired", acNormal, , , acFormAdd
Else
' record exists, open form and position it oon the record's ID
DoCmd.OpenForm "FormHired", acNormal, , "ID='" & RS!ID & "'", acFormEdit
End If
Of course it won't work like this as you didn't provide enough info.
Review this code and adapt it with your fields name (IDs), table name (TableHired) and FormName (FormHired) and following the situation on where and how you will trigger it. Also if your ID is not a string, you should remove the quotes in the SQL

Get all top level entry values from NotesView

What is the easiest way to get all top level entry values from a notes view? I have found the property "TopLevelEntryCount" returning the number of alle categories, but I want the values, too.
To iterate the view entries and column values need too much time.
Set ecl = view.Allentries
Set ve = ecl.Getfirstentry()
While Not(ve Is Nothing)
If IsArray(ve.Columnvalues(0)) Then
If flag = "" Then
arr = ve.Columnvalues(0)
Else
arr = ArrayUnique(ArrayAppend(arr, ve.Columnvalues(0)))
End If
Else
'error if arr is not already an array
arr = ArrayUnique(ArrayAppend(arr, ve.Columnvalues(0)))
End If
flag = "1"
Set ve = ecl.Getnextentry(ve)
Wend
Anyone know a faster way? It must not be written in LotusScript, actually I would prefer Java, JS or SSJS.
Idea 1: You can use the NotesViewNavigator class, and call the getFirst() and getNextCategory() methods. This should be faster than walking all the entries.
Idea 2: You can use NotesSession.Evaluate() with a formula that does an #Unique on #DbColumn for the first column of view. That should bring you back an array, and you can get the ubound of the array. Formulas tend to be very fast, but evaluate() has to compile them first, so I don't know if this will be faster or not. The disadvantage of this approach is that for very large views, this could exceed formula language's size limits. But if it does prove to be faster, you could catch the exception and fall-back to the slower method of iterating.
Richard's response is perfect!
For a java version it's almost the same:
see the Domino help for getNextCategory the given example below ViewNavigator really answer your need: link here
The evaluate method is also available in java Session