'selection.footnotes.count' versus 'for each footnote in selection.footnotes' - vba

I want to make some modification to the footnotes in a selected portion of a MS Word document. I would like to use a 'for each footnote in selection.footnotes', but this does not work. It returns all the footnotes in the document. I do not understand why, because if I run a 'selection.footnotes.count' this returns only the footnotes in the selected portion, which is what I want.
See this code, which can be used for testing the problem.
For example: type a number of lines in Word, insert some footnotes in each line, select one of the lines and run this code to see the difference.
Does anyone know what is wrong with my use of the For each footnote ...' statement?
For Each Footnote In Selection.Footnotes
i = i + 1
Next Footnote
MsgBox "• For ... Next: " & i & Chr(13) & _
"• Count: " & Selection.Footnotes.Count, vbOKOnly, "Number of footnotes in selection, counted using:"

I can reproduce what you describe, with the Range as well as the Selection object. I don't know "why"; it makes no sense as far as the object model goes. Sometimes, weird things happen with Selection, but that it also happens with Range...
I'd class it as a bug, but I don't have any confirmation to that effect from Microsoft.
This works, though
For ftCounter = 1 To rng.Footnotes.Count
Set ft = rng.Footnotes(ftCounter)
'Debug.Print ft.Range.Text
Next

Related

Count selected lines in MS Word

I wanted to count the lines of selected text in MS word document. I come up with code:
Sub Count_Lines()
ActiveDocument.ComputeStatistics (wdStatisticLines)
MsgBox ("The document contains " & NumLines & " Lines")
End sub
But it actually count the line of whole document.
Can anybody help, finding code which could count selected line only?
Just use Selection.Range object instead of ActiveDocument
Sub Count_Lines()
MsgBox ("The selection contains " & Selection.Range.ComputeStatistics(wdStatisticLines) & " Lines")
End Sub
Your code intends to show the value of NumLines in the message box. However, no value was assigned to that variable. In fact the variable itself was also not declared. The code below fills these two shortcomings and then works just fine. However, it will count all the lines in the document. If you just want the selected lines use the solution offered below.
Option Explicit
Sub Count_Lines()
Dim NumLines As Long
NumLines = ActiveDocument.ComputeStatistics(wdStatisticLines)
MsgBox ("The document contains " & NumLines & " Lines")
End Sub
NB. They say, Option Explicit "forces" you to declare variables which forces the reaction to resist. Compare the force with that of the traffic police who insist on your driving on the right side of the road. Using Option Explicit will not prolong your life but it will drastically reduce the amount of time you spend chasing stupid errors because you are too proud to let your computer point them out to you. Option Explicit was the tool that provided me with an immediate pointer to the source of the problem you are seeking help on here.

Detect type of non printing character in word vba

In word vba, I need to select entire line, and to be able to know which kind of non printing character it is (page break, section break, ... )
After various attempts not worth sharing, I have no idea how to do this.
Highlight the text you want to check and run this, it will print the ASCII value for each character in the selected text. You can use this value to see what character it is.
Sub MM()
For i = 1 To Selection.Characters.Count
Debug.Print Selection.Characters(i) & ":" & vbTab & Asc(Selection.Characters(i))
Next i
End Sub

InsertCrossReference Fails On Last Item

I think I have found a bug with the InsertCrossReference. If I use from code for the last list item in the document and there being nothing after that list item, it fails with error "Run-time error '4198' Command failed". If you do it manually, it all works. So you are thinking, I have written the code incorrectly, but this isn't the case. To make sure, i recorded a macro of inserting the cross reference and then ran the macro it recorded and the same error happens.
I have googled this problem and seen a couple of people raise it but a)they haven't pointed out that it only fails for the last list item and there being nothing in the document after that list item b)they haven't had any work-around replies.
I am using Word 2010 but I have also tried it on Word 2013 and the same thing happens.
If you want an example, if I set up the following:
This is my xref
Hello
Bye
where 1 and 2 are standard numbered lists and I have nothing in the document after "Bye" and then I run:
ActiveDocument.Range(16, 16).InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext, ReferenceItem:="2", InsertAsHyperlink _
:=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
Note having nothing after "Bye" is key. If you start a new line, with or without a list type, the code above will work
If anyone has any work-around for this, I'd appreciate it
You cannot insert anything AFTER the last paragraph mark in a document. I've had this issue in the past. My work around was to pragmatically add a paragraph mark in.
Something like the following:
Dim CRRange as Word.Range 'Make a range object to store where the cross reference will go.
set CRRange = ActiveDocument.Range(16,16)
if CRRange.end >= ActiveDocument.Range.End then 'Check to see if we reached the end of the document
ActiveDocument.Paragraphs.Add Range:=ActiveDocument.Range(ActiveDocument.Range.End -1, ActiveDocument.Range.End -1)
End If
CRRange.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext, ReferenceItem:="2", InsertAsHyperlink _
:=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

Adding text to a MS Word table in vb.net too slow

From this question and many other examples on the internet one can figure out how to automate the text addition to a MS Word table:
ProductTable.Cell(2, 1).Range.Text = "whatever text here"
But the problem is that the process is so slow that you can even see the lines being added one by one.
This is not a problem for a small application, but in my case I need to add about 6000 rows of text.
Is there any faster way to do this?
You can try, whether it is faster, if you first add the complete table content in CSV manner and then convert it to table with Range.ConvertToTable method.
E.g. in Word VBA:
With ActiveDocument
Set myRange = .Paragraphs(.Paragraphs.Count).Range
myRange.Text = _
"data11,data12,data13" & vbCrLf & _
"data21,data22,data23" & vbCrLf & _
"data31,data32,data33" & vbCrLf
myRange.ConvertToTable Separator:=wdSeparateByCommas 'not necessarily commas, depends on the Locale properties
End With
Greetimgs
Axel

VBA Formating on Characters greater than 255 using Characters.Font

I posted a question recently on how to format the first part of text from a input box and append it to existing text in a cell.
So for example if I have a cell with... this
23/08/2013: Hi there how are you today
24/08/2013: Customer is feeling good today
and I double click the cell, I get an input box to enter a comment. I take the comment.. add todays date to it in the VBA code and then append and format it to the existing code using this
If Target.Column = NOTES_COL Then 'Add a note
lngPos = Len(Target.Text)
strNote = InputBox(Prompt:="Enter Note", _
Title:="Notes", Default:="")
If (Len(Trim(strNote)) > 0) Then
If Target.Value = "" Then
Target.Font.Bold = False
newVal = Date & ": " & strNote
Else
newVal = Chr(10) & Date & ": " & strNote
End If
Target.Characters(Start:=lngPos + 1).Text = newVal
Target.Characters(Start:=lngPos + 1, Length:=11).Font.Bold = True
End If
End If
So basically this takes a comment... adds a date to the comment, and a new line and then appends it to the existing characters and formats the date Bold.
This all works fine until I go over 255 Characters, which the poster that helped me in the last solution warned about, but I thought it was trying to insert one comment > 255 and not the entire cell lenght.
How do I get around this... as I can add mulitple comments into a cell
regards Mick
A similar issue was discussed on SO here. According to that thread, the issue is that the InputBox method only allows a max of 255 characters. I've verified this myself.
That thread also suggested using a userform. That is the best option, especially considering entering over 255 characters in a one-line inputbox is probably setting up for frustration. How will you review what you've already typed, for example?
There are a few less polished options I can think of:
Have users enter comments directly in another cell - for example, select (and I hate using select!) a different cell, and let the user put whatever notes they desire there. Then run code to append that cell's value to your target cell.
Something even more crazy would be to call an outside program, such as notepad (or the Mac counterpart), save, and import that information in to append to the target cell.
All of these options are just work-arounds for the InputBox limit. Maybe there are some better alternatives someone more experienced can think of?
Update
Also, there seems to be an issue with using .Characters to add to anything over 255 characters long. I suspect this is because the .Characters method (not property) is set up to work on a textframe, not a cell value. A textframe is actually an object that works with shapes. It's interesting you're using it here on a range to edit its value - cool trick, courtesy of another question you've asked.
A solution would be to replace the cell value. You can then use .Characters as an object, to format portions of the cell value, as I think you've already tried to do in your other question.