Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word 2003 Startup Macro Error 91

Posted on 2006-07-10
6
Medium Priority
?
817 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

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 143

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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

722 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