How to remove the error from macro-code?

A

avkokin

Hello dear experts.
If you please help me with my macro-code (into template below). If I
work with it into my template then all OK. But when I am placing it
into folder "StartUp", launch Word and try it to use I'm get the error
"Object variable or With block variable not set". However all these
operators is exist into code. I not understand how to remove this
error.
The template with macro-code is here (bm_engl.dot): http://www.box.net/shared/8drmjis2az

Thank you very much!
 
J

Jan Hyde (VB MVP)

avkokin <[email protected]>'s wild thoughts were released on
Tue, 21 Oct 2008 04:18:23 -0700 (PDT) bearing the following
fruit:
Hello dear experts.
If you please help me with my macro-code (into template below). If I
work with it into my template then all OK. But when I am placing it
into folder "StartUp", launch Word and try it to use I'm get the error
"Object variable or With block variable not set". However all these
operators is exist into code. I not understand how to remove this
error.
The template with macro-code is here (bm_engl.dot): http://www.box.net/shared/8drmjis2az

Thank you very much!

Can you post the macro here, I like many others are not too
keen on clicking unknown links.
 
H

Helmut Weber

Hi Anton,

without understanding all of your question,
as your little project seems to be quite complex
and I'm far from understanding all:

There is one line:
If ActiveWindow.View.ShowBookmarks Then
change it to:
If ActiveDocument.ActiveWindow.View.ShowBookmarks Then

msgbox activewindow.caption
should work, as according to the help,
the application has an activewindow property,
but it doesn't work here and now.

There is another line in your code:
cb.ShowPopup .Left + 25, .Top + .Height - 24
Change it to:
cb.ShowPopup cb.Left + 25, cb.Top + cb.Height - 24

I'm sorry, I don't know how that could work
without the corrections,
wherever the code may be located.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

avkokin

Hello Jan.
Yes, that is the code (below).
I will explain that I want. This macro(template) show menu with tools
for work with bookmarks. See screen: http://img.photobucket.com/albums/v190/baston/bm.gif
But first you should create toolbar "bkm" and place it to left
position and add to it button of macro "mymenu".
The code:
Option Explicit
Dim cb As CommandBar
Dim cbbBM As CommandBarButton
Dim cbbSrvs As CommandBarPopup
Dim cbbShowBM As CommandBarButton 'variable - "Show bookmark"
Dim cbbSortBM As CommandBarButton '
Dim cbbRemoveBM As CommandBarPopup '
Dim cbbDelAllBM As CommandBarButton '
Dim cbbDelBM As CommandBarButton '
Dim cbbListBM As CommandBarButton '

Dim bm As Bookmark '
Dim bBkmOnRange As Boolean '
Dim bShowHS As Boolean '
Dim bFirstItem As Boolean '
Dim bFirstItemCur As Boolean '
Dim i As Long '

Sub mymenu()
'On Error Resume Next
'Check whether open documents
If Application.Documents.Count = 0 Then
MsgBox "Not open document"
Else

Set cb = CommandBars.Add("bmPopup", msoBarPopup)

bShowHS = ActiveDocument.Bookmarks.ShowHidden
ActiveDocument.Bookmarks.ShowHidden = False
'for separator of deleting bookmarks
bFirstItem = True
'for separator of current bookmarks
bFirstItemCur = True

'create item menu "Service" with popup menu
Set cbbSrvs = cb.Controls.Add(msoControlPopup)
With cbbSrvs
.Caption = "Service"
'create item "Show bookmark"
Set cbbShowBM = cbbSrvs.Controls.Add(msoControlButton)
With cbbShowBM
If ActiveWindow.View.ShowBookmarks Then
.FaceId = 1664
.Caption = "Hide bookmark"
Else
.FaceId = 0
.Caption = "Show bookmark"
End If
.OnAction = "bmShow"
End With
'create item "Sort by location"
Set cbbSortBM = cbbSrvs.Controls.Add(msoControlButton)
If ActiveDocument.Bookmarks.Count = 0 Then
cbbSortBM.Enabled = False
End If
With cbbSortBM
.Caption = IIf(bBkmOnRange, "Sort by location", "Sort by
alphabetically")
.OnAction = "bmSortLoc"
End With
End With

'create item "Remove bookmarks"
Set cbbRemoveBM = cb.Controls.Add(msoControlPopup)
If ActiveDocument.Bookmarks.Count = 0 Then
cbbRemoveBM.Enabled = False
End If
With cbbRemoveBM
.Caption = "Remove bookmarks"
.BeginGroup = True
'create item "Delete all bookmarks"
Set cbbDelAllBM = cbbRemoveBM.Controls.Add(msoControlButton)
With cbbDelAllBM
.Caption = "Delete all bookmarks"
.FaceId = 1985
.OnAction = "bmDeleteAll"
End With
'create list of deleting bookmarks
For Each bm In IIf(bBkmOnRange, ActiveDocument.Bookmarks,
ActiveDocument.Range.Bookmarks)
Set cbbDelBM = cbbRemoveBM.Controls.Add(msoControlButton)
With cbbDelBM
.Caption = bm.Name
.Style = msoButtonCaption
.Tag = bm.Name
.OnAction = "bmDel"
'set separator
If bFirstItem Then
.BeginGroup = True
bFirstItem = False
End If
End With
Next bm
End With

'create list of bookmarks subject to select type sorting
For Each bm In IIf(bBkmOnRange, ActiveDocument.Bookmarks,
ActiveDocument.Range.Bookmarks)
Set cbbListBM = cb.Controls.Add(msoControlButton)
With cbbListBM
.Caption = bm.Name
.Style = msoButtonCaption
.OnAction = "bmGoTo"
.Tag = bm.Name
'set separator
If bFirstItemCur Then
.BeginGroup = True
bFirstItemCur = False
End If
End With
Next bm


'Plasing popup menu
With CommandBars.ActionControl
' .TooltipText = "Work with bookmark"
cb.ShowPopup .Left + 25, .Top + .Height - 24
' .FaceId = 454
End With

'Restore all variable or set these is nothing
ActiveDocument.Bookmarks.ShowHidden = bShowHS

Set cb = Nothing
Set cbbSrvs = Nothing
Set cbbShowBM = Nothing
' Set cbbSortBM = Nothing
Set cbbRemoveBM = Nothing
Set cbbDelAllBM = Nothing
Set cbbDelBM = Nothing
Set cbbListBM = Nothing

End If
End Sub

'show/hide bookmark
Sub bmShow()
With ActiveWindow.View
.ShowBookmarks = Not .ShowBookmarks
End With
End Sub

'sort bookmark
Sub bmSortLoc()
bBkmOnRange = Not bBkmOnRange
With cbbSortBM
.OnAction = "mymenu"
End With

End Sub

'remove all bookmarks
Sub bmDeleteAll()
For i = ActiveDocument.Bookmarks.Count To 1 Step -1
ActiveDocument.Bookmarks(i).Delete
Next i
End Sub

'remove one bookmark
Sub bmDel()
ActiveDocument.Bookmarks(CommandBars.ActionControl.Tag).Delete
Selection.Collapse wdCollapseStart
End Sub

'Go to bookmark
Sub bmGoTo()

ActiveDocument.Bookmarks(CommandBars.ActionControl.Tag).Range.Select
End Sub
 
A

avkokin

Hi Helmut.
Thank you but it not helped. I changed block of code for ActionControl
property but it not got effect too.
Set cbMenuBtn = CommandBars.ActionControl
With cbMenuBtn
' .TooltipText = "Work with bookmarks"
' .FaceId = 454
cb.ShowPopup _
cbMenuBtn.Left + 25, _
cbMenuBtn.Top + cbMenuBtn.Height - 24
End With
 
J

Jan Hyde (VB MVP)

avkokin <[email protected]>'s wild thoughts were released on
Tue, 21 Oct 2008 10:20:04 -0700 (PDT) bearing the following
fruit:
Hello Jan.
Yes, that is the code (below).
I will explain that I want. This macro(template) show menu with tools
for work with bookmarks. See screen: http://img.photobucket.com/albums/v190/baston/bm.gif
But first you should create toolbar "bkm" and place it to left
position and add to it button of macro "mymenu".


It goes wrong here

With CommandBars.ActionControl
cb.ShowPopup .left + 25, .top + .height - 24

At least it does for me. ActionControl is set to nothing,
hence you get an object variable not set error when you
attempt to access the Left property.

J
 
J

Jan Hyde (VB MVP)

avkokin <[email protected]>'s wild thoughts were released on
Tue, 21 Oct 2008 10:20:04 -0700 (PDT) bearing the following
fruit:
Hello Jan.
Yes, that is the code (below).
I will explain that I want. This macro(template) show menu with tools
for work with bookmarks. See screen: http://img.photobucket.com/albums/v190/baston/bm.gif
But first you should create toolbar "bkm" and place it to left
position and add to it button of macro "mymenu".

Darn, posted too quick.

I meant to say, does it go wrong in the same place (I doubt
it will because you should be set up correctly)

If not, then on which line does the error occur?

J
 
A

avkokin

Yes, the problem into ActiveControl. But I change a little my code
(add the check of count bookmark for list of showed bookmarks). It
work if uncomment operator "On Error Resume next". But if I comment it
I got still error. How can I correct the mistake? Thank you very much.
The code new:
Option Explicit
Dim cb As CommandBar
Dim cbbBM As CommandBarButton
Dim cbbSrvs As CommandBarPopup
Dim cbbShowBM As CommandBarButton 'variable - "Show bookmark"
Dim cbbSortBM As CommandBarButton '
Dim cbbRemoveBM As CommandBarPopup '
Dim cbbDelAllBM As CommandBarButton '
Dim cbbDelBM As CommandBarButton '
Dim cbbListBM As CommandBarButton '

Dim bm As Bookmark '
Dim bBkmOnRange As Boolean '
Dim bShowHS As Boolean '
Dim bFirstItem As Boolean '
Dim bFirstItemCur As Boolean '
Dim i As Long '

Sub mymenu()
On Error Resume Next
'Check whether open documents
If Application.Documents.Count = 0 Then
MsgBox "Not open document"
Else

Set cb = CommandBars.Add("bmPopup", msoBarPopup)

bShowHS = ActiveDocument.Bookmarks.ShowHidden
ActiveDocument.Bookmarks.ShowHidden = False
'for separator of deleting bookmarks
bFirstItem = True
'for separator of current bookmarks
bFirstItemCur = True

'create item menu "Service" with popup menu
Set cbbSrvs = cb.Controls.Add(msoControlPopup)
With cbbSrvs
.Caption = "Service"
'create item "Show bookmark"
Set cbbShowBM = cbbSrvs.Controls.Add(msoControlButton)
With cbbShowBM
If ActiveWindow.View.ShowBookmarks Then
.FaceId = 1664
.Caption = "Hide bookmark"
Else
.FaceId = 0
.Caption = "Show bookmark"
End If
.OnAction = "bmShow"
End With
'create item "Sort by location"
Set cbbSortBM = cbbSrvs.Controls.Add(msoControlButton)
If ActiveDocument.Bookmarks.Count = 0 Then
cbbSortBM.Enabled = False
End If
With cbbSortBM
.Caption = IIf(bBkmOnRange, "Sort by location", "Sort by
alphabetically")
.OnAction = "bmSortLoc"
End With
End With

'create item "Remove bookmarks"
Set cbbRemoveBM = cb.Controls.Add(msoControlPopup)
If ActiveDocument.Bookmarks.Count = 0 Then
cbbRemoveBM.Enabled = False
End If
With cbbRemoveBM
.Caption = "Remove bookmarks"
.BeginGroup = True
'create item "Delete all bookmarks"
Set cbbDelAllBM = cbbRemoveBM.Controls.Add(msoControlButton)
With cbbDelAllBM
.Caption = "Delete all bookmarks"
.FaceId = 1985
.OnAction = "bmDeleteAll"
End With
'create list of deleting bookmarks
If ActiveDocument.Bookmarks.Count > 0 Then
For Each bm In IIf(bBkmOnRange, ActiveDocument.Bookmarks,
ActiveDocument.Range.Bookmarks)
Set cbbDelBM = cbbRemoveBM.Controls.Add(msoControlButton)
With cbbDelBM
.Caption = bm.Name
.Style = msoButtonCaption
.Tag = bm.Name
.OnAction = "bmDel"
'set separator
If bFirstItem Then
.BeginGroup = True
bFirstItem = False
End If
End With
Next bm
End If
End With

'create list of bookmarks subject to select type sorting
If ActiveDocument.Bookmarks.Count > 0 Then
For Each bm In IIf(bBkmOnRange, ActiveDocument.Bookmarks,
ActiveDocument.Range.Bookmarks)
Set cbbListBM = cb.Controls.Add(msoControlButton)
With cbbListBM
.Caption = bm.Name
.Style = msoButtonCaption
.OnAction = "bmGoTo"
.Tag = bm.Name
'set separator
If bFirstItemCur Then
.BeginGroup = True
bFirstItemCur = False
End If
End With
Next bm
End If


'Plasing popup menu
With CommandBars.ActionControl
' .TooltipText = "Work with bookmark"
cb.ShowPopup .Left + 25, .Top + .Height - 24
' .FaceId = 454
End With

'Restore all variable or set these is nothing
ActiveDocument.Bookmarks.ShowHidden = bShowHS

Set cb = Nothing
Set cbbSrvs = Nothing
Set cbbShowBM = Nothing
' Set cbbSortBM = Nothing
Set cbbRemoveBM = Nothing
Set cbbDelAllBM = Nothing
Set cbbDelBM = Nothing
Set cbbListBM = Nothing

End If
End Sub

'show/hide bookmark
Sub bmShow()
With ActiveWindow.View
.ShowBookmarks = Not .ShowBookmarks
End With
End Sub

'sort bookmark
Sub bmSortLoc()
bBkmOnRange = Not bBkmOnRange
With cbbSortBM
.OnAction = "mymenu"
End With

End Sub

'remove all bookmarks
Sub bmDeleteAll()
For i = ActiveDocument.Bookmarks.Count To 1 Step -1
ActiveDocument.Bookmarks(i).Delete
Next i
End Sub

'remove one bookmark
Sub bmDel()
ActiveDocument.Bookmarks(CommandBars.ActionControl.Tag).Delete
Selection.Collapse wdCollapseStart
End Sub

'Go to bookmark
Sub bmGoTo()

ActiveDocument.Bookmarks(CommandBars.ActionControl.Tag).Range.Select
End Sub
 
A

avkokin

Hello All! I found reason for my mistake.
Firstly this code was placed in project of template into module
"ThisDocument". But when I created new module into project and placed
my code there my macro-code starts to work absolutely correctly and
without error. Why?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top