?
Solved

Word VBA: Code not working properly

Posted on 2008-10-16
2
Medium Priority
?
835 Views
Last Modified: 2013-12-27
Could someone please check my code and correct my mistakes for me please and tell me what i'm doing wrong. My code is meant to go through a word doc and go to bookmarks. For example when i click the first Bookmark button its meant to go to first bookmark and the bookmark should appear in the text box. The same should happen for To go to button, next, previous and last bookmark button. but as my textbox is not showing my bookmarks i don't know if my code is wrking properly or not. . please. I also have attached the exercise file to make sense of what is actually required by my code. Please check it and correct me in 3-4 fours time if you can.

Thankyou

Option Explicit
'current document, bookmark, bookmark position and count of bookmarks
Dim Doc As Document
Dim bkMrk As bookmark
Dim intPosition As Integer
Dim lngLast As Long
Private Sub userform_initalize()
'Initalise globals
intPosition = 1
Sets Doc = ActiveDocument
lngLast = ActiveDocument.Bookmarks.Count
'Hide and disable update buttons
Cancel.Visible = False
Update.Visible = False
Cancel.Enabled = False
Update.Enabled = False
'Common display form rtn
UpdateForm
End Sub
 
 
Private Sub AddBkMrk_Click()
'Add bookmark
'Find the string
With Selection.Find
.ClearFormatting: .Text = InputBox("Enter the text")
.Forward = True: .Wrap = wdFindContinue: .Execute
End With
'calculate the line number where the string is found and place the cursor two lines further down
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, _
Count:=Selection.Information(10) + 2, Name:=""
'add a bookmark named MyBookmark
ActiveDocument.Bookmarks.Add Name:="MyBookmark", Range:=Selection.Range
'Prepare textbox for input
TextBox1.Text = ""
TextBox1.SetFocus
 
End Sub
 
Private Sub FirstBkMrk_Click()
'Point to the first bookmark
intPosition = 1
Selection.GoTo What:=wdGoToBookmark, Which:=wdGoToNext, Name:="Bookmark2"
TextBox1.Text = ActiveDocument.Bookmarks("Bookmark2").Range
 
End Sub
 
 
Private Sub LastBkMrk_Click()
'Point to last bookmark
lngLast = ActiveDocument.Bookmarks.Count
intPosition = lngLast
Selection.GoTo What:=wdGoToBookmark, Which:=wdGoToLast, Name:="Bookmark5"
TextBox1.Text = ActiveDocument.Bookmarks("Bookmark5").Range
 
End Sub
 
Private Sub NextBkMrk_Click()
'Point to the next record and update
lngLast = ActiveDocument.Bookmarks.Count
If intPosition >= ActiveDocument.Bookmarks.Count Then
   intPosition = intPosition + 1
   intPosition = lngLast
End If
 
End Sub
Private Sub PreviousBkMrk_Click()
'Point to the Previous record and update
 
If intPosition > 1 Then
   intPosition = intPosition - 1
   Else
   intPosition = 1
End If
ShowHidden = False
TextBox1.SetFocus
 
End Sub
Private Sub DeleteBkMrk_Click()
  Dim rng As Range
    
Set rng = ActiveDocument.Bookmarks("incl1").Range
    rng.Collapse wdCollapseEnd 'just in case the bookmark isn't empty
    rng.End = ActiveDocument.Bookmarks("NotIncl1").Range.Start
    rng.Text = ""
ActiveDocument.Bookmarks("incl1").Delete
 
End Sub
 
 
Private Sub GotoBkMrk_Click()
'Point to the selected bookmark
Selection.GoTo = InputBox("Enter bookmark: Name or Number")
If ActiveDocument.Bookmarks.Exists("Bookmark3") = True Then
   'Do something, i.e.,:
   ActiveDocument.Bookmarks("Bookmark3").Range.Select
   End If
'Update the form
Update.Enabled = True
 
End Sub
 
Private Sub Cancel_Click()
'Cancel the Update
'Disable and hide the buttons and displasy the form
Cancel.Visible = False
Update.Visible = False
Cancel.Enabled = False
Update.Enabled = False
'Update the form
Update.Enabled = True
End Sub
Private Sub OK_Click()
'Apply the changes to bookmark
'Update the form
Update.Enabled = True
Unload UserForm1
UserForm1.Hide
End Sub
Private Sub UpdateForm()
ShowBookmarks = True
'Update current count and bookmark
lngLast = Doc.Bookmarks.Count
Set bmCurrent = ActiveDocument.Bookmarks(intPosition)
'Display the current Bookmark's position, Name and text
Label1.Caption = bmCurrent.Name
Label2.Caption = intPosition
TextBox1.Text = bmCurrent.Range.Text
FirstBkMrk.Enabled = True
PreviousBkMrk.Enabled = True
NextBkMrk.Enabled = True
LastMkMrk.Enabled = True
 
End Sub
 
 
 
Private Sub UserForm_Click()
 
End Sub

Open in new window

Tutorial1E0-1-.doc
0
Comment
Question by:Anciena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
rachitkohli earned 200 total points
ID: 23041357
I have done some changes in your macro, in First Bookmark, Last BookMark & GoTo bookmark..
You can refer the same, & am sure it should help you to extend it further the way you want..
Tutorial1E0-1--1.doc
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question