Hi there
The below code works if you don't try to do anything to the word document
which is great (Not :D)
I am having problems with the context command both text (i.e initial document with no text) or a subsequent addition of text, insert, to the document.
as this is going to be used along side other com opened applications Excel power point etc. it needs to work in the self.X orientation as i can get it to work normally see code 2
[CODE=python]
import win32com.client , pythoncom
class main():
def __init__(self, visible = 0):
#Staring the Word's file.
#example: r = Word.main)
#now try to open word, work backwards.
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 11") # Word 2003
except:
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 10") # Word XP
except:
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 9") # Word 2000
except:
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 8") # Word 97
except:
try:
self.Word = win32com.client .DispatchEx('Wo rd.Application' ) # Word 5.0?
except:
print "No Office... oh "
sys.exit("no Office")
if visible == True:
self.Word.Visib le = 1
else:
self.Word.Visib le = 0
def new_or_open(sel f,theFileName=" "):
# this is to create a new word file or to open an exsisting.
if theFileName=="" :
#new
self.Word.Docum ents.Add()
else:
#exisitng
try:
self.Word.Docum ents.Open(FileN ame=theFileName )
except:
print "oh rubbish something went wrong"
def addSomeWords(se lf,wobble):
#add some words
self.Word.Docum ents.Content.In sertAfter(wobbl e)
def save(self, theFileName="") :
#this function is to save the file or to resave the file
if theFileName=="" :
try:
self.Word.Activ eDocument.Save( )
except:
print "oh Crap something else went worng"
else:
self.Word.Activ eDocument.SaveA s(FileName=theF ileName)
#print "File saved"
def close(self,acti veDoc=0):
if activeDoc==1:
self.Word.Activ eDocument.Close ()
else:
#print "Closing... "
self.Word.Activ eDocument.Close ()
self.Word.Quit( )
#no reset everything
#self.Word = False
if __name__ == "__main__":
print "Performing Word's test..."
try:
#open
r = main(True)
#create new
r.new_or_open(" c:\wobble.doc")
#now do something
r.addSomeWords( "hello there world")
r.save()
r.close()
except:
exit()
[/CODE]
code 2
[CODE=Python]
pythoncom.CoIni tializeEx(pytho ncom.COINIT_APA RTMENTTHREADED)
wordapp = win32com.client .Dispatch("Word .Application") # Create new Word Object
wordapp.Visible = 1 # Word Application should`t be visible
worddoc = wordapp.Documen ts.Add() # Create new Document Object
worddoc.Content .Text = "hello world \n\r"
x=0
while x <>10:
wobble=r'=rand( 200,9)\n\r'
worddoc.Content .InsertAfter(wo bble)
x=x +1
#wend
worddoc.Content .MoveEnd
worddoc.SaveAs( "c:\wiggle.doc" )
worddoc.Close() # Close the Word Document (a save-Dialog pops up)
wordapp.Quit() # Close t
pythoncom.CoUni nitialize()
[/CODE]
The below code works if you don't try to do anything to the word document
which is great (Not :D)
I am having problems with the context command both text (i.e initial document with no text) or a subsequent addition of text, insert, to the document.
as this is going to be used along side other com opened applications Excel power point etc. it needs to work in the self.X orientation as i can get it to work normally see code 2
[CODE=python]
import win32com.client , pythoncom
class main():
def __init__(self, visible = 0):
#Staring the Word's file.
#example: r = Word.main)
#now try to open word, work backwards.
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 11") # Word 2003
except:
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 10") # Word XP
except:
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 9") # Word 2000
except:
try:
self.Word = win32com.client .DispatchEx("Wo rd.Application. 8") # Word 97
except:
try:
self.Word = win32com.client .DispatchEx('Wo rd.Application' ) # Word 5.0?
except:
print "No Office... oh "
sys.exit("no Office")
if visible == True:
self.Word.Visib le = 1
else:
self.Word.Visib le = 0
def new_or_open(sel f,theFileName=" "):
# this is to create a new word file or to open an exsisting.
if theFileName=="" :
#new
self.Word.Docum ents.Add()
else:
#exisitng
try:
self.Word.Docum ents.Open(FileN ame=theFileName )
except:
print "oh rubbish something went wrong"
def addSomeWords(se lf,wobble):
#add some words
self.Word.Docum ents.Content.In sertAfter(wobbl e)
def save(self, theFileName="") :
#this function is to save the file or to resave the file
if theFileName=="" :
try:
self.Word.Activ eDocument.Save( )
except:
print "oh Crap something else went worng"
else:
self.Word.Activ eDocument.SaveA s(FileName=theF ileName)
#print "File saved"
def close(self,acti veDoc=0):
if activeDoc==1:
self.Word.Activ eDocument.Close ()
else:
#print "Closing... "
self.Word.Activ eDocument.Close ()
self.Word.Quit( )
#no reset everything
#self.Word = False
if __name__ == "__main__":
print "Performing Word's test..."
try:
#open
r = main(True)
#create new
r.new_or_open(" c:\wobble.doc")
#now do something
r.addSomeWords( "hello there world")
r.save()
r.close()
except:
exit()
[/CODE]
code 2
[CODE=Python]
pythoncom.CoIni tializeEx(pytho ncom.COINIT_APA RTMENTTHREADED)
wordapp = win32com.client .Dispatch("Word .Application") # Create new Word Object
wordapp.Visible = 1 # Word Application should`t be visible
worddoc = wordapp.Documen ts.Add() # Create new Document Object
worddoc.Content .Text = "hello world \n\r"
x=0
while x <>10:
wobble=r'=rand( 200,9)\n\r'
worddoc.Content .InsertAfter(wo bble)
x=x +1
#wend
worddoc.Content .MoveEnd
worddoc.SaveAs( "c:\wiggle.doc" )
worddoc.Close() # Close the Word Document (a save-Dialog pops up)
wordapp.Quit() # Close t
pythoncom.CoUni nitialize()
[/CODE]
Comment