Question Populating microsoft word template

Onamission71

Member
Joined
Jul 8, 2009
Messages
14
Programming Experience
Beginner
Hi, hope someone can help, I have a windows form with one textbox and one button (this is just until i get it working, then there will be more) basically I want to add the text press the button and this will open a Word template and populate a space in a sentence within that template.
code so far:-
Imports Word = Microsoft.Office.Interop.Word
Imports office = Microsoft.Office.Core
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim location As String
Dim oWord As Word.Application
Dim oDoc As Word.Document
location = TextBox1.Text

'Start Word and Open document template

oWord = CreateObject("Word.Application")
oWord.Visible = True

oDoc = oWord.Documents.Add("C:\test.dotx")

oDoc.Bookmarks("mybookmark").Range.Text = location


End Sub
End Class
This opens the template but where I have created the bookmark in the word document I get Error! bookmark not defined. Am I creating the bookmark wrong or is it my code? How should I create the bookmark.?
 
If this can't be done, can someone tell me how to find and replace text using vb.net in a word document, I can open and insert text within a document but not find and replace
 
Usually when I need any VB to access either word or excel the easier way to get the code is to create a macro in Word (or excel) manually do the action and then you go and check what is the code that was created and adapt wherever necessary.

the bookmarks works, I've did it before, I'm sure it's like MattP wrote.

I just did the find and replace macro now and found:
VB.NET:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Text"
        .Replacement.Text = "Replacement"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

and a bit of analyse you can see that what you really need is just the
VB.NET:
Selection.Find.Execute( parameters  )
the parameter list is quite big, you just check on word itself what you need.
 
Back
Top