?
Solved

MS Word 2007 late binding errors

Posted on 2009-04-21
20
Medium Priority
?
625 Views
Last Modified: 2012-05-06
Greetings Experts;

I have a customer management application that deals with credit ratings, inventory management and sales history.  Within the Credit Rating section, I have a button that, when clicked, generates a MS Word document and populates it with customer rating data pulled from a SQL 2005 db.

I have not been able to test this section of code, however, because I keep generating Late Binding errors with the WordApp object.  I added the Microsoft Office 12.0 Object Library COM reference, however that did not appear to fix anything.  Any suggestions your fixes you can offer would be most appreciated.

Thanks for your time.
Dim WordApp As Object = CreateObject("Word.Application")
Dim doc As Object = WordApp.documents.add
 
'add text to document
With WordApp.selection
            .font.size = WordApp.selection.font.size + 2
            .typetext(strTodaysDate)
            .typeparagraph()
            .typetext(strIntroText)
            .typeparagraph()
            .typetext(strBodyText)
            .typeparagraph()
End With
 
WordApp.visible = True
doc.activewindow.activate()
 
Dim fName As String
SaveFileDialog1.Filter = "Documents|*.doc"
SaveFileDialog1.ShowDialog()
fName = SaveFileDialog1.FileName
If fName <> "" Then
            Try
                doc.saveas(fName)
            Catch ex As Exception
                MessageBox.Show("Failed to save document" & vbCrLf & ex.Message)
            End Try
End If

Open in new window

0
Comment
Question by:gwosgood
  • 9
  • 6
  • 5
20 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24202668
Adding a reference to the Word COM type won't help any, if you are still using late-bound objects.  WordApp and doc are still Object, so the compiler can't help you find problems.  What are the errors that you are getting?

0
 
LVL 2

Author Comment

by:gwosgood
ID: 24203694
I get Late Binding errors in the following places...

With WordApp.selection
 .
 .
 .
End With

every reference to WordApp within this With block is flagged as a late binding error, and unresolved.

And also when trying to reference any properties of doc.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 24203779
I think a better option is to use the Microsoft Interop because it provides you complete Office Object Model in .NET classes.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 24204254
If you are getting late-binding errors, I would imagine you have Option Strict On.  You should be able to use early-bound references, like this:

Dim WordApp As New Word.Application()
Dim WordDoc As Word.Document = WordApp.Documents.Add()
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24206658
What Namespace do I need to import in order to get Word.Application and Word.Document to defined and recognized?

Ive got
Imports Microsoft.Office.Interop

and a reference to
Microsoft Office 12.0 Object library

but I still get errors saying that Word.Application and Word.Document are undefined.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24206705
When you install Microsoft Office Interop Assemblies, you add reference to individual namespaces.
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24207046
Ok, I went to the microsoft site and downloaded/installed the Assembly package, rebooted my computer and still nothing.  

I get this warning message when I hover over the Microsoft.Office.Interop import line --

Namespace or type specified in the Imports 'Microsoft.Office.Interop' doesnt contain any public member or cannot be found.  Make sure the namespace or the type is defined and contains at least one public member.  Make sure the imported element name doesnt use any aliases.

The attached pic is the Reference Properties for my project.  You can see that I have the Office 12.0 COM referrence, however the Microsoft.Office.Interop namespace is not listed in the available namespace list.
Missing-Interop.jpg
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24207086
You need to add reference to Microsoft.Office.Interop.Word.dll
See the screenshot.
Screen.JPG
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24207157
Ive got .NET 2005 if that makes a difference, I see your folder structure points to Visual Studio 9.0.

I looked within my folder structure but did not see a PIA folder.  Is that something new to 2008?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24208681
When you install PIA, the files become available within the .NET tab.You do not need to search for the folder.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 24208880
With Office 2007, you don't need to install any PIAs.  When you add a reference to the Microsoft Word 12.0 Object Library, the COM interop run-time callable wrapper will be automatically generated for you.


Add-Reference-Dialog----Word-12..png
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24208944
Ive added the Word 12.0 Object library, as seen in my earlier screen grab, however the declaration

Dim WordApp As New Word.Application

still gives me the error   "Type 'Word.Application' is not defined"

Is there another way I can do this?

I need to create a file based off some SQL info, insert a header image (company logo), insert some legal boilerplate and then the SQL data related to the customer in question.  I originally considered using StreamWriter, but I could not find any reference to image insertion with this method.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24208971
Did you try my previous suggestion?
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24208993
Yeah, I downloaded and installed the file

2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies

from

http://www.microsoft.com/downloads/details.aspx?FamilyID=59daebaa-bed4-4282-a28c-b864d8bfa513&displaylang=en

it downloaded and installed fine, but I still get nothing.  Its possible the file is bjorked??


Either that or im doing it wrong
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24208996
You can find out what is expected by typing out Imports Microsoft.Office.Interop at the top of the code-module.  Each successive namespace will show.

Imports Microsoft.
Imports Microsoft.Office
Imports Microsoft.Office.Interop

Example:
Imports Microsoft.Office.Interop
 
Public Class WordProcessor
 
    Private m_application As Word.Application = Nothing
    Private m_document As Word.Document = Nothing
 
    Public Sub New()
        m_application = New Word.Application()
        m_document = m_application.Documents.Add()
    End Sub
 
End Class

Open in new window

0
 
LVL 2

Author Comment

by:gwosgood
ID: 24209022
TheLearnedOne,

The only two options I have are :

Imports Microsoft.Office.Core
Imports Microsoft.Office.Tools


I dont get Interop for some reason.  If I manually type it, it gives me a warning

"Namespace or type specified in the Imports 'Microsoft.Office.Interop' doesnt contain any public member or cannot be found.  Make sure the namespace or the type is defined and contains at least one public member.  Make sure the imported element name doesnt use any aliases."
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24209137
As seen in the attached screenshot, click on Add Reference, click on the .NET tab, scroll down and select Microsoft.Office.Interop.Word.dll from the list.
Add-Reference-Dialog----Word-12..png
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24209183
This is all I get in the .NET tab.

Ive already added the Microsoft.Office.Tools.Word .dll, which is why I can import it.  Is there perhaps a way I can get the Word functionality through this reference instead?
No-Interop.jpg
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24211047
That is the .NET tab, you need to be looking on the COM tab.  That is VSTO on the .NET tab.
0
 
LVL 2

Author Comment

by:gwosgood
ID: 24214311
My apologies gentlemen, it took the both of you to make me realize that Microsoft Office 12.0 and Microsoft Word 12.0 were not the same thing.  I scrolled further down my list of COM references and found what I needed.  

Since im not sure how to award the points for this, im just going to split it 50-50.  Appreciate all the help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question