PDA

View Full Version : [SOLVED:] Problem with populating content control



Jfp87
11-14-2014, 12:39 AM
Guys,

Can anyone see a problem with this particular snippet of code? (Word 2010)

Case "Subject"
For N = 1 To 8
For P = 1 To 4
If strFileName = arrDoc(N) & " (" & P & ")" & ".docx" Then
cc.Range.Text = arrSub(N, P)
Exit For
Exit For
End If
Next P
Next N

arrSub(1, 1) = frmSubHeadings.txtRisk1.Value
arrSub(1, 2) = frmSubHeadings.txtRisk2.Value
arrSub(1, 3) = frmSubHeadings.txtRisk3.Value
arrSub(1, 4) = frmSubHeadings.txtRisk4.Value
...
etc.

"Subject" is the only content control within my document which won't update. When the code is run, the cc doesn't get populated with an empty string - it just remains as the original document property placeholder text. I am wondering if there is a glaring error which I have missed.

Cheers,
Joe

gmaxey
11-14-2014, 08:34 AM
Add a Stop before cc.Range.Text and run the code to see if the condition you have established is ever met:

Stop
cc.Range.Text = arrSub(N,P)

If the code runs to completion then the condition is was not met. If the code stops then maker sure arrSub(N, P) is not returning null string.

Jfp87
11-17-2014, 04:08 AM
Thanks Greg,

Your post helped me find a solution - one which I dont quite understand.

I am comparing document names in a folder, to document names in an array. If they match, the code retrieves the appropriate sub-heading and inserts it into the document via the cc.

My original array: arrDoc = split("N|L|M|P|Z", "|")
My new array: arrDoc = split("|N|L|M|P|Z", "|")

I added an extra delimiter at the start. I dont understand why this fixes the problem. As far as I know, it shouldn't have to be there.

Before the extra delimiter, and using ideas your post gave me, I decided to print the elements of arrDoc(N) to the screen as the code was looping. I found the code was starting on the 2nd element and not the 1st. The additional delimiter fixed this.

I have 'Option Base 1' in the general declarations area....not quite sure what's going on.

Joe

gmaxey
11-17-2014, 05:52 AM
Joe,

Split returns a zero based array regardless of the Option Base statement.


Option Base 1
Sub Dem0()
Dim lngIndex As Long
Dim arrDoc() As String
arrDoc = Split("N|L|M|P|Z", "|")
For lngIndex = 0 To UBound(arrDoc)
Debug.Print arrDoc(lngIndex)
Next lngIndex
End Sub

Jfp87
11-18-2014, 01:48 AM
Thanks Greg,

That makes sense now.

Joe