Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

User-defined tye not defined error

Posted on 2009-02-22
15
Medium Priority
?
434 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:9XqUwH3S
  • 9
  • 6
15 Comments
 

Author Comment

by:9XqUwH3S
ID: 23707181
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 1500 total points
ID: 23707213
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 1500 total points
ID: 23707503
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:9XqUwH3S
ID: 23707772
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 1500 total points
ID: 23709271
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
 

Author Comment

by:9XqUwH3S
ID: 23718059
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
 

Author Comment

by:9XqUwH3S
ID: 23718074
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 1500 total points
ID: 23719516
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
 

Author Comment

by:9XqUwH3S
ID: 23719657
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1500 total points
ID: 23719826
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
 

Author Comment

by:9XqUwH3S
ID: 23722350
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
 

Author Comment

by:9XqUwH3S
ID: 23722446
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
 

Author Comment

by:9XqUwH3S
ID: 23740558
I haven't forgotten. Things are a little crazy over here.
Let me know if you think I should close the question.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 23740751
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
 

Author Closing Comment

by:9XqUwH3S
ID: 31549889
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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