Solved

Word 2003 Startup Macro Error 91

Posted on 2006-07-10
6
807 Views
Last Modified: 2008-02-01
I'm trying to set up a shortcut to word that includes a macro in the parameter.  

I recorderd the macro "macro1" in Word 2003 using the Tools, Macros, Record New Macro menu.  Then to test the macro, I tried running the macro from the Start, Run menu as follows:  

winword /mmacro1

This is the text of the macro:

Sub TestMacro()
'
' TestMacro Macro
' Macro recorded 7/10/2006 by S Frank Jones
'
    Selection.TypeText Text:= _
        "This is my test macro.  It runs when I start Word."
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeText Text:="This is the last line of the macro."
End Sub


When I run the command, I get the following error:
 
"Run Time Error 91:

Object variable or with block variable not set"

Why is this not working?  I also tried creating a new "normal.dot" file, but when the macro is recreated with the new normal.dot file, I get the same error.  I've also tried running it from a shortcut.  

Since I have an immediate need, I'm putting 500 points for this question.

Thanks!
0
Comment
Question by:sfjcpu
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17077021
if Selection object is not set to a valid document, this will fail.
additionally, you should add error handling:


Sub TestMacro()
'
' TestMacro Macro
' Macro recorded 7/10/2006 by S Frank Jones
'
on error goto ErrHandler:

   if not (selection is nothing) then
    Selection.TypeText Text:= _
        "This is my test macro.  It runs when I start Word."
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeText Text:="This is the last line of the macro."
  End if

Exit Sub

ErrHandler:
  msgbox err.description

End Sub
0
 

Author Comment

by:sfjcpu
ID: 17077347
hello angelIII.  that change stopped the error message, but since the Selection object is not an existing document. can the macro be set to prompt for a file name and then insert the desired text?  the pupose of the macro is to insert a letterhead automatically into a new document when the shortcut is executed from the desktop.  Thanks!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17077385
>can the macro be set to prompt for a file name and then insert the desired text?
yes: http://www.microsoft.com/technet/scriptcenter/resources/officetips/aug05/tips0825.mspx

Like this:

Const msoFileDialogOpen = 1

Set objWord = Application

objWord.ChangeFileOpenDirectory("C:\Scripts")

objWord.FileDialog(msoFileDialogOpen).Title = "Select the files to be openend"
objWord.FileDialog(msoFileDialogOpen).AllowMultiSelect = false

If objWord.FileDialog(msoFileDialogOpen).Show = -1 Then
    objWord.WindowState = 2
    For Each objFile in objWord.FileDialog(msoFileDialogOpen).SelectedItems
        set objDoc = objWord.Documents.Open(objFile.path)
        with objDoc.Range
           .TypeText Text:= _
        "   This is my test macro.  It runs when I start Word."
             .TypeParagraph
             .TypeParagraph
             .TypeText Text:="This is the last line of the macro."

        end with

    Next
   
End If

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17079596
The only time that there is no Selection Object is when there are no documents open in the Application, so all you need to do is to create one.

Sub UseSelection()
    If Documents.Count = 0 Then
        Documents.Add
    End If
    Selection.TypeText Text:= _
        "This is my test macro.  It runs when I start Word."
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeText Text:="This is the last line of the macro."
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17079696
Obviously a macro can only record the selections that you make and the editing that you do, but personally, I prefer to avoid the Selection Object, but use a Range object instead.

The code for that would be:

Sub UseRange()
    Dim Doc As Document
    Dim rng As Range
   
    If Documents.Count = 0 Then
        Set Doc = Documents.Add
    Else
        Set Doc = ActiveDocument
    End If
    Set rng = Doc.Range
    rng.Collapse
    rng.Text = "This is my test macro.  It runs when I start Word."
    rng.InsertParagraphAfter
    rng.InsertParagraphAfter
    rng.InsertAfter "This is the last line of the macro."
End Sub


In this simple macro, the advantages are not obvious, but they include:

1. You can have several Range objects
2. There is less 'flicker'.
3. For that reason it is faster
4. You can work on a document even if it isn't the Active document.




 
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17079733
Actually, you might like to consider two alternatives to having a macro. They are to create your own template, or to use Autotext. Either way, you can easily format the text to your liking.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now