PDA

View Full Version : Impoting paragraph from another Word document using VBA



mawallace
06-03-2012, 01:51 AM
I have created a userform in a Macro

If a user answers Yes I want to have a paragraph which is in another Word document imported into my Word document at a certain point.

I have played with VBA codes and got no where.

Could you help?

gmaxey
06-03-2012, 06:32 AM
You need to have the path of the other Word document and a defined range for the text you want (e.g., table cell, content control, bookmark, etc.). In your active document you need a target for the text. Something like this:

Sub Demo()
InsertFileRange "bmTarget", "bmSource"
End Sub
Sub InsertFileRange(ByRef strBMTarget As String, strBMSource As String)
'Identify the file name and path of the data source
Const pDataFile As String = "D:\\Data Stores\\IncludeText Tip\\IncludeTextDataStore.docx"
Dim oRng As Word.Range
Dim oRngFileRangeEnd As Word.Range
'Set a target range for the text to insert. This is the bookmark "Narrative" range.
Set oRng = ActiveDocument.Bookmarks(strBMTarget).Range
Application.ScreenUpdating = False
'Clear target range of any previous content. This deletes the bookmark.
If Len(oRng.Text) > 0 Then oRng.Delete
'When you use InsertFile method you have to use a little Word trickery to determine the range of the inserted text.
'Anchor range start
oRng.Collapse wdCollapseStart
'Create a duplicate range
Set oRngFileRangeEnd = oRng.Duplicate
'Add a temporary character to the duplicate range
oRngFileRangeEnd.InsertAfter "*"
'Insert the content
oRng.InsertFile pDataFile, strBMSource 'Add ", , True" to link to the source text.
'Remove the temporary character from the duplicate range.
oRngFileRangeEnd.Characters.Last.Delete
'Anchor range end
oRng.End = oRngFileRangeEnd.End
'Re-create the bookmark
ActiveDocument.Bookmarks.Add strBMTarget, oRng
Application.ScreenUpdating = True
lbl_Exit:
Exit Sub
End Sub

mawallace
06-03-2012, 10:27 AM
I have been playing and using the .InsertFile(FileName, Range,) command will do the tirck - but I am trying to work out how to define the range bit now!!

If the start of this is marked with bookmark whicj is, say "bookmark one" and the end "bookmark two" how would this work

i.e insert text from file name xyz.doc from beginning of bookmaek one and to bookmark two!

gmaxey
06-03-2012, 06:50 PM
It wouldn't work. You need to select all of the text in the source document and assign it a single bookmark (e.g, bmSourceText). You need to define a point in your target document where the text needs to go and bookmark it (e.g., bmTarget). Then pass the source and target bookmarks as arguments to the procedure I provided.

mawallace
06-04-2012, 12:25 AM
How about if I then want to import the next four paragraphs starting from a particular bookmark in the source document. Would that work?

How would I script that?

fumei
06-04-2012, 01:36 AM
The easiest way is to make WHATEVER text you want a single bookmark. That is what bookmarks are for, marking text. So if you have one bookmark already - "a particular bookmark in the source document" - make it larger to encompass the text you want.

You can define a range for essentially whatever you want. So yes, you could start with an existing bookmark, and then include the next four paragraphs. But again, if you already have a bookmark, why not make that bookmark the text you want.

It would help if you clearly stated what you want to do. Will it always be the same chunk of text? If so, then bookmark it - all of it.

Is it going to dynamic, sometimes this chunk, sometimes that chunk? This would somewhat different code, and would require some input that will define the chunk.

So far, the assumption appears to be that the document with the chunk you want is not open. Will that always be the case?

Is this a one-off, or something you will use often? And again, if you are going to be using this often is the chunk you want the SAME chunk?

Would perhaps an INCLUDETEXT field be the way to go? That could make things easier.

fumei
06-04-2012, 01:51 AM
It is important to understand that while all bookmarks are ranges, what you USE them for can be essentially one of two things (or both).

A bookmark can be "empty" and is used as a pointer to a location.

A bookmark can have content, and is used to manipulate that content. Copy it. Move it. Change format. Whatever.

So if you want to get content from a document, and put it at a location....

In the document you want to get content from (GetFrom.doc) there is a bookmark named MyInclude. This content can be one paragraph, four paragraphs, 16 pages, a table.....whatever. It must be contiguous though.

In the document you have the userform in, you have a bookmark (PutHere) that marks the location you want to put the contents of Myinclude.
Sub WithRangeDirectly()
ActiveDocument.Bookmarks("PutHere").Range.Fields.Add _
Range:=ActiveDocument.Bookmarks("PutHere").Range, _
Type:=wdFieldEmpty, _
Text:="INCLUDETEXT ""C:\\Blah\\Whatever\\GetFrom"" MyInclude", _
PreserveFormatting:=True
End Sub

The location bookmark PutHere has a field added to its range, with the contents of the bookmark Myinclude that is in the file C:\Blah\Whatever\GetFrom.doc

It is VERY important that you use the correct syntax for the field text, done like this within a procedure. When you use the GUI (the dialogs) Insert > Field you use "normal" syntax in the path - single slashes. You can see later that Word changes the syntax. You MUST use the final syntax when you insert an empty field and add the text content of the field with VBA.

macropod
06-05-2012, 05:32 AM
Cross-posted at: http://www.msofficeforums.com/word-vba/13154-impoting-paragraph-another-word-document-using-vba.html
http://answers.microsoft.com/en-us/office/forum/office_2010-customize/selecting-a-range-of-text-using-bookmarks-in-vba/97341bda-7053-4ac0-9d7a-bcbe4d9cb977
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184