PDA

View Full Version : Modifying Word table cell text without loosing footnotes/endnotes and formatting



ybadihi
08-15-2016, 07:46 AM
Hi all,

Is there a way to modify a table cell text without loosing the footnotes?

Thanks

gmaxey
08-15-2016, 11:33 AM
Yes. You can put the cursor in the cell and modify the text and you can modify parts of the range with VBA. What are you trying to do

ybadihi
08-15-2016, 11:45 PM
Hi Greg,

I'm trying to change the text in the cell. I need to delete some spaces in special cases. I manage to do so using this statement:
oCell.Range.Text = Replace(sCellText, arrFromStrings(i), arrToStrings(i))
oCell.Range is the cell. It is getting the new string without the spaces.
sCellText is the original cell text.
arrFromStrings(i) is the substring that contains the spaces that need to be removed.
arrToStrings(i) is the substring without the spaces.

The text is being replaced perfectly but if the cell had a footnote attached to some word, it is getting lost. I read somewhere that it's a known problem that if you use macro to change a cell - you lose the footnotes, endnotes and any formatting you had in this cell. I hoped there is a solution. How would you do it? Thanks.

ybadihi
08-15-2016, 11:50 PM
sorry for the typo. it should be "losing", not "loosing".

gmayor
08-16-2016, 02:08 AM
Without knowing what is in your text strings or the cell it is difficult to answer, but the following will not delete any notes that are not part of the found/replaced string.


Dim oCell As Cell
Dim oRng As Range
Set oCell = ActiveDocument.Tables(1).Cell(1, 1)
Set oRng = oCell.Range
oRng.End = oRng.End - 1
With oRng.Find
Do While .Execute(FindText:="Text to find")
oRng.Text = "Replacement text"
oRng.Collapse 0
Loop
End With

ybadihi
08-16-2016, 02:30 AM
Thanks, Greg! I will try it.

gmaxey
08-16-2016, 04:46 AM
That was Graham and we are often confused ;-)

Grahams, code can be modified as follows to process the whole cell, table, or document in one run as follows:


Sub ScratchMacro()
'With ActiveDocument.Tables(1).Cell(1, 1)
'With ActiveDocument.Tables(1)
With ActiveDocument
.Range.Find.Execute FindText:="is", ReplaceWith:="was", _
Forward:=True, Wrap:=wdFindContinue, Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub

ybadihi
08-16-2016, 05:31 AM
Thanks a lot, Greg and Graham. I'll try the other one too.

ybadihi
08-16-2016, 06:05 AM
I think you did it, Greg. Thank you very much! Thank you all.
I will not mark the case as solved yet since I want to do few more tests before but I'm very optimistic now.

ybadihi
08-18-2016, 12:42 AM
Case soled.
Thanks again for all the help. :bow: