|
-
Apr 3rd, 2003, 04:34 PM
#1
Thread Starter
New Member
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:
Dim MyDoc As Word.Document
Documents.Add.SaveAs("Hello.doc")
Set MyDoc = Documents("Hello.doc")
Trying to paste it into VB.NET:
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
[b]Windows Form Designer Generated Code[/b]
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDoc As Word.Document
Documents.Add.SaveAs("Hello.doc")
MyDoc = Documents("Hello.doc")
End Sub
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.
-
Apr 4th, 2003, 02:47 AM
#2
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:
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!
-
Apr 4th, 2003, 12:49 PM
#3
Thread Starter
New Member
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:
Private Sub SaveCloseDoc()
Dim objWord As New Word.ApplicationClass()
Dim MyDoc As New Word.Document()
objWord.Documents.Add.SaveAs("Hello.doc")
objWord.Quit()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|