PDA

View Full Version : inserting text in the middle of a template



dinotom
12-06-2010, 10:07 AM
I am a newbie to Word vba coding but am pretty good in excel
I am trying to read text from a file and import into a specific spot ina word template file. That spot can be found using find or after a certain paragrpah number. what is the proper way to do that.

lucas
12-06-2010, 10:51 AM
You don't say how you are retrieving the text from the text file but you should probably use a bookmark in the template file and insert it there.

dinotom
12-06-2010, 10:55 AM
How do Insert a bookmark in the template?

fumei
12-06-2010, 10:56 AM
Please clarify "template file".

Do you mean a document?

"Certain spot". This has to be defined. Specific spot often means a bookmark - which by definition IS a specific spot.

So, if you know the spot, put a bookmark there. Or, if you do NOT know th spot, but are looking for it (using Find), then when you find it, simply put the text there.

I take it you want to read text from a file and import into another document. Assuming that text is in a string variable (InsertThis), you go into the other document, find whatever it is you are searching for, collapse the Find range, and insert the text. Something like:

Dim InsertThis As String
Dim r As Range

InsertThis = however you are getting the text from the other file

Set r = ActiveDocument.Range
With r.Find
.Text = "FindThisString"
.Execute
If .Found = True Then
r.InsertAfter InsertThis
r.collapse
End If
End With

fumei
12-06-2010, 10:57 AM
How do you insert a bookmark? Go to where you want the bookmark, and use Insert > Bookmark.

dinotom
12-06-2010, 11:07 AM
This is the code I am using which errs on r.Find

It is run from within excel. It opens the .xltm template file replaces the text "TodaysDate" with today's date and now I want to insert text I read from a text file into the body of the template. ( ies, after the etx I am searching for "(281) 240-1787"

With objWord.ActiveDocument
Set myRange = .Range(Start:=0, End:=1000)
With myRange.Find
.ClearFormatting
.Text = "TodaysDate"
With .Replacement
.ClearFormatting
.Text = strDate
.Font.Italic = True
End With
.Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=True, _
MatchWholeWord:=True



End With
strTemp = GetText("X:\TestText.txt")
Set r = .Range
With r.Find
.Text = "(281) 241-1787"
.Execute
If .Found = True Then
r.InsertAfter strTemp
r.Collapse
End If
End With
End With

VBA Tags added to code

fumei
12-06-2010, 11:54 AM
Huh?

1. "It opens the .xltm template file replaces the text "TodaysDate"

And since you are working with an EXCEL file...what are you doing with Word????

2. It certainly should err!

Set r = .Range
With r.Find
Set r = WHAT.Range?

At best it would set r as an EXCEL range, which is not the same thing at all as a Word range!

3. Set myRange = .Range(Start:=0, End:=1000)
With myRange.Find

myRange in the above IS an Excel range. You can not use Word functionality to it.

dinotom
12-06-2010, 12:02 PM
I added a bookmark and now insert the text at the bookmark. Thank you for the suggestion

dinotom
12-06-2010, 12:03 PM
not true, it is a word range and functions correctly

fumei
12-06-2010, 12:26 PM
Oooops. I am wrong.

Set myRange = .Range(Start:=0, End:=1000)

I did not look carefully. Yes, if myRange was properly declared as Word.Range, then it is a word range. You did not show how you declared it, if you declared it at all (you do not show an Option Explicit).

I missed the numeric aspect of it. I assumed - wrongly - that I was seeing cell location.

Perhaps because Start:=0 End:=1000 seems rather odd to me. You are only checking the first 1000 characters? Strange, but OK.

dinotom
12-06-2010, 12:27 PM
Yes, I only showed part of the Sub, my apologies. The bookmark trick works nicely and Now I know the functionality so Ill use it more often.

fumei
12-06-2010, 01:46 PM
Just be aware that if you use the bookmark range to insert text, this deletes the bookmark.

If you want to be able to re-use the bookmark (say to put different text in at a different time), you need a macro like this:
Sub FillBM(strBM As String, strText As String)
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
End Sub
You then Call the Sub with the bookmark name, and the text to go inside it.

Sub Whatever()
Call FillBM("bookmarkName", "The text going into the bookmark")
End Sub
You can of course use variables.

Sub Yadda()
Dim Bm_Name As String
Dim TheText As String

Bm_Name = "Whatever"
TheText = ActiveDocument.Paragraphs(3).Range.Text

Call FillBM(Bm_Name, TheText)
End Sub
The bookmark "Whaever" would get the text of the third paragraph.

macropod
12-06-2010, 09:45 PM
You could also create some custom document properties for the code to populate, with corresponding DOCPROPERTY fields in the document, then use 'ActiveDocument.Fields.Update' to update the document with the new values.

fumei
12-08-2010, 10:31 AM
That's it Paul, expand the options! <grin>