Link to home
Start Free TrialLog in
Avatar of sfjcpu
sfjcpuFlag for United States of America

asked on

Word 2003 Startup Macro Error 91

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!
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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
Avatar of sfjcpu

ASKER

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!
>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

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
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.




 
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial