Global confusion
Gaetan Corneau
Gaetan_Corneau at baan.com
Tue Sep 28 17:25:28 EDT 1999
Hello,
I often use scripts like this:
n = "Hello!"
def F():
print n
F()
Today, a friend of mine (a newbie) who is trying to use Python with word,
wrote the following:
import win32com.client.dynamic
Srv = win32com.client.dynamic.Dispatch("word.application")
Srv.Documents("Document1").Activate # Word must already be running and have
document1 open
nbPar = Srv.ActiveDocument.Paragraphs.Count
Srv.ActiveDocument.Paragraphs(nbPar).Range.InsertParagraphAfter()
# Function to add a paragraph
def AddParagraph(style, text):
Srv.ActiveDocument.Paragraphs[nbPar].Range.InsertParagraphAfter()
Srv.ActiveDocument.Paragraphs[nbPar].Range.Style =
Srv.ActiveDocument.Styles(style)
Srv.ActiveDocument.Paragraphs[nbPar].Range.InsertAfter(text)
nbPar = nbPar + 1
# Create a section
def AddObjectSection(objName):
AddParagraph("Heading 1", objName)
AddObjectSection("Customer")
and he got the following:
Traceback (innermost last):
File "D:\harddisk\temp\dantest.py", line 24, in ?
AddObjectSection("Customer")
File "D:\harddisk\temp\dantest.py", line 21, in AddObjectSection
AddParagraph("Heading 1", objName)
File "D:\harddisk\temp\dantest.py", line 14, in AddParagraph
Srv.ActiveDocument.Paragraphs[nbPar].Range.InsertParagraphAfter()
NameError: nbPar
I looked in my "Learning Python" book and told my friend to insert "global
nbPar" as the first line of the "AddParagraph" function.
It works, but I don't understand why he has to do that...
Anybody who can exlain?
TIA,
______________________________________________________
Gaetan Corneau
Software Developer (System Integration Team)
BaaN Supply Chain Solutions
E-mail: Gaetan_Corneau at baan.com
Compuserve: Gaetan_Corneau at compuserve.com
ICQ Number: 7395494
Tel: (418) 266-8252
______________________________________________________
"Profanity is the one language all programmers know best"
More information about the Python-list
mailing list