Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting small paragraphs into a Word document using CommandButtons 1

Status
Not open for further replies.

PhilBell

Technical User
Dec 11, 2006
8
GB
I'm relatively new to VB, so my knowledge is very limited.

I'm attempting to use VB (6.3) to insert text into an existing Word document. The document is mail merge and the plan is to have a window pop-up on merge, with command buttons, clicking on one button will input a specific paragraph (relatively small in size) about half way through the document. I've been playing around with the InsertAfter and InsertBefore commands but I'm really not getting anywhere.
 
It would help if you posted some code.

InsertBefore, InsertAfter.....what? Those can be used for Selection, or a Range. Are you using them to do an InsertBefore or InsertAfter a bookmark (which is what I would recommend BTW)?

Sorry, but you need to give a bit more detail.

Gerry
My paintings and sculpture
 
I can input paragraphs at the very begining of the document using this code:-
Set myRange = ActiveDocument.Range(0, 0)
With myRange
.InsertParagraph
.InsertBefore "Your Text Here"
End With

I have a bookmark where I intend the paragraph(s) to go, but I have no idea how to tell VBA to use that bookmark (named "Tent") as the insertion point. I've tried:
Set myRange = ActiveDocument.Bookmarks("Tent")
With myRange
.InsertParagraph
.InsertAfter "Your Text Here"
End With

But there's quite blatently something wrong with it. I expect its something incredibly basic, I'm hoping it is so that when I've sorted it out, this post will give people something to laugh about on these forums :>

like I said I'm very new to this so please bear with me :)
 
Code:
Sub aTest()
With ActiveDocument

' Insert text
    .Bookmarks("Test").Range.Text = "Insert Text"

' delete the bookmark
    .Bookmarks("Test").Delete

End With
End Sub
 
Not to worry. No one is going to laugh at you.

I would like to point out that, at least up to version 2002, the following are equivalent.

Code:
ActiveDocument.Bookmarks("Test").Range.InsertAfter Text:="Insert text"

and:
Code:
ActiveDocument.Bookmarks("Test").Range.Text = "Insert text"

In both cases, the text is inserted AFTER the bookmark.

.Range.Text does NOT make the actual bookmark range that text.

There is a way to do so however.

This is only an issue if you wish to have the text actually BE bookmarked. That is, you can redo the action and replace the text with some other text, OR, retrieve the actual text.

Just to be clear:
Code:
Dim BookmarkText As String
ActiveDocument.Bookmarks("Test").Range.Text = "Insert text"
BookmarkText = ActiveDocument.Bookmarks("Test").Range.Text
In the code above the string variable BookmarkText would be "" - blank.

The following will truly put any given text into the actual range of a given bookmark.
Code:
Sub FillABookmark(strBM As String, strText As String)
Dim oRange As Word.Range
   Set oRange = ActiveDocument.Bookmarks(strBM).Range
   ActiveDocument.Bookmarks(strBM).Range.Text = strText
   With oRange
      .Collapse Direction:=wdCollapseEnd
      .MoveEnd Unit:=wdCharacter, Count:=Len(strText)
   End With
   ActiveDocument.Bookmarks.Add strBM, Range:=oRange
End Sub

It is a generic bookmark range filling procedure. It will put any text into the REAL range of a bookmark. You use it like:
Code:
Sub TestBM()
   Call FillABookmark("Test", "Insert text.")
End Sub
This will put the text "Insert text." into the actual range of the bookmark "Test".

Now:
Code:
Dim BookmarkText As String
BookmarkText = ActiveDocument.Bookmarks("Test").Range.Text
will make the string variable BookmarkText = "Insert text."

As for your code:
Code:
Set myRange = ActiveDocument.Bookmarks("Tent")
With myRange
    .InsertParagraph
    .InsertAfter "Your Text Here"
End With
You should actually be getting a Type Mismatch error.

Set myRange = ActiveDocument.Bookmarks("Tent") will not make myRange equal anything.

Set myRange = ActiveDocument.Bookmarks("Tent").Range will.

So, it depends. If you want to actually make the bookmark EQUAL to the text inserted, you will need to make sure the actual range is correctly used.

If you just want to insert after, then InsertAfter, or .Range.Text will do that.

Lastly, I notice you are using .InsertParagraph in your code. While certainly not incorrect, you can also do this using a VBA constant. Using your bookmark "Tent" (is that a typo???, but no matter...), if you want to make the actual bookmark range text = a paragraph, followed by "Your text here.", you can use FillABookmark like this - assuming you have already put the procedure FillABookmark into your code module(!!!):
Code:
Sub InsertMyText()
  Call FillABookmark("Tent", vbCrLf & "Your text here.")
End Sub
The vbCrLf is a paragraph mark.

The main reason for using a procedure that will actually make a bookmark range some text (rather than putting that text after), is if you ever want to get that text value out easily.

If text is actually IN the bookmark range, then:
Code:
ActiveDocument.Bookmarks([i]bookmake_name[/i]).Range.Text
will BE that text.

Here is an example of getting stuff from bookmarks.
Code:
Sub GetBookmarkStuff()
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File

Dim oDocBM As Bookmarks
Dim oBM As Bookmark

Dim strFolderPath As String
Dim BookmarkText() As String
Dim msg As String
Dim Finalmsg As String

Dim i As Integer
Dim var
strFolderPath = InputBox("Enter folder to process.")

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strFolderPath)

For Each fil In fld.Files
   i = 0
   ReDim BookmarkText(i)
   msg = ""
   Documents.Open FileName:=strFolderPath & _
      Application.PathSeparator & _
      fil.Name
   Finalmsg = Finalmsg & vbCrLf & ActiveDocument.Name
   Set oDocBM = ActiveDocument.Bookmarks
   For Each oBM In oDocBM
      ReDim Preserve BookmarkText(i)
      BookmarkText(i) = "Bookmark Name: " & oBM.Name _
         & vbTab & "Text:  " & oBM.Range.Text
      i = i + 1
   Next
   For var = 0 To UBound(BookmarkText())
      msg = msg & vbCrLf & BookmarkText(var)
   Next
   Finalmsg = Finalmsg & msg & vbCrLf
   Set oDocBM = Nothing
   ActiveDocument.Close wdDoNotSaveChanges
Next

Documents.Add
Selection.TypeText Text:="The following is a listing of " _
      & "documents with their Bookmarks and their text." & _
      vbCrLf & Finalmsg
End Sub
This is what it does.
[ol A][li]declares a bunch of variables and objects[/li][li]sets a string variable for a folder path entered from an inputbox[/li][li]sets FileSystemObject for folder assuming only valid files are in the folder (if not, this HAS to be error trapped!)[/li][li]process EACH file as follows[/li][ol i][li]clear counter and arrays for that file[/li][li]add current file name to FINAL message string[/li][li]set a Bookmark collection object[/li][li]for each bookmark in Collection grab its Name, and its text value, and add to an array[/li][li]build a current file bookmark message string from that array[/li][li]add current bookmark message string to FINAL message[/li][li]destroy bookmark Collection object[/li][li]close current file[/li][/ol][li]go to next file[/li][li]open a new document[/li][li]put in FINAL message listing each document name, with each bookmark name and text value for that document.[/li][/ol]

Before all the Excel people start laughing....

We can have literally hundreds of people on training (all over the country) in any given month. Each one has to send in a Word form with course name, cost, dates, assessment of course, where the course was taken, instructor name, assessment of the instructor blah blah blah blah.

I use a version of this to grab information out of all those Word documents (all input via a userform and placed into bookmarks), and collate that into an Excel file.

And there....my extended rant post for the week. An early start.

Gerry
My paintings and sculpture
 
Thanks for the incredibly informative post Gerry, very much appreciated :)



(Btw your artwork is great, particularly your sculptures)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top