问题
Here is my code in Form1.vb:
Imports Word = Microsoft.Office.Interop.Word
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oWord As Word.Application
Dim oWord1 As Microsoft.Office.Interop.Word.Application
Dim oWord2 as Application
I'm using VStudio2012 and Office 2010 and following the sample from https://support.microsoft.com/kb/316383
The declaration of oWord is flagged by intellisense as "Type 'Word.Application' is not defined,
while oWord1 and oWord2 are accepted. Intellisense provides several error correction options - two of which I've tried (oWord1 and oWord2) - the other options don't seem to apply.
In short, I am confused about why the use of the namespace alias Word on the Imports statement does not satisfy the syntax Dim oWord As Word.Application as described in the KB article. Is there some "master knob" to turn in the project properties dialog or something?
回答1:
You will need to add a reference to import the DLL for this namespace. In Visual Studio 2012
, in your solution explorer` right click on the project name > click "Add Reference" > Choose "Assemblies" > "Extensions" > "Microsoft.Office.Interop.Word". There may be multiple version of the Interop Libraries (Office 2003, 2007, 2010, etc), choose the appropriate one.
I believe these assemblies are originally installed when you do your installation of Microsoft Office from the DVD. So you will need to have Word installed I believe.
Edit: this works for me, with Intellisense:
Imports Word = Microsoft.Office.Interop.Word
Module Module1
Sub Main()
Dim oWord As Word.Application
End Sub
End Module
回答2:
Instead of 'Imports Word = Microsoft.Office.Interop.Word' I used 'Imports Microsoft.Office.Interop' and it worked fine
来源:https://stackoverflow.com/questions/20911217/alias-for-microsoft-office-interop-word-namespace-not-recogized