Hello,

I am trying to embed a html file (a report from sql) in to a word document in a particular location.
I am trying to get this done using the Office Interop.

1) How can I embed a html file in to a word document ?
2) How can I embed it on to a location by replacing a token in the Word doc. Eg . The word file would have a token <html_report>. The HTML file has to be embedded in the place where This token is .

Thanks !

I figured out how to embed a HTML File .
But I am having trouble in the 2nd part , which is placing that HTML file in a desired location in the Word doc.
I guess it has to do with 'Range' . But I do not know how to achieve it .

Any direction/suggestion/explanation would be appreciated.

How about a little context? It would help us to see some code so we know what you're working with

I was able to embed a html file to the word doc using ' document.InlineShapes.AddOLEObject ' .

Where I am is stuck is, trying to embed the file in a desired location Eg: The document has a string "<report>" . I have to find the range of this word or the start location , so I can use this for the 'range' argument in the document.InlineShapes.AddOLEObject function call.

My Python code :

import clr
import System
clr.AddReference("Microsoft.Office.Interop.Word")
import Microsoft.Office.Interop.Word as Word
missing = System.Type.Missing
replaceAll = Word.WdReplace.wdReplaceAll
source_filename = 'C:\\Sample_Report.doc'
destination_filename = 'c:\\Validation Report.doc'

# Open the Report Template MSWord File
word_application = Word.ApplicationClass()
word_application.Visible = False
document = word_application.Documents.Open(source_filename,missing,missing)

# Attempt to find the range of that string in the document
for rge in document.Words:
	r.Find.Text = 'mlrepor'
	r.Find.Replacement.Text = 'XXX'
	r.Find.Wrap = Word.WdFindWrap.wdFindContinue
	found = r.Find.Execute(missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,\
	replaceAll, missing, missing, missing, missing)
	print rge.Start
	if (found == True):
		print found
		obj = document.InlineShapes.AddOLEObject(missing,'C:\\Documents and Settings\\sp19206\\Desktop\\out1.html',False,missing,missing,missing,missing,rge)


document.SaveAs(destination_filename)
#document.Close()
document = None
#word_application.Quit()
word_application = None

Use code tags when posting code or else it is unreadable (as above).

[code=python] # Put your code in here

[/code]

Please

My Python code :

import clr
import System
clr.AddReference("Microsoft.Office.Interop.Word")
import Microsoft.Office.Interop.Word as Word
missing = System.Type.Missing
replaceAll = Word.WdReplace.wdReplaceAll
source_filename = 'C:\\Sample_Report.doc'
destination_filename = 'c:\\Validation Report.doc'

# Open the Report Template MSWord File
word_application = Word.ApplicationClass()
word_application.Visible = False
document = word_application.Documents.Open(source_filename,missing,missing)

# Attempt to find the range of that string in the document
for rge in document.Words:
r.Find.Text = 'mlrepor'
r.Find.Replacement.Text = 'XXX'
r.Find.Wrap = Word.WdFindWrap.wdFindContinue
found = r.Find.Execute(missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,\
replaceAll, missing, missing, missing, missing)
print rge.Start
if (found == True):
print found
obj = document.InlineShapes.AddOLEObject(missing,'C:\\Documents and Settings\\sp19206\\Desktop\\out1.html',False,missing,missing,missing,missing,rge)


document.SaveAs(destination_filename)
#document.Close()
document = None
#word_application.Quit()
word_application = None

Thanks for that suggestion !!! So, now can I have hopes ? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.