![]() |
#1
|
|||
|
|||
![]()
Hi,
I am reaching out regarding an issue with exporting Confluence content to MS Word. The Confluence exporter does not appear to support transferring more than one table header row. To work around this, I have developed a VBA macro that scans for rows marked with the <HDR> tags in the first cell of a table row (in Confluence) and converts any such rows into header rows. In addition of marking them as header rows, the macro also removes the <HDR> tag from MS-Word content. This works perfectly fine when .docm is opened and autosaved as .docx—the header rows are retained and behave as expected. However, once the .docx file is closed and reopened, all the header rows derived from <HDR> tags revert to normal rows and lose their header formatting. With your expertise, could you please advise if there’s a way to make these header rows persist when docx file is closed and reopened? I would appreciate any guidance. Thanks! Here is the sub: Code:
Private Sub AddHeaderRows_Tables() Dim table_instance As Table Dim cell_instance As Cell Dim txt As String Dim updated_txt As String Dim max_header_row As Long Dim target_row_index_for_header As Long Dim start_position As Long Dim end_position As Long Dim rng As Range If ActiveDocument.Tables.Count = 0 Then Exit Sub For Each table_instance In ActiveDocument.Tables ' find the last row in cell (x,1) that has tag + remove it max_header_row = 0 'for farthest tag entry in rows For Each cell_instance In table_instance.Range.Cells 'scanning all cells, but action on 1st col If cell_instance.ColumnIndex = 1 Then txt = cell_instance.Range.Text If Len(txt) >= 2 Then txt = Left$(txt, Len(txt) - 2) ' delete end-of-cell ms-word markers If InStr(1, txt, "<HDR>", vbTextCompare) > 0 Then updated_txt = Replace$(txt, "<HDR>", "", , , vbTextCompare) 'if tag present, updated text will not hv it cell_instance.Range.Text = updated_txt & Chr(13) & Chr(7) ' append end-of-cell ms-word markers If cell_instance.RowIndex > max_header_row Then max_header_row = cell_instance.RowIndex ' this will reset row index from 0 to RowIndex, and will increase till last tag End If End If End If Next cell_instance ' target_row_index_for_header holds rowIndex till where rows will be converted as header rows If max_header_row > 0 Then target_row_index_for_header = max_header_row Else target_row_index_for_header = 1 'in case if no tag found, atleast mark row no. 1 to be made header End If ' Setting range >> 1..target_row_index_for_header start_position = table_instance.Cell(1, 1).Range.Start end_position = start_position For Each cell_instance In table_instance.Range.Cells If cell_instance.RowIndex <= target_row_index_for_header Then If cell_instance.Range.End > end_position Then end_position = cell_instance.Range.End 'setting end position to row will farthest tag entry End If End If Next cell_instance Set rng = ActiveDocument.Range(Start:=start_position, End:=end_position) ' table_instance.Cell(1, 1).Range.Select ' table_instance.ApplyStyleHeadingRows = False ' applying header format On Error Resume Next ' rng.Rows.HeadingFormat = wdToggle rng.Rows.HeadingFormat = True rng.Font.Bold = True rng.ParagraphFormat.Alignment = wdAlignParagraphCenter On Error GoTo 0 Next table_instance End Sub |
#2
|
||||
|
||||
![]()
It sounds like the issue is not necessarily with your macro. If the macro finishes and the table is formatted the way you wanted it - and it persists while the document is open then it sounds OK.
If the problem then appears only after Close/Open then you may have other macros that are running which 'standardise' the tables. Can you have a look at your templates and verify that there aren't automacros which might be causing your table local formatting to change? I would also question what you mean by losing the heading format. Your macro is applying a table .Rows.HeadingFormat as well as local font.bold and paragraphformat.alignment settings. Are all three of these settings disappearing? Would applying a paragraph style persist better for the last two?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
Tags |
table headers, table macro condition, word vba macro |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Issue with logo proportions after insertion in header | hank1234 | Word VBA | 8 | 10-31-2023 10:54 PM |
[Word 97] Different First Page Header Issue | dw85745 | Word | 2 | 04-01-2023 11:56 AM |
header format issue | mfitness92 | Word | 3 | 07-31-2022 01:03 AM |
Word Macro to delete table row and table header | NorthSuffolk | Word VBA | 6 | 10-11-2016 05:04 AM |
Macro to add title in header is missing text once macro is run | shawnee24 | Excel Programming | 1 | 05-27-2015 11:50 PM |