Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-09-2012, 07:03 AM
tinfanide tinfanide is offline Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Windows 7 64bit Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Office 2010 32bit
Expert
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err


Views: 8 Size: 46.6 KB">Desktop.zip


In the attachment, I want to get
Code:
Word.Range.Information(wdStartOfRangeRowNumber)
but it returns an error
Code:
''' Application-defined or Object-defined error
Please kindly open the xlsm file in the zip file and run the sub t3().
Many thanks in advance.
Reply With Quote
  #2  
Old 06-09-2012, 09:13 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Windows 7 32bit Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Your code has a lot of problems. You are using late binding to automate Word (ie. You do not have the MS Word library referenced) which means that Excel does not know what all the Word enumerated constants in your code are.

Because you have not enforced variable declaration, the VBA compiler scans your code and creates Variant variables for anything in your code which it does not recognise. So, wdStartOfRangeRowNumber is implicitly a Variant with a value of Empty.

To fix your code you should follow best coding practices and put an Option Explicit statement at the top of your code module (you should always do this). Then go to Debug > Compile to identify your undeclared variables such as oMatch1. You should declare these variables as appropriate types. As you repeat this process, you will also get compile errors for all those word enumerated constants which are unrecognised. For these you need to replace them with the numbers they represent. For example, wdStartOfRangeRowNumber should be replaced by 13. Since 13 is a pretty meaningless number, what I do is declare constants for them. eg
Code:
Const lngSTART_OF_RANGE_ROW_NUMBER As Long = 13
Then you can use this constant in your code. To get the correct values you can look them up in the MS Word VBA Object Browser.


The last point I'll make for now is a problem with the way you have declared some of your variables. Take this example:
Code:
Dim oDoc, oLetter As Object
In VBA, if you declare your variables like this, oDoc will be a Variant and not an Object. If you want to declare them both on the same line then you need to use:
Code:
Dim oDoc As Object, oLetter As Object
Here's another one:
Code:
Dim sPosition, sDelivery As String
In this case, sPosition will be a Variant. To declare them both as String types use:
Code:
Dim sPosition As String, sDelivery As String


I think that's enough for you to work on for now. Once you've fixed those issues we can then address the unqualified Word references in your code.
Reply With Quote
  #3  
Old 06-09-2012, 10:19 AM
tinfanide tinfanide is offline Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Windows 7 64bit Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Office 2010 32bit
Expert
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Thumbs up

Colin, really thank you for your walkthrough. Fix a lot and learn lots of good practices when coding VBA. The most troublesome part would be looking up in the enumeration references when I use late binding of Word.

By the way, I forgot one question:
Code:
With oDoc
    With .MailMerge
        .MainDocumentType = 0
        .OpenDataSource Name:=ThisWorkbook.FullName, SQLStatement:="SELECT * FROM `Sheet1$`"
        .DataSource.ActiveRecord = Selection.Rows(1).Row - 1
        .ViewMailMergeFieldCodes = False
        With .DataSource
 
            Do
                .FirstRecord = .ActiveRecord
                .LastRecord = .ActiveRecord
'''
                oDoc.MailMerge.Destination = 0
                oDoc.MailMerge.Execute Pause:=False
 
'''                
                Set oLetter = oWord.ActiveDocument
'''
I have long wondered how I could do something like this:
Code:
Set oLetter =  oDoc.MailMerge.Execute Pause:=False
Though I have set the object oLetter = the new document created by MailMerge, it refers to the actived ocument at the moment. I don't know if it is true or not. It is not safe when I refer to ActiveDocument all the time. The active document may not be the mail merge created document by accident.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err FileSystemObject Compile Error: User-Defined Type Not Defined gsrikanth Excel Programming 2 03-28-2022 06:32 AM
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Application-defined or Object-defined error Manit Excel Programming 4 12-08-2011 07:35 PM
Compile error: sub or function not defined.. xena2305 Excel Programming 0 08-02-2011 10:17 AM
TOC printing Error Bookmark not Defined techexpressinc Word 0 12-14-2008 05:24 PM
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err Defined Views in Tasks Inga Office 3 07-20-2005 07:10 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft