Results 1 to 3 of 3

Thread: Very newbie-like question

  1. #1

    Thread Starter
    New Member Xee's Avatar
    Join Date
    Apr 2003
    Posts
    3

    Very newbie-like question

    Is it possible to run VBA code in VB (.NET)? If so, what is needed to do so? For example, if I wanted to create a new document in Word, I would use:

    VB Code:
    1. Dim MyDoc As Word.Document
    2. Documents.Add.SaveAs("Hello.doc")
    3. Set MyDoc = Documents("Hello.doc")

    Trying to paste it into VB.NET:

    VB Code:
    1. Public Class Form1
    2.   Inherits System.Windows.Forms.Form
    3.  
    4.   [b]Windows Form Designer Generated Code[/b]
    5.  
    6.   Private Sub Button1_Click(ByVal sender As System.Object, _
    7.       ByVal e As System.EventArgs) Handles Button1.Click
    8.     Dim MyDoc As Word.Document
    9.     Documents.Add.SaveAs("Hello.doc")
    10.     MyDoc = Documents("Hello.doc")
    11.   End Sub
    12. End Class

    I get errors such as "Type 'Word.Document' is not defined" and "Name 'Documents' is not declared."

    What needs to be done in order to get this working (if it's possible)? I only started coding in VB a few weeks ago so bear with me if this is an inane question. I've been searching all over google and any VB forum I could find for the answer to this question but haven't found anything.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Never worked with .NET, but
    at least you have to create a object that is the Word.application
    In VBA Excel I use this to open Word
    VB Code:
    1. Set appWD = CreateObject("Word.Application")

    of course appWd has to be dimmed as an object!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    New Member Xee's Avatar
    Join Date
    Apr 2003
    Posts
    3
    Thanks. With a little more searching on google and perusing code on this forum, I was able to fill in the rest. To just open, save, and quit a document, here's the final code:

    VB Code:
    1. Private Sub SaveCloseDoc()
    2.     Dim objWord As New Word.ApplicationClass()
    3.     Dim MyDoc As New Word.Document()
    4.     objWord.Documents.Add.SaveAs("Hello.doc")
    5.     objWord.Quit()
    6.   End Sub

    As you may notice, I dim'd objWord as Word.ApplicationClass instead of the usual Word.Application. This was because, when doing the latter, and then trying to quit the application via objWord.Quit(), I received this error:

    "'Quit' is ambiguous across the inherited interfaces 'Microsoft.Office.Interop.Word._Application' and 'Microsoft.Office.Interop.Word.ApplicationEvents3_Event'."

    A little sleuthing in google turned up an article by Chris Kunicki (just citing my source) who figured out why this error was coming up after he received it.

    "What? I had no clue what this meant. Well, after a little digging around (about an hour later), I could see that two object members had the same method name and they were conflicting and that it only worked if I used Word.ApplicationClass. It just so happens that the Office gurus here at MSDN have documented this issue along with a number of others you need to know about in an article entitled Office XP Primary Interop Assemblies Known Issues. This article explains that "objects ending with the suffix Class should only be used when you are working with events or object members with ambiguous names. Otherwise, use objects that do not end with the suffix Class." I wish I had known that when I started."

    Well, I've figured how to open, save and close a document. Yay! Now, on to the rest . . .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width