Gary Dale wrote:
Andrew Douglas Pitonyak wrote:
Gary Dale wrote:
Sorry if this is pretty basic but I'm trying to print a form letter
and I wanted to customize it a little based on the values of the
data in the mailing list. If a field contains a value, I add an
"attn:" line if I have a name or a title to send to the attention of.
The macro is assigned to the Print Document event, which seems to be
correct because it is getting called once per database record - and
failing each time with the same error.
Anyway, below is my code - it's failing on the "IF Found THEN" line
with a "BASIC runtime error. Incorrect property value" error.
The idea, as I said, is straightforward: search for the word "Dear"
which starts the letter, then add an "Attn:" paragraph above it if
there is someone it should be directed to. I'm just trying to get
the adding of the "Attn:" working with this code. I'll add the
fields later if I can get this working. :)
Any hints?
Sub Main
Dim Doc As Object Dim Cursor As Object
Dim SearchDesc As Object
Doc = StarDesktop.CurrentComponent
SearchDesc = Doc.createSearchDescriptor()
SearchDesc.searchString = "Dear"
SearchDesc.searchCaseSensitive = True
SearchDesc.searchWords = True
SearchDesc.searchSimilarity = False
Found = Doc.findFirst(SearchDesc)
IF Found THEN
Cursor = Doc.Text.createTextCursor
IF NOT Doc.Text.isStartOfSentence() THEN
Doc.Text.gotoStartOfSentence(FALSE)
END IF
Cursor.String = "Attn:"
Doc.Text.insertControlCharacter(Cursor,
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
END IF
END IF
End Sub
What happens if you run this instead?
Sub Main
Dim Doc As Object Dim Cursor As Object
Dim SearchDesc As Object
Doc = StarDesktop.CurrentComponent
SearchDesc = Doc.createSearchDescriptor()
SearchDesc.searchString = "Dear"
SearchDesc.searchCaseSensitive = True
SearchDesc.searchWords = True
SearchDesc.searchSimilarity = False
Found = Doc.findFirst(SearchDesc)
IF Found THEN
REM I am a comment so we do nothing
END IF
End Sub
What happens if you place a line as:
Option Explicit
As one of the first lines in the file (a line by itself before any
variables or subroutines are defined)?
I have some more thoughts, but I really need to go to sleep now.
I am interested in what you are doing. I have some comments related
to using the standard document Text object, it does not strike me as
safe, but I must explain that later.
Commenting out the code in the IF statement had no affect.
OK, this indicates that it is complaining about your IF statement. You
need to check Found against NULL.
Turning on Option Explicit and declaring Found to be Boolean moved the
error up to the Found = Doc.findFirst line. This suggests that
findFirst is returning something other than a Boolean. The only way I
think that could happen is if findFirst isn't a method of Doc - it may
be returning a null value.
Found is NOT declared and it is NOT boolean.
One thing I'm not clear on in printing a form letter is which file is
the current component? How do I distinguish the database row from the
text document? Or to put it another way, in the code above, how do I
ensure that Doc = StarDesktop.CurrentComponent is actually picking up
the text document?
If you are in the Basic IDE then you do not want CurrentComponent. I
usually use ThisComponent because of that.
I made a few changes, see if they work for you. But, now I need to get
ready for church.
REM ***** BASIC *****
Option Explicit
Sub Main
Dim Doc
Dim Cursor
Dim SearchDesc
' Declare the variable Found.
Dim Found
Dim oText
' I am testing in the IDE so I can not use the
' CurrentComponent
'Doc = StarDesktop.CurrentComponent
Doc = ThisComponent
SearchDesc = Doc.createSearchDescriptor()
SearchDesc.searchString = "Dear"
SearchDesc.searchCaseSensitive = True
SearchDesc.searchWords = True
SearchDesc.searchSimilarity = False
Found = Doc.findFirst(SearchDesc)
' Check against NULL, it is NOT boolean
' Also, you probably want to find all instances
Do While Not IsNull(Found)
' Use the cursor's text object in case the cursor
' is in a text table or frame.
oText = Found.getText()
'Create the text cursor where the text was found.
Cursor = oText.createTextCursorByRange(Found)
' The text object is NEVER at the start of a sentence.
' The cursor however, might be.
' Also, you can move the cursor, but NOT the text object.
IF NOT Cursor.isStartOfSentence() THEN
Cursor.gotoStartOfSentence(FALSE)
END IF
' Although this will work, it will NOT move the cursor
' So, when you insert the new paragraph, it will be BEFORE
' the text Attn:
'Cursor.String = "Attn:"
' Do this instead.
oText.insertString(Cursor, "Attn: ", False)
oText.insertControlCharacter(cursor,
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
'Do the search again to find the next object...
Found = Doc.findNext(Found, SearchDesc)
Loop
End Sub
--
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
My Book: http://www.hentzenwerke.com/catalog/oome.htm
Info: http://www.pitonyak.org/oo.php
See Also: http://documentation.openoffice.org/HOW_TO/index.html
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]