|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err
Desktop.zip
In the attachment, I want to get Code:
Word.Range.Information(wdStartOfRangeRowNumber) Code:
''' Application-defined or Object-defined error Many thanks in advance. |
#2
|
||||
|
||||
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 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 Code:
Dim oDoc As Object, oLetter As Object Code:
Dim sPosition, sDelivery As String 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. |
#3
|
|||
|
|||
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 ''' Code:
Set oLetter = oDoc.MailMerge.Execute Pause:=False |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
FileSystemObject Compile Error: User-Defined Type Not Defined | gsrikanth | Excel Programming | 2 | 03-28-2022 06:32 AM |
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 |
Defined Views in Tasks | Inga | Office | 3 | 07-20-2005 07:10 AM |