TBR
05-04-2022, 03:37 AM
Hello,
Context: I have set up a macro on excel that creates a word file using a word template file dotx. The .docx created contains the Bookmarks I've set up in the Word template file.
The user of the excel file will select the content he wants to display in the word document. But the .docx that is created contains all the bookmarks even those that the user did not select in Excel. The bookmarks remain visible but are not necessary.
The .docx file created contains 50 pages and for the moment the user has to delete each page + bookmark by hand so that the word document is clean.
I have 1 bookmark per page.
Need: I would like to find a way to delete the pages containing the unused bookmarks to get a clean document without even having to manually edit the content.
After several unsuccessful attempts, I am open to any idea.
I have listed on an Excel tab the list of bookmarks and according to the user's choice. (either 0 or 1).
If 0 => Delete the page where the Bookmark is in Column A.
If 1 => OK
Bookmarks
Valid? 1/0
FS1
1
FS2
0
OVER1
1
OVER2
1
OVER3
1
OVER4
1
OVER5
0
OVER10
1
NF1
1
...
...
Sample of the code :
'Trigger dotx wordfile
With wdapp
.Visible = True
.Activate
.Documents.Add "C:\Users\" & user & "\HH\QT\testwordcopy.dotx"
Application.CutCopyMode = False
'Copy Financial Summary
Sheets("Print_Content1").Select
ActiveSheet.Range("D5:H26").Copy
.Selection.Goto wdGoToBookmark, , , "FS1"
.Selection.Paste
'Copy Overview page1
Sheets("Print_Content2").Select
ActiveSheet.Range("A2:B17").Copy
.Selection.Goto wdGoToBookmark, , , "OVER1"
.Selection.Paste
End With
'----- THIS PART BELOW IS NOT WORKING EVEN WITHOUT A CONDITIONNAL STATEMENT------
'delete the unused word page
With wDoc
.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Name:=3 'Page number
.Bookmarks("FS2").Select
With Selection
.Delete
End With
End With
____________________
Hope it makes sense... As I said above, If you have a better idea, to add directly a vba code in my dotx file, change the way I'm transferring the datas,... I'm all ears !
Thanks a lot
Thomas
Context: I have set up a macro on excel that creates a word file using a word template file dotx. The .docx created contains the Bookmarks I've set up in the Word template file.
The user of the excel file will select the content he wants to display in the word document. But the .docx that is created contains all the bookmarks even those that the user did not select in Excel. The bookmarks remain visible but are not necessary.
The .docx file created contains 50 pages and for the moment the user has to delete each page + bookmark by hand so that the word document is clean.
I have 1 bookmark per page.
Need: I would like to find a way to delete the pages containing the unused bookmarks to get a clean document without even having to manually edit the content.
After several unsuccessful attempts, I am open to any idea.
I have listed on an Excel tab the list of bookmarks and according to the user's choice. (either 0 or 1).
If 0 => Delete the page where the Bookmark is in Column A.
If 1 => OK
Bookmarks
Valid? 1/0
FS1
1
FS2
0
OVER1
1
OVER2
1
OVER3
1
OVER4
1
OVER5
0
OVER10
1
NF1
1
...
...
Sample of the code :
'Trigger dotx wordfile
With wdapp
.Visible = True
.Activate
.Documents.Add "C:\Users\" & user & "\HH\QT\testwordcopy.dotx"
Application.CutCopyMode = False
'Copy Financial Summary
Sheets("Print_Content1").Select
ActiveSheet.Range("D5:H26").Copy
.Selection.Goto wdGoToBookmark, , , "FS1"
.Selection.Paste
'Copy Overview page1
Sheets("Print_Content2").Select
ActiveSheet.Range("A2:B17").Copy
.Selection.Goto wdGoToBookmark, , , "OVER1"
.Selection.Paste
End With
'----- THIS PART BELOW IS NOT WORKING EVEN WITHOUT A CONDITIONNAL STATEMENT------
'delete the unused word page
With wDoc
.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Name:=3 'Page number
.Bookmarks("FS2").Select
With Selection
.Delete
End With
End With
____________________
Hope it makes sense... As I said above, If you have a better idea, to add directly a vba code in my dotx file, change the way I'm transferring the datas,... I'm all ears !
Thanks a lot
Thomas