AbeFisher
asked on
VSTO: subrouting ending unexpectedly
I have a subroutine in a Word 2007 Add-in (written in VB.NET 2008), that iterates through the footnotes collection of the documentt to edit the text of the RichText ContentControls in the footnotes by copying the text of a System.Windows.Forms.Richt TextBox control (named "rtb") to the clipboard, and then pasting it into the ContentControl.
The problem is that when I try the following:
rtb.SelectAll()
rtb.copy()
CCtl.Range.PastandFormat(W ord.WdReco veryType.w dFormatSur roundingFo rmattingWi thEmphasis )
It ends up pasting the document's selected text into the ContentControl. So I tried using the clipboard class instead, as so:
rtb.SelectAll()
Clipboard.SetData(DataForm ats.Rtf, rtb.Rtf)
CCtl.Range.Text = Clipboard.GetData(DataForm ats.Rtf)
But when I try this, for some reason as soon as the last line above is executed, the subroutine ends without finishing the For-Each loop.
Any ideas about this bizzare behavior?
The problem is that when I try the following:
rtb.SelectAll()
rtb.copy()
CCtl.Range.PastandFormat(W
It ends up pasting the document's selected text into the ContentControl. So I tried using the clipboard class instead, as so:
rtb.SelectAll()
Clipboard.SetData(DataForm
CCtl.Range.Text = Clipboard.GetData(DataForm
But when I try this, for some reason as soon as the last line above is executed, the subroutine ends without finishing the For-Each loop.
Any ideas about this bizzare behavior?
Private Sub WordDoc_ContentControlAfterAdd(ByVal NewContentControl As Microsoft.Office.Interop.Word.ContentControl, ByVal InUndoRedo As Boolean) Handles WordDoc.ContentControlAfterAdd
Dim RefTags() As String
Dim CitationList As Citations
Dim Index As Long
Dim C As Citation
Dim rtb As New RichTextBox
CitationList = New Citations
For Each fn In WordDoc.Footnotes
For Each CCtl In fn.Range.ContentControls
RefTags = Split(CCtl.Tag, "|")
Index = CitationList.Find(RefTags(0))
If Index = 0 Then
'this is the first citation of this reference
'so add it to the collection
CitationList.Add(RefTags(0), fn.Index)
C = CitationList.Item(CitationList.Count)
Else
C = CitationList.Item(Index)
C.Count = C.Count + 1
End If
'first, check if this citation is locked or not
If RefTags(2) = "True" Then 'True = "updatable"
If C.Count = 1 Then
'This is the first time this reference is cited in this document
rtb.Rtf = Reference.getReferenceString(RefTags(0), rtb, TemplateTypes.Footnote)
Else
If C.LastNoteIndex = fn.Index - 1 Then
'This is a consecutive citation, so use "ibid."
rtb.Rtf = Reference.getReferenceString(RefTags(0), rtb, TemplateTypes.Ibid)
Else
'This is a repeated, but not consecutive citation
'so use the short form.
rtb.Rtf = Reference.getReferenceString(RefTags(0), rtb, TemplateTypes.ShortForm)
End If
End If
rtb.SelectAll()
Clipboard.SetData(DataFormats.Rtf, rtb.Rtf)
CCtl.Range.Text = Clipboard.GetData(DataFormats.Rtf)
C.LastNoteIndex = fn.Index
End If
Next
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both for your comments.
CodeCruiser - I've tried this in the past, but I just ended up with the RTF codes in my Word document instead of the formatted text.
JackOfPH - I did indeed catch an exception. The message is: "One of the values passed to this method or property is incorrect." I'll have to investigate why that is the case. In the meantime, if you have any ideas about what might be wrong, please let me know!
Thanks.
CodeCruiser - I've tried this in the past, but I just ended up with the RTF codes in my Word document instead of the formatted text.
JackOfPH - I did indeed catch an exception. The message is: "One of the values passed to this method or property is incorrect." I'll have to investigate why that is the case. In the meantime, if you have any ideas about what might be wrong, please let me know!
Thanks.
ASKER
The expert pointed out that the cause of the unexpected exit, but the issue remains unresolved because I only know there is an error - not what the cause of the error is or how to fix it.
Sorry for the late response, I have a deadline to make this weekend, so I need to stay away from the internet.
Cause of the error:
I think there is an object in the clipboard that cannot be converted to the rtf object.
So, to avoid this problem:
You have to clear the clipboard first, then send the data to the clipboard then retrieve it. Just like the code below.
If this does not help you remove the error, sorry friend I do not have anymore idea.
Cause of the error:
I think there is an object in the clipboard that cannot be converted to the rtf object.
So, to avoid this problem:
You have to clear the clipboard first, then send the data to the clipboard then retrieve it. Just like the code below.
If this does not help you remove the error, sorry friend I do not have anymore idea.
Clipboard.Clear
Clipboard.SetData(DataFormats.Rtf, rtb.Rtf)
CCtl.Range.Text = Clipboard.GetData(DataFormats.Rtf)
ASKER
Thanks for checking back in, Jack, and good luck with your project.
I tried your suggestion, but unfortunately still get the same error. As a work-around I ended up just writing a routine using "selection.TypeText" to enter the text into the contentControl, formatting each segment as required, but its not the ideal solution.
Interestingly - if you check back here sometime (or for anyone else who may read this post): The regular old
I tried your suggestion, but unfortunately still get the same error. As a work-around I ended up just writing a routine using "selection.TypeText" to enter the text into the contentControl, formatting each segment as required, but its not the ideal solution.
Interestingly - if you check back here sometime (or for anyone else who may read this post): The regular old
rtb.Copyworks perfectly well from my VB.NET project, but not from within the Word2007 Add-in project. Not sure what the difference is, but there does seem to be a difference in how they operate. For example, when stepping through the code in the VB.NET app, I can see what happens in the Word document step by step, but not so in the Add-in project: there the changes only show up once the subroutine is finished.
CCtl.Range.PasteAndFormat
Maybe you are right, there is something different in word 2007 in how they operate.
Actually, I test your code in my word 2003 version and it is working. It is the reason I thought there is something in the clipboard that cannot be converted to rtf format.
Well, I have to investigate it, some other time.
Actually, I test your code in my word 2003 version and it is working. It is the reason I thought there is something in the clipboard that cannot be converted to rtf format.
Well, I have to investigate it, some other time.
Clipboard.SetData(DataForm
CCtl.Range.Text = Clipboard.GetData(DataForm
with this
CCtl.Range.Text = rtb.RTF