Solved

Word 2003 Startup Macro Error 91

Posted on 2006-07-10
6
811 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-indent word doc 5 46
Word 2016 Search Comments 5 39
Office 365 home questions 7 65
Find fonts in Word 2010 32 25
Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
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 video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

773 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