Click here to Skip to main content
15,889,460 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello,

I am trying to find and replace tags on the word Document which loaded on windows form.to use the word.Range i declared
VB
`Dim wd As Object = New Word.Document

Dim wa As Object = New Word.Application

wd = wordControl.ActiveDocument

 wa = wd.Application

Dim HeaderFooter As Word.Range          'Line1

Dim Header As Word.HeaderFooter         'Line2

//Line1 and Line 2's variable are noting after execution

HeaderFooter = wa.ActiveDocument.Range



For Each HeaderFooter In wa.ActiveDocument.StoryRanges     'this line throws exception

 With HeaderFooter.Find

  .Format = False

  .Replacement.Highlight = 100

  .Replacement.Font.Bold = True

  .Text = "<<Client Name>>"

  .Replacement.Text = "Mr. Client"

  .Execute(Replace:=Word.WdReplace.wdReplaceAll, Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue)

 End With`

Exception:

Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: could not be found. (Exception from HRESULT: 0x80030002 (STG_E_FILENOTFOUND)).


for that I am Using
- Visual Studio 2005 professional edition
- MS Office 2003 professional edition

when I am trying to create a word or excel object the object shows all the properties as attached in the Image.but during the declaration it did not gave any exception:

I am using following dll and library as a reference:

- Interop.office.dll

- Interop.Excel.dll

- Interop.Word.dll

- Microsoft office 11.0 object Library

- Microsoft Excel 11.0 object Library

- Microsoft Word 11.0 object Library


This codes working fine in many combination of windows and MS Office.but give problem in some combination.


Regards,

Vaibhav
Posted
Updated 17-Jul-12 21:17pm
v2

1 solution

Your code snippet is a chaos. What's wordControl? Where is it declared? And what about wd = wordControl.ActiveDocument?
If that is not Nothing, why don't you use it in your ForEach clause?
VB
For Each HeaderFooter In wd.StoryRanges
 
Share this answer
 
Comments
VAIBHAV PANDYA 18-Jul-12 9:17am    
Hello Mr. Hiller

Thank you for your feedback.

wordControl is a name of control which provides the functionality to loading up a word document on the windows Application form.

wd = wordControl.ActiveDocument means the Active Document(which is currently loaded on the wordControl) is assigned to object of word.Document

For Each HeaderFooter In wa.ActiveDocument.StoryRanges
is used to get the variable which needs to be replaced on the document.

I also tried

For Each HeaderFooter In wd.StoryRanges

but it also threw exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Word.Range'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{0002095E-0000-0000-C000-000000000046}' failed due to the following error: could not be found. (Exception from HRESULT: 0x80030002 (STG_E_FILENOTFOUND)).

Thank You,
Bernhard Hiller 18-Jul-12 11:24am    
See also: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/08f13e9d-895c-4102-b6d9-e327af8cf8c0/
The installation of MS Office might be corrupt.
VAIBHAV PANDYA 31-Jul-12 9:24am    
I reinstall Office and also read the link which you mentioned but both are unable to resolve the problem.
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900