I am exploring using word to "capture formatting" when converting to a text file.
The overall aim is to capture custom document structure and format when converting to a text file. i.e to generate a text file that says (BOLD) if the text if bold, or (UNDERLINED) if underlines etc.
Following previous questions, this generally works to generate text files capturing sentences as a while (i.e. not introducing unwanted line breaks) which can then be read using power query.
Ultimately this has proved useful but I have realised that a document's structure and formatting have valuable information that so far is not captured.
I only realised yesterday that it is actually possible using word to find and replace formatting features. Here in the image below, I have used word to recognise if the text is underlined and replace this with the text (HEADER) before it. The same can be done for presumably most formatting marks (e.g. Tab).
For the most part dynamically tag headers in the Text file which I can then use to create a function in power query to iterate over various similar documents and start separating the text into relevant sections.
This being said I have come across the end of cell marker
as shown here:
Initially, I thought it would be feasible to find and replace such markers with another character (|
), however, it seems that that formatting mark cannot be searched for in the above way.
This being said there is some hope with people seemingly doing similar things using VBA to get Chr(13)
which is apparently the value for this and recognising it.
I just want to create a script to tag each end of the cell marker with some character.
CodePudding user response:
Found this answer: https://answers.microsoft.com/en-us/msoffice/forum/all/add-character-before-end-of-cell-marker-in-word/2b7fe2c3-e96e-4d34-a887-b1ddf17d512f
'If you want to add the character to only the cell that contains the cursor, use this (see http://www.gmayor.com/installing_macro.htm if needed):
Sub AddToSelectedCell()
Dim cl As Cell
If Selection.Information(wdWithInTable) Then
Set cl = Selection.Cells(1)
cl.Range.Text = _
Left(cl.Range.Text, Len(cl.Range.Text) - 2) _
& "a" ' <= the character you want to add
Else
MsgBox "Put the cursor in the cell and rerun this macro."
End If
End Sub
'If you want to add the same character to the end of every cell in the table that contains the cursor, use this instead:
Sub AddToEveryCell()
Dim tbl As Table
Dim rw As Row
Dim cl As Cell
If Selection.Information(wdWithInTable) Then
Set tbl = Selection.Tables(1)
For Each rw In tbl.Rows
For Each cl In rw.Cells
cl.Range.Text = _
Left(cl.Range.Text, Len(cl.Range.Text) - 2) _
& "a" ' <= the character you want to add
Next cl
Next rw
Else
MsgBox "Put the cursor in the table and rerun this macro."
End If
End Sub