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 -
-
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("Word.Application.11") # Word 2003
-
except:
-
try:
-
self.Word = win32com.client.DispatchEx("Word.Application.10") # Word XP
-
except:
-
try:
-
self.Word = win32com.client.DispatchEx("Word.Application.9") # Word 2000
-
except:
-
try:
-
self.Word = win32com.client.DispatchEx("Word.Application.8") # Word 97
-
except:
-
try:
-
self.Word = win32com.client.DispatchEx('Word.Application') # Word 5.0?
-
except:
-
print "No Office... oh "
-
sys.exit("no Office")
-
-
if visible == True:
-
self.Word.Visible = 1
-
else:
-
self.Word.Visible = 0
-
-
def new_or_open(self,theFileName=""):
-
# this is to create a new word file or to open an exsisting.
-
if theFileName=="":
-
#new
-
self.Word.Documents.Add()
-
else:
-
#exisitng
-
try:
-
self.Word.Documents.Open(FileName=theFileName)
-
except:
-
print "oh rubbish something went wrong"
-
-
def addSomeWords(self,wobble):
-
#add some words
-
self.Word.Documents.Content.InsertAfter(wobble)
-
-
def save(self, theFileName=""):
-
#this function is to save the file or to resave the file
-
if theFileName=="":
-
try:
-
self.Word.ActiveDocument.Save()
-
except:
-
print "oh Crap something else went worng"
-
else:
-
self.Word.ActiveDocument.SaveAs(FileName=theFileName)
-
#print "File saved"
-
-
def close(self,activeDoc=0):
-
-
if activeDoc==1:
-
self.Word.ActiveDocument.Close()
-
else:
-
#print "Closing..."
-
self.Word.ActiveDocument.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 2 -
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
-
wordapp = win32com.client.Dispatch("Word.Application") # Create new Word Object
-
wordapp.Visible = 1 # Word Application should`t be visible
-
worddoc = wordapp.Documents.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(wobble)
-
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.CoUninitialize()
-
1 4337
DOH!
ok simple really have to tell it to move the cursor to the end of the line before you can insert after..
hence at line 45 insert the following - self.Word.ActiveDocument.Content.MoveEnd
it works wonderfully
I really should try everything before posting :D
cheers
D2
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ingvald |
last post by:
hi,
i've hit a wall regarding php and ms word. what i want is to open a
document containing bookmarks, insert text where the bookmarks are, and
save.
it's working, unless the bookmark is in...
|
by: Diane Yocom |
last post by:
I realize this may not be the best place to post this, but I was at a loss
as to where to go, so...
I'm trying to merge two separate RTF files (created in Word) into one Word
document. I'd like...
|
by: blue_nirvana |
last post by:
I have a form that a user loads pictures onto using picture boxes. I want to
be able to insert those pictures into a Word document. How can I insert a
picture into a Word document through...
|
by: Adam Faulkner via DotNetMonster.com |
last post by:
I want to create a method within a class that opens a Microsoft Word 2000
Document and has the facility to Create a new word document and then extract
a Page that exists within the original Word...
|
by: narayana |
last post by:
hi gurus,
I am kid at DB2 .problem is I am unable to insert values of time and
date datatype fields into tables .default date datatype is taking
4bytes lenght so I am unable to insert date value...
|
by: rn5a |
last post by:
During registration, users are supposed to enter the following
details: First Name, Last Name, EMail, UserName, Password, Confirm
Password, Address, City, State, Country, Zip & Phone Number. I am...
|
by: carmen413 via VBMonster.com |
last post by:
Dear All,
my text file like following,
a1, b1, c1, d1, e1
a2, b2, c2, d2, e2
a3, b3, c3, d3, e3
a4, b4, c4, d4, e4
.................
|
by: Cintury |
last post by:
Hi all,
I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
|
by: bugboy |
last post by:
I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'.
I've been...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |