无法转换类型为"Microsoft.Office.Interop.Word.ApplicationClass"的COM对象 [英] Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass'
问题描述
你好,
我正在尝试查找和替换Windows窗体上加载的文档文档一词上的标签,以使用该单词.我声明的范围
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
`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`
例外:
无法将类型为"Microsoft.Office.Interop.Word.ApplicationClass"的COM对象转换为接口类型为"Microsoft.Office.Interop.Word._Application".该操作失败,因为对IID为"{00020970-0000-0000-C000-000000000046}"的接口的COM组件上的QueryInterface调用由于以下错误而失败:找不到. (来自HRESULT的异常:0x80030002(STG_E_FILENOTFOUND)).
为此,我正在使用
-Visual Studio 2005专业版
-MS Office 2003专业版
当我尝试创建单词或excel对象时,该对象显示了Image中附加的所有属性.但是在声明期间,它没有给出任何异常:
我正在使用以下dll和库作为参考:
-Interop.office.dll
-Interop.Excel.dll
-Interop.Word.dll
-Microsoft Office 11.0对象库
-Microsoft Excel 11.0对象库
-Microsoft Word 11.0对象库
此代码在Windows和MS Office的许多组合中都可以正常工作,但是在某些组合中会出现问题.
问候,
Vaibhav
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
推荐答案
您的代码段很混乱.什么是wordControl
?声明在哪里?那wd = wordControl.ActiveDocument
呢?
如果不是Nothing
,为什么不在ForEach
子句中使用它?
Your code snippet is a chaos. What''swordControl
? Where is it declared? And what aboutwd = wordControl.ActiveDocument
?
If that is notNothing
, why don''t you use it in yourForEach
clause?
For Each HeaderFooter In wd.StoryRanges
这篇关于无法转换类型为"Microsoft.Office.Interop.Word.ApplicationClass"的COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!