View Full Version : Solved: Populating Array (Extreme Newbie)
sjpeters75
12-30-2012, 11:40 PM
Hello,
I'm in my first few days of learning VBA and can't figure out how to fix the "Type Mismatch error" caused, I think, by the line:
paragraphArray(i) = Paragraphs(i)
My intent is to iterate through each paragraph in a document and place that paragraph in an array. My code is lised below. Many thanks!
Sub Test()
Dim paragraphCount As Integer
paragraphCount = ActiveDocument.Paragraphs.Count
ReDim paragraphArray(paragraphCount) As String
Dim i As Integer
For i = 1 To UBound(paragraphArray)
paragraphArray(i) = Paragraphs(i)
Next i
End Sub
Paul_Hossler
12-31-2012, 06:58 AM
1. If you use the [ VBA ] button, the code formats
2. I think you're missing 'Set' to assign objects to an object variable (i.e. 'Paragraph'), assuming that's what you want. If you're looking to get the Text into the array element, that's something different
Sub Test()
Dim paragraphCount As Long
paragraphCount = ActiveDocument.Paragraphs.Count
ReDim paragraphArray(1 to paragraphCount) As Paragraph
Dim i As Long
For i = 1 To UBound(paragraphArray)
Set paragraphArray(i) = Paragraphs(i)
Next i
End Sub
Paul
sjpeters75
12-31-2012, 07:47 AM
Thanks, Paul! (For the proper posting tip, as well)... I don't understand the difference between getting the Paragraph object into the array element and "getting the Text into the array element." Thanks a ton for the clarification.
Regards,
steve
gmaxey
12-31-2012, 12:34 PM
sjpeters,
I don't have any formal schooling myself so take or leave what I have to say:
First, you should declare all of your variables. Here are two variations of your code. The first uses a variable string (you don't need Set) the second uses a object variable (Paragraphs are objects) and you do need Set.
Option Explicit
Sub TestI()
Dim lngIndex As Long
Dim lngCount As Long
Dim arrParaText() As String
lngCount = ActiveDocument.Paragraphs.Count
ReDim arrParaText(1 To lngCount)
For lngIndex = 1 To UBound(arrParaText)
arrParaText(lngIndex) = ActiveDocument.Paragraphs(lngIndex).Range.Text
Next lngIndex
For lngIndex = 1 To UBound(arrParaText)
Debug.Print arrParaText(lngIndex)
Next lngIndex
End Sub
Sub TestII()
Dim lngIndex As Long
Dim lngCount As Long
Dim arrParaText() As Object
lngCount = ActiveDocument.Paragraphs.Count
ReDim arrParaText(1 To lngCount)
For lngIndex = 1 To UBound(arrParaText)
Set arrParaText(lngIndex) = ActiveDocument.Paragraphs(lngIndex)
Next lngIndex
For lngIndex = 1 To UBound(arrParaText)
arrParaText(lngIndex).Range.Font.Color = wdColorRed 'Note color change in document.
Debug.Print arrParaText(lngIndex).Range.Text
Next lngIndex
End Sub
sjpeters75
12-31-2012, 01:20 PM
Wow,
Thanks, Greg! I understand. Really appreciate the help!
Best,
steve
gmaxey
12-31-2012, 01:27 PM
Steve,
If you really understand then you really are off to the races. I was shown something similar a dozen times or more before I really understood ;-)
Good luck.
fumei
12-31-2012, 05:36 PM
And as a third option (in addition to strings, and paragraph objects), you could use Ranges.
It really depends on what is the intended purpose of the array items.
sjpeters75
01-07-2013, 01:10 AM
Thanks everybody!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.