Word 2007 and 2003 automation problem

T

Terry

I am trying to correct preexisting code to use Word 2007 or Word 2003. The
code opens and transforms an XML document. In 2007, it inserts the Header
Image fine, however when I run the code on amachine with 2003 I get an Error
on the ViewHeaderOnly ( "ViewHeaderOnly" on type '_ComObject' not found).
The code is as follows:

Dim wa As Microsoft.Office.Interop.Word.Application
Dim wd As Microsoft.Office.Interop.Word.Document
wa = New Microsoft.Office.Interop.Word.Application
wa.Documents.Open(REPORT_DATA_XML)
wd = wa.ActiveDocument
wd.TransformDocument(REPORT_TRANSFORM, True)

wa.WordBasic.ViewHeaderOnly()
Dim strValue As String
strValue = AppPath & "\ReportHeader.JPG"
wa.Selection.InlineShapes.AddPicture(FileName:=strValue, LinkToFile:=False,

SaveWithDocument:=True)

Any suggestions on how I could correct this to work in either 2007 or 2003?

Terry
 
C

Cindy M.

Hi Terry,

Is this VB.NET or something else?

Quite likely, Word 2003 doesn't have the ViewHeaderOnly method. You should
always program against the oldest version, for two reasons: 1. To avoid this
kind of problem (using new functionality not available in older versions) and
2. Office more readily changes the type library references going from older to
newer than vice-versa.

In any case, you should avoid using the Selection object whenever possible.
Better is to work with the underlying object (usually a Range). So your code
should look more like this (off the top of my head, so there may be a typo):

wd = wa.Documents.Open(REPORT_DATA_XML)
wd.TransformDocument(REPORT_TRANSFORM, True)

Dim strValue As String
strValue = AppPath & "\ReportHeader.JPG"
Dim rng as Word.Range
rng = wd.Sections(1).Headers(Word.WdHeaderFooter.wdHeaderFooterPrimary).Range
rng.InlineShapes.AddPicture(FileName:=strValue, LinkToFile:=False, _
SaveWithDocument:=True, Range:=rng)
I am trying to correct preexisting code to use Word 2007 or Word 2003. The
code opens and transforms an XML document. In 2007, it inserts the Header
Image fine, however when I run the code on amachine with 2003 I get an Error
on the ViewHeaderOnly ( "ViewHeaderOnly" on type '_ComObject' not found).
The code is as follows:

Dim wa As Microsoft.Office.Interop.Word.Application
Dim wd As Microsoft.Office.Interop.Word.Document
wa = New Microsoft.Office.Interop.Word.Application
wa.Documents.Open(REPORT_DATA_XML)
wd = wa.ActiveDocument
wd.TransformDocument(REPORT_TRANSFORM, True)

wa.WordBasic.ViewHeaderOnly()
Dim strValue As String
strValue = AppPath & "\ReportHeader.JPG"
wa.Selection.InlineShapes.AddPicture(FileName:=strValue, LinkToFile:=False,

SaveWithDocument:=True)

Any suggestions on how I could correct this to work in either 2007 or 2003?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top