Using Microsoft Visual FoxPro to add images to a Microsoft Word 2003 or 2007 InLineShapes collection

Action

You are using Microsoft Visual FoxPro to execute OLE automation code against Microsoft Word 2003 or 2007. The code attempts to add an image to an InLineShapes collection in a Microsoft Word document.

 

Result

You may receive this error in Microsoft Visual FoxPro:

“OLE Idispatch exception code 0 from Microsoft Word: This is not a valid file name.”

 

Cause

The Microsoft Visual FoxPro code in use may not properly reference the InLineShapes collection in the Microsoft Word document.

 

Resolution

The following code demonstrates how to add a bitmap image to an InlineShapes collection in a Microsoft Word 2003 or 2007 document. This code was written and tested with Microsoft Visual FoxPro 9.0 SP2, but with a little modification it should execute successfully from any Microsoft Visual FoxPro version. This sample code was adapted from a much larger example written by Calvin Hsia and posted to his BLOG:
Sudoku puzzles screen capture (http://blogs.msdn.com/calvin_hsia/archive/2005/08/17/452734.aspx)

*——– Begin Code
*
PUBLIC oW AS WORD.APPLICATION, ;
lcBMP AS STRING, liVFPWinState AS INTEGER

*– Store WindowState of _SCREEN and minimize
liVFPWinState = _SCREEN.WINDOWSTATE
_SCREEN.WINDOWSTATE= 1
oW = CREATEOBJECT( ‘Word.Application’ )

*– Alter the path to the bitmap file as needed.
lcBMP = _SAMPLES + ‘\TASTRADE\BITMAPS\BEVERAGE.BMP’
WITH oW AS WORD.APPLICATION
.WINDOWSTATE= 1 && wdWindowStateMaximize
.VISIBLE = 1
.Documents.ADD
.APPLICATION.ACTIVATE
WITH .ActiveDocument.PageSetup AS WORD.PageSetup
.TopMargin = 12.25
.BottomMargin = 12.25
.LeftMargin = 12.25
.RightMargin = 12.25
ENDWITH
.SELECTION.InlineShapes.AddPicture( lcBMP, .F., .T. )
.SELECTION.EndKey( 6 )
.SELECTION.MoveLeft( 1, 1, 1 )
.SELECTION.InlineShapes( 1 ).WIDTH = 190
.SELECTION.InlineShapes( 1 ).HEIGHT = 210
.SELECTION.EndKey( 6 )
.SELECTION.HomeKey( 6 ) && Go to top of doc
=INKEY( 5 ) && Wait five seconds.
.QUIT( 0 ) && Quit w/o saving. Pass -1 to save.
ENDWITH

*– Restore _SCREEN
_SCREEN.WINDOWSTATE = liVFPWinState
*
*——– End Code

 

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

 


APPLIES TO
Microsoft Visual FoxPro 8.0 Professional Edition
Microsoft Visual FoxPro 9.0, Professional Edition
Microsoft Office Word 2003
Microsoft Office Word 2007

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

 


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image