Solved

Word 2003 Startup Macro Error 91

Posted on 2006-07-10
6
809 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

920 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

12 Experts available now in Live!

Get 1:1 Help Now