Vba Error - String parameter too long

Imtiaz1313

New Member
Joined
May 18, 2022
Messages
7
Office Version
  1. 2007
Platform
  1. Windows
Hello, I've been trying to use a code to search a MS word file for some words in a column in excel and get the page numbers that contains the words in it.

I found this code online. When I try to run it, it gives an error - "String parameter too long". There's probably a limit of 250 characters.

Can someone help me break down the string into small chunks of less than 250 characters ?

The code is -

Sub OpenWordDoc()
Dim wordapp As Word.Application
Dim findRange As Excel.Range
Dim findCell As Excel.Range
Dim rngFound As Word.Range

Set wordapp = CreateObject("word.Application")
wordapp.Visible = True
wordapp.Activate
wordapp.Documents.Open "filename.docx"
Set findRange = Sheet1.Range("D4:D8")
For Each findCell In findRange.Cells
Set rngFound = wordapp.ActiveDocument.Range
With rngFound.Find
.Text = findCell.Value
.Execute
End With
If rngFound.Find.Found Then
findCell.Offset(columnOffset:=1) = rngFound.Information(wdActiveEndPageNumber)
Else
findCell.Offset(columnOffset:=1) = findCell.Value
End If
Next findCell
wordapp.Quit

Set rngFound = Nothing
Set findCell = Nothing
Set findRange = Nothing
Set wordapp = Nothing
End Sub
 
I can't get any further without being able to see your Excel data and Word file. If you don't have any words greater than 20 characters then you should not have gotten this error.
Is it possible to share the files with you via mail or any other means.
I am searching the internet for last 5 days. No clue.
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Your code is looking for words in Sheet2.Range("D2:D60") but your actual word list is inSheet2.Range("D6:D1295"). That is going to cause you some problems because rows 2, 3, 4 are merged, which is never a good idea.

Second, your code only finds each term one time and inserts the page number. It sounds like you want to find all occurrences.

I changed the revised line of code I gave you back to
VBA Code:
           RngFound.Text = FindCell.Value
I also added Option Explicit and declared all your variables. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

Then I ran your code, and it ran to completion with no errors, giving correct results in column E.

Here is the file as run. Words to index sonjit 220517 0214.xlsm


Words to index sonjit 220517 0214.xlsm
CDEFGH
1
2Index Table for Page Number
3
4
5LettersWordsWords2First Supplementary BundlePleadings BundleWitness Bundle
6A
7Aback Aback N/AN/AN/A
8Able 59, 14. 60. 61. 63, 69, 70, 71, 72, 73, 79.251, 326, 331, 346, 347.131, 181.
9Absent Absent N/AN/AN/A
10Absolutely Absolutely 12, 18, 29.329, 335N/A
11Abundance Abundance N/AN/AN/A
12Accept Accept 185,
13Access Access 193.
14Accrue Accrue N/A
15Acquired Acquired 76.
16Act 784, 181, 188.
17Action Action 84, 188, 191, 192, 193, 196.
18Activate Activate N/A
19Activated Activated 63, 118.
20Active Active N/A
21Actual Actual 180.
22Add Add N/A
23Added Added N/A
24Addition Addition 38, 39, 83, 93, 130.
25Address Address 76, 83, 125, 181, 182, 183, 192.
26Addressing Addressing N/A
27Adjourn Adjourn N/A
28Adjourned Adjourned N/A
29Adjournment Adjournment N/A
30Adjourns Adjourns N/A
Indexing
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top