Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 25th, 2007, 04:02 PM
Registered User
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically add objects based on Word Bookmarks

I am trying to build a simple template engine that will do the following:

1) Open a word file that I have already inserted a series of bookmarks into.

2) Read through the file and count up the # of bookmarks.

3) Dynamically add to the main panel a series of labels and text boxes named after each bookmark found in the document.

4) Use the input from the text boxes to fill out the template (replace the bookmark text with the text from the text box.

I can open the file, read it, count the bookmarks, and get their names. I even understand how to replace the text, but I am having trouble creating the labels and boxes and adding them to the panel. Below is my code:
Public Class Form1

    Private Sub btnFindFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindFile.Click
        With OpenFileDialog1
            If Windows.Forms.DialogResult.OK Then
                Dim theFileName As String = .FileName
                Dim objWord As Word.Application
                objWord = New Word.Application
                objWord.Visible = True
                Dim docToChange As Word.Document
                docToChange = objWord.Documents.Open(theFileName)
                Dim bookmarks As Word.Bookmarks = docToChange.Bookmarks
                Dim countBookmarks As Integer = bookmarks.Count
                Dim counter As Integer = 1
                Dim textForDisplay As String
                While counter <= countBookmarks
                    textForDisplay = docToChange.Bookmarks(counter).Name
                    counter = counter + 1
                    Dim newLabel As New Label
                    newLabel.Name = textForDisplay
                    newLabel.Visible = True
                    newLabel.Text = textForDisplay
                End While
            End If
        End With
    End Sub
End Class
Thank you for all your help. - Adam
Old February 26th, 2007, 02:20 AM
Authorized User
Join Date: Sep 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra

Visit http://support.microsoft.com/kb/190670/en-us

Old February 27th, 2007, 03:57 AM
Registered User
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks for the tip, but it still doesn't seem to offer any help. A lot of the code isn't even supported in VB 2005. Does anyone know of nay other online tutorial I could look at given the specs I outlined above. I usually do web stuff with php, and I know it would be so easy in PHP, but I need to work with Word files. Thanks for the help - Adam

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form and bookmarks in Word paul20091968 Word VBA 0 May 16th, 2006 09:09 AM
Automate the process of creation bookmarks in word manish.sharma04 BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 March 1st, 2006 10:09 AM
add a image as your 'avatar' for bookmarks crmpicco HTML Code Clinic 4 September 30th, 2005 03:51 AM
Replacing Word Bookmarks from VB pavel Pro VB 6 0 May 3rd, 2004 09:20 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.