PDA

View Full Version : Word VBA - Replace Bookmark that contains a Table



GRCNC
03-29-2017, 03:09 PM
Hi - Hopefully an easy question to answer... My Word document contains several Bookmarks as well as a UF. One of the purposes of the UF is to allow the end user to replace Bookmarks. I've got a combobox, user selects the Bookmark to be replaced and my macro opens up the appropriate file, and using the VBA command "Selection.InsertFile", copies the contents of the file and should replace the Bookmark in the active file with these contents from the newly opened file. The macro works as expected on all of the Bookmarks except 1. This 1 Bookmark contains a Table and when the macro tries to replace it, I'm getting the following error:

Runtime error '5260'
You cannot insert this selection into a table.

The file that I'm inserting has a table at the start of the document. I ended up putting a blank row before the table thinking that might help, but it didn't.

Anybody else come across this issue and find a solution?

Here is the code I'm using




Dim strPath As String
strPath = "C:\Users\jsmith\Documents\Word\Files" 'declare the path as a string variable


If ActiveDocument.Bookmarks.Exists("BK_Tires") = True Then
ActiveDocument.Bookmarks("BK_Tires").Select


Selection.InsertFile FileName:=strPath & "Accounting%20Folder%202017/File%20Summary%20Intro__1.docx"
Else
MsgBox "Can't replace Intro section. Bookmark does not exist."
End If

macropod
03-29-2017, 03:45 PM
Try:

Dim strPath As String
strPath = "C:\Users\" & Environ("UserName") & "\Documents\Word\Files\" 'declare the path as a string variable
With ActiveDocument
If .Bookmarks.Exists("BK_Tires") = True Then
With .Bookmarks("BK_Tires").Range
.Delete
.InsertFile FileName:=strPath & "Accounting%20Folder%202017/File%20Summary%20Intro__1.docx"
End With
Else
MsgBox "Can't replace Intro section. Bookmark does not exist."
End If
End With

GRCNC
03-30-2017, 12:22 PM
Thanks for the help Paul. That did eliminate the error and it brought over the new bookmark file, but the format is off. Instead of 9 pages, the bookmark now comes over into the file as 11 pages. For example, page 1 is now split with 1/2 of it being on page 1 and the other 1/2 on page 2.

I thought that I had read somewhere that with using .InsertFile, the inserted file / text would take on the format of the main document, so I was surprised to see it come over differently.

Is there any line of code that I could use to make sure the formatting remains?

thanks!
GRCNC

macropod
03-30-2017, 02:25 PM
Thanks for the help Paul. That did eliminate the error and it brought over the new bookmark file, but the format is off. Instead of 9 pages, the bookmark now comes over into the file as 11 pages. For example, page 1 is now split with 1/2 of it being on page 1 and the other 1/2 on page 2.

I thought that I had read somewhere that with using .InsertFile, the inserted file / text would take on the format of the main document, so I was surprised to see it come over differently.
What you're seeing has nothing to do with the code I posted, but with conflicting Style definitions and/or page layouts between the two documents - most likely the former, with a Style in the destination document having the 'page break before' attribute set, but the same Style isn't defined or used that way in the source document. Using .InsertFile, the inserted content takes on the Style definitions of the destination document.