User-defined tye not defined error

RE: Lines 7 - 9

This code supposedly worked, but for which versions of outlook and word, unknown.

I'm getting"User-defined type not defined". How should "Set objWord = New word.Application" be defined?

Thank you for your time and effort.

Also, Doesn't Target App and TargetDoc need dimensioned?
Sub Transfer()
 
' Now we open the canned response
Dim objWord As Application
 
' We need an easy pointer back to our new e-mail message, such as:
Set TargetApp = Application.ActiveInspector.WordEditor
Set TargetDoc = TargetApp.Windows(1).Selection
Set objWord = New word.Application
 
 
objWord.Visible = True
objWord.Documents.Open "C:\Documents and Settings\Steven Boyd\Desktop\Transfer.docx", , , True
 
' Select and Copy
objWord.Activate
Selection.WholeStory
Selection.Copy
Selection.End = True
 
' Then we just need to close the canned response
objWord.Activate
objWord.Quit
 
' Now we return focus to our e-mail message and paste the text
TargetDoc.Paste
 
' Clean up
Set objDoc = Nothing
Set objSel = Nothing
Set TargetApp = Nothing
Set TargetDoc = Nothing
End Sub

Open in new window

9XqUwH3SAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
You are using COM (Component object model), aka OLE Automation, now just Automation.

You can use this to link between most Microsoft applications, and many others.

Your code can use early or late binding.

In late binding, all the target application's object are, if declared, declared as objects:
Dim wrdApp as Object
Dim wdrDoc as Object

The advantage in this method is better for inter-version compatibility.

In early binding, you need to set a reference to the appropriate library. The main advantages of early binding are:

Full access to the help system via F1 key.
'Intellisense' - a list of relevant properties pops up when the "." as typed after an object variable.

Constant names (e.g. wdDoNotSaveChanges) instead of numbers can be used.

A bookmark is a place marker. In Word it represents a position or a stretch of a document.
0
 
9XqUwH3SAuthor Commented:
Oh yes, this application is designed to take a Word document on the desktop and lift the formatted text and insert it into Outlook Email via a macro.
0
 
GrahamSkanRetiredCommented:
The code is written for early binding, so you need to set a reference to the Microsoft Word Object Library (Tools/References in the VBA editor).
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
GrahamSkanRetiredCommented:
You don't need to Dim everything, but it is a good idea.

If you have Option Explicit  in each module, the complier will warn you about any variables that have not been declared. You will then catch any misspelled variables.

You can force the application to put Option explicit in each new module by setting  'Require Variable declaration' in VBA Options - accessed via the Tools menu.
0
 
9XqUwH3SAuthor Commented:
Would line 9 be a logical, numeric or string data type I would be assigning? What am I binding to, the actual document or to the Microsoft Word Object Library? I'm declaring the variable to be an application, so what more does it need to know?
 
 
0
 
GrahamSkanRetiredCommented:
Sorry. The code colouring on this site is always wrong, but  still find it confusing.

Line 4:
Dim objWord As Application
is coloured green as if it were a comment so I didn't read it properly. It needs to read Word.Application
Option Explicit
 
Sub Transfer()
 
' Now we open the canned response
Dim objWord As Word.Application
Dim objWordDoc As Word.Document
' We need an easy pointer back to our new e-mail message, such as:
Set TargetApp = Application.ActiveInspector.WordEditor
Set TargetDoc = TargetApp.Windows(1).Selection
 
Set objWord = CreateObject("Word.Application") 'preferred to .New method
objWord.Visible = True
Set objWordDoc = objWord.Documents.Open("C:\Documents and Settings\Steven Boyd\Desktop\Transfer.docx", , , True)
objWordDoc.Range.Copy
' Then we just need to close the canned response
objWordDoc.Close wdDoNotSaveChanges
objWord.Quit
 
' Now we return focus to our e-mail message and paste the text
TargetDoc.Paste
 
' Clean up
Set objDoc = Nothing
Set objSel = Nothing
Set TargetApp = Nothing
Set TargetDoc = Nothing
End Sub

Open in new window

0
 
9XqUwH3SAuthor Commented:
Wow! You really outdid yourself, GrahamSkan!
If I had known how much work that code needed, I wouldn't have troubled you, so. Really, I feel embarrassed.
Can you ellaborate about the following 4 questions?
Line 10: Is "Windows(1)"  an array?
Line 6, 7: I see you distinguished between Word.Application and WordDoc.
Where did you Dim "objSel", since this is Option Explicit? Don't you have to dimension any variable that you 'Set'?
Line 14: the commas, what are those. This looks like stuff I see in  a macro. Do the commas represent arguments that are to follow?
0
 
9XqUwH3SAuthor Commented:
Curious, I'm getting line 7 "objWord As Word.Application" as User-defined variable undefined.
I had the same problem. What and why do you think it has a problem with line 7?
0
 
GrahamSkanRetiredCommented:
Line 10 is your own code which I haven't modified. Windows(1) is a member of a Collection. A collection is an array of objects.

I assumed that you have a Dim for objSel somewhere else in your code.

Line 14: the commas were in your original code. It calls a Word command that can take several parameters (arguments), most of them optional. Only the first and the fourth are needed, so the commas indicate that 'True' is the fourth parameter.
Alternatively, arguments can be named. However, it turns out that the fourth parameter is 'Password', so the original code was probably wrong.

Set objWordDoc = objWord.Documents.Open(FileName:= "C:\Documents and Settings\Steven Boyd\Desktop\Transfer.docx", Password:= True)

Re the 'User-defined variable undefined' message. Have you set a reference to the Microsoft Word Object Library?
0
 
9XqUwH3SAuthor Commented:
No. I hadn't done that because I thought that was handled in the first part of  LIne 17.
I found "Word Application Object Model Reference (examples) at http://msdn.microsoft.com/en-us/library/bb288731.aspx.
Am I looking to establish a 'bookmark'? Is that what I'm trying to do to accomplish this?
0
 
9XqUwH3SAuthor Commented:
This is a great learning experience.
Let me see if I can work on the next errors.
Thank you. I'll get back to you.
0
 
9XqUwH3SAuthor Commented:
I want to take some time to really study your responses.
It might be a short while before I get back.
The VBA books I own just don't seem to explain things in an intuitive and natural format.
Might you have any recommendations for books or is this the normal way by asking questions and hit and miss?
0
 
9XqUwH3SAuthor Commented:
I haven't forgotten. Things are a little crazy over here.
Let me know if you think I should close the question.
0
 
GrahamSkanRetiredCommented:
I see that I didn't answer your penultimate comment. That's because I have never read a book on VBA. I have some on VB.Net, but I'm not getting on very well with it, and they aren't any help at all with legacy VB.

It is expected that you close the question when you consider that the original question has been answered.
0
 
9XqUwH3SAuthor Commented:
Penultimate: is that a property or a method, hmmh...
Your answers were quite educational.
I think I shall bookmark this project until I get more experience.
Nice job.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.