PDA

View Full Version : [SOLVED:] Insetfile on enclosed bookmark



Paulus123
03-05-2014, 02:10 AM
Hello,

I have created a Userform with a listbox to select multiple values. What i want is to insert a file at a bookmark if i select a value. This works but now the bookmark is a placemarker, but i rather have a enclosed bookmark so if i call the userform again and change the value it will change text that is inserted.


Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks("Bookmark").Range
If Listbox.Text = "one" Then
BMRange.InsertFile FileName:= _
"c:\text1.docx"
ElseIf Listbox.Text = "two" Then
BMRange.InsertFile FileName:= _
"c:\text2.docx"


Could someone help me?

Thanks in advance.

gmaxey
03-05-2014, 08:51 AM
I do it this way:


Sub ScratchMacro()
InsertWholeFileInBookmarkRange "d:\test.docm", "DocTarget"
End Sub
Sub InsertWholeFileInBookmarkRange(strFileName As String, strBookmark As String)
Dim oRng As Word.Range
Dim oRngFileRangeEnd As Word.Range
Set oRng = ActiveDocument.Bookmarks(strBookmark).Range
'Clear target range to ensure bookmark range is empty of any previous content.
oRng.Delete
'Anchor range start
oRng.Collapse wdCollapseStart
'Create duplicate range
Set oRngFileRangeEnd = oRng.Duplicate
'Add temporary character to duplicate range
oRngFileRangeEnd.InsertAfter "*"
'Insert the file
oRng.InsertFile strFileName
'Remove character from end of duplicate range.
oRngFileRangeEnd.Characters.Last.Delete
'Anchor range end
oRng.End = oRngFileRangeEnd.End
'Re-create the bookmark
ActiveDocument.Bookmarks.Add strBookmark, oRng
Application.ScreenUpdating = True
End Sub

Paulus123
03-06-2014, 12:58 AM
Great! Thank you very much! you fixed it for me!

snb
03-06-2014, 02:32 AM
Why don't you use the combination of the includetext field and a documentvariable field ?

{IncludeText {docvariable filename}}

The only thing you have to do in VBA is:


activedocument.variables("filename")="c:\text" & iif(If Listbox.Text = "one",1,2) &".docx"
activedocument.fields.update

You can change the docvariable's value as often as you like without having to change anything in the document.

macropod
03-06-2014, 03:19 AM
Why don't you use the combination of the includetext field and a documentvariable field ?
Maybe because the OP wants to insert the document, not just a link to it, and wants the users to be able to insert a different document if they then decide to do so.