Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word 2010 run-time error '91' but works on windows 7 1

Status
Not open for further replies.

basbrian

Programmer
Feb 18, 2002
49
0
0
AU
We have an add-in that lists names of bookmarks that the user selects and it pastes the bookmark into the current document. this has been working fine on a windows 7 pc running word 2010. when we run it on a windows 10 pc with word 2010 we get a runtime error '91' object variable or with block variable not set. Any help with will be appreciated as it was written by an ex employee and I have very little VBA experience.

Code:
Sub STC161()
    setvariables
AppCond "STC161", STcond
End Sub
Sub STC162()
    setvariables
AppCond "STC162", STcond
End Sub

Sub AppCond(varbkmrk, doclocation)
'
' AppCond Macro
'
 Documents.Open filename:=doclocation, _
 ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
        PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
        WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
        wdOpenFormatAuto
    Selection.GoTo What:=wdGoToBookmark, Name:=varbkmrk
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Copy
    ActiveWindow.Close
    Selection.EndKey Unit:=wdStory
    Selection.Paste
    Selection.fields.Update
    Selection.EndKey Unit:=wdStory

End Sub

Sub setvariables()

STcond = "c:\Template\book marks\book marks conditions.doc"

End Sub
 
Before the first line of the code you posted, you should have a line like:
Dim STcond As String
Otherwise, the rest of your code won't know what refers STcond to. As for STCond itself, that refers to a file named:
c:\Template\book marks\book marks conditions.doc
You need to ensure that file is in the specified location; even the extension must remain the same or the code must be changed to suit.

As for the overall code, much of it is pointless. Try:
Code:
Option Explicit
Const STcond As String = "c:\Template\book marks\book marks conditions.doc"

Sub STC161()
Call AppCond("STC161", STcond)
End Sub

Sub STC162()
Call AppCond("STC162", STcond)
End Sub

Sub AppCond(varbkmrk, doclocation)
Application.ScreenUpdating = False
Dim DocTgt As Document, DocSrc As Document
Set DocTgt = ActiveDocument
Set DocSrc = Documents.Open(FileName:=doclocation, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
With DocTgt
  .Range.Characters.Last.FormattedText = DocSrc.Bookmarks(varbkmrk).Range.FormattedText
  DocSrc.Close False
  .Fields.Update
End With
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top