problem in changing the text of sentences before tables

I am developing a word automation application. In a method of mine, I change 
the text of some sentences of an opened word file, but the problem is when I 
change the text of a sentence which located before a table, it will be moved 
to the first cell of the table. My code is as follow:

void myMethod( long startingSentenceNumber, const char *toBeSearched, const 
char *replacement,  bool replace )
{
Range currentSentenceRange;
Selection sentenceSelection;	
Sentences sentencesList = m_document.GetSentences();
long sentencesCount = sentencesList.GetCount();  
CString replacementCStr(replacement);
	
	for (long newIndex = startingSentenceNumber; newIndex <= sentencesCount; 
newIndex++)
	{
		currentSentenceRange = sentencesList.Item(newIndex);
		CString currentSentenceText = currentSentenceRange.GetText();
		const char *temp = currentSentenceText;
		std::string currentString(temp);
		std::string toBeSearchedString(toBeSearched);
		
		if (/*some condition*/)
		{
			std::string endCharacters = currentString.substr(strlen(toBeSearched), 
currentString.size() - strlen(toBeSearched));
			replacementCStr += endCharacters.c_str();
			if (replace)
			{	
				currentSentenceRange.SetText(replacementCStr);
			}
			foundedSentenceIndex = newIndex;
			break;
		}
		
		currentSentenceRange.ReleaseDispatch();
		sentenceSelection.ReleaseDispatch();
	}
	sentencesList.ReleaseDispatch();
}

How could I solve this problem?

0
Utf
2/20/2010 1:00:01 PM
word.vba.general 1023 articles. 1 followers. Follow

1 Replies
682 Views

Similar Articles

[PageSpeed] 23

Hi,

I can reproduce your problem by placing the cursor in the sentence 
immediately before a table and running the following macro, which replaces 
the text in a Range object just like your code does.

Sub ChangeSentenceBeforeTable1()

    Dim MyText As String
    Dim MyRange As Range

    Set MyRange = Selection.Sentences(1)
    MyText = MyRange.Text + " Added text."
    MyRange.Text = MyText

    Set MyRange = Nothing
End Sub

The modified sentence ends up in the table because inserting additional text 
into the range in this way extends the end of the Range object into the 
table. I found that if I place the cursor in the last sentence of a paragraph 
immediately before another paragraph, the additional text is also be added to 
the beginning of the next paragraph.

I found that I can correctly insert additional text at the end of a sentence 
anywhere, including at the end of a sentence before a table and at the end of 
the last sentence in a paragraph by placing the cursor in the sentence and 
running the following macro. 

Sub ChangeSentenceBeforeTable2()

    Dim MyText As String
    Dim MyRange As Range

    Set MyRange = Selection.Sentences(1)
    MyText = MyRange.Text + " Added text."
    MyRange.Collapse Direction:=wdCollapseEnd
    MyRange.MoveStart wdCharacter, -1
    MyRange.InsertBefore " Added text."
    
    Set MyRange = Nothing
End Sub

The solution for you is to modify your code so that it collapses the Range 
object for the sentence to its end, moves the end back one position (before 
the end of the paragraph), and inserts the additional text before the 
modified Range object using the InsertBefore method.

-- 
Hope this helps,
Pesach Shelnitz
My Web site: http://makeofficework.com 


"mjlaali" wrote:

> I am developing a word automation application. In a method of mine, I change 
> the text of some sentences of an opened word file, but the problem is when I 
> change the text of a sentence which located before a table, it will be moved 
> to the first cell of the table. My code is as follow:
> 
> void myMethod( long startingSentenceNumber, const char *toBeSearched, const 
> char *replacement,  bool replace )
> {
> Range currentSentenceRange;
> Selection sentenceSelection;	
> Sentences sentencesList = m_document.GetSentences();
> long sentencesCount = sentencesList.GetCount();  
> CString replacementCStr(replacement);
> 	
> 	for (long newIndex = startingSentenceNumber; newIndex <= sentencesCount; 
> newIndex++)
> 	{
> 		currentSentenceRange = sentencesList.Item(newIndex);
> 		CString currentSentenceText = currentSentenceRange.GetText();
> 		const char *temp = currentSentenceText;
> 		std::string currentString(temp);
> 		std::string toBeSearchedString(toBeSearched);
> 		
> 		if (/*some condition*/)
> 		{
> 			std::string endCharacters = currentString.substr(strlen(toBeSearched), 
> currentString.size() - strlen(toBeSearched));
> 			replacementCStr += endCharacters.c_str();
> 			if (replace)
> 			{	
> 				currentSentenceRange.SetText(replacementCStr);
> 			}
> 			foundedSentenceIndex = newIndex;
> 			break;
> 		}
> 		
> 		currentSentenceRange.ReleaseDispatch();
> 		sentenceSelection.ReleaseDispatch();
> 	}
> 	sentencesList.ReleaseDispatch();
> }
> 
> How could I solve this problem?
> 
0
Utf
2/21/2010 9:35:01 AM
Reply:

Similar Artilces:

Problems installingOutlook 2002 on new computer
I just got a new computer at work and I'm trying to install Outlook 2002 so I can "Active Sync" my Ipaq 3800 w/ this new computer. Outlook 2002 seemed to installed just fine but when I try to open it, it tells me I need to create a user profile. I have done this about 10 times now and the system doesn't recognize any profiles. It also give a 2nd error message that says that "the server is not available". I am not trying to hook up to a server so I'm wondering if there is a setting somewhere that is incorrect and Outlook is looking for a server. Can a...

Changing start date
Scenario: In OL 2003, I have an event that's two weeks long, starting June 1 and ending June 14. I want to change the start date to May 27 but leave the end date alone. If I change the start date, OL automatically changes the end date to preserve the two-week duration. Is there a way to change the start date without affecting the end date? Richard Evans <infodex@mindspring.com> wrote: > In OL 2003, I have an event that's two weeks long, starting June 1 and > ending June 14. I want to change the start date to May 27 but leave > the end date alone. If I change the s...

Open relay problem Exchange 2003
We encountered some big problems when first turning up a new SBS2003 Server. We still have a remaining issue that I can't seem to get an answer to. I recently installed SBS 2003 on a new server and Exchnge installed as an open relay by default. Within hours of being online thousands upon thousands of SPAM email was relaying (or trying to relay) through the server. In addition, one of the initial Windows update may have corrupted the registry hive. Ended up reloading this new machine twice with in a very short time and both times Exchange installed as an open relay. Once the fiasco...

Can you ignore table validation on query append?
Hello, I have a table that has validation: IIf([Document 1] Is Null,[Document 2] Is Not Null And [Document 3] Is Not Null,IIf([Document 1] Is Not Null,[Document 2] Is Null And [Document 3] Is Null)) This is because when my user fills out their form, they must select one choice from List A OR one item from List B and one item from list C. My problem is that I have an append query to add any new hires to the table in an autoexec. The append query is failing because I am not updating any data for the above Documents. Is there anyway for me to get around the table validation on my append? Any ...

Need Help With Text To Time Formula
I receive a text file that contains several "time" entry columns formatted as text. The cells contain values such as "8:30AM" (no space between the zero and "A"). I need to convert these to time formats so that I can subtract multiple columns from one another. The first thing I need to do is put a space between zero and "A" (or "P" for PM) and I need to format the column as "hh:mm a/p". I have tried several methods and none seem to work (I have thousands of cells to process). Text to Columns is not an option because there ...

cells stop accepting text after 1801 characters
Someone in my office tried inserting text characters into a cell in a worksheet. After 1801 characters, it quit entering text. The cell is formatted as a merged cell (merged to what, I can't tell) with text wrap, and is locked. Why can't my person enter more charcters. You can have a cell that contains about 32k worth of characters, but left to its own devices, excel will only show about 1000. You can add alt-enters to force new lines within your cell and you can see more. But with mergedcells, you'll have to adjust the rowheight yourself--it won't react nicely to auto...

Can't display all text in cell
I'm trying to display text in a cell. I've got Wrap Text on, and I hav several paragraphs of text. All of it appears in the formula bar, bu not in the cell displayed on the screen. And yes, I increased the ro height, but it still doesn't show. Any ideas? Thank -- Message posted from http://www.ExcelForum.com I am having the same problem. Apparantly there is a 1024 character limitation per cel in excel. The only advice I have received so far, which does work, is to press alt+enter to create a line break. If you find another work around, I would be interested. Hi besid...

Worksheet Problem 01-04-10
So I have a couple of issues. I go to open up a file today and try to unhide two worksheets that were hidden, and they are gone. I can still see them in VBA, but cannot open them and they are not visible at all in excel. I also am having trouble inserting a new worksheet. All the options are they, but I am not able to select insert. Any help would be appreciated. CHeck to see if workbook protection is activated. That would prevent you from being able to add new sheets (or unhide). Also, in VBE, check to see if the missing sheets have a visible status of "xlSheetVe...

Excel document changes format
Hi, I have an Excel document that changes format unexpectedly. It seems to change the format of the document eventhough i saved it. I have to resize the windows of the bar graphs, etc. It just doesn't save it the way i want. I re-installed MSOffice and re-did the NT profile, but still persists. Any ideas? Thanks. Excel does seem to have problems if the screen zoom is anything othe than 100%. Usually changing to 100 and back resets he screen display -- Message posted from http://www.ExcelForum.com ...

Managing Data Table
Hi, I need help to wrap the tex in Data Table. If any one knows how to do it, please let me know. Thanks for your help in advance. Yogesh Hi Yogesh What version of PowerPoint are you using? What kind of table is it and how did it get there (e.g. insert -> table, or a table connected with a graph)? If it's a graph, is it an Excel graph or an MSGraph graph? Lucy -- Lucy Thomson PowerPoint MVP MOS Master Instructor www.aneasiertomorrow.com.au "Yogesh" <Yogesh@discussions.microsoft.com> wrote in message news:F68FDA89-6289-4341-8886-54C580...

Hotkeys for text & background coloring
Hi. Does anyone know the hotkeys for text coloring (font color) & background coloring (fill color)? Eg: First you need to select the font color & fill color in the related toolbar incons. - When I highlight "I am great", then press "Ctrl+O" for example, the text will be colored as what it's displayed in the "font color" toolbr icon. - The simliar way applies to background coloring If the above is not available, any workaround is still apreciated. -- Additional information: - I'm using Office XP - I'm using Windows XP You could use a sma...

System restore problem
Windows XP Home SP3. I'm trying to do a system restore, by going to Start | Accessories | System Tools | System restore. I select a date, and click next, but nothing happens. I've tried several different dates with no luck. Am I screwed or is there a way to manually force a system restore without using this wizard? Thanks in advance for any help. Craig Try SR in Safe Mode. Why do you want to run SR? Are you having a problem? "Craig Schiller" <cschiller1@earthlink.net> wrote in message news:u9722H$sKHA.5276@TK2MSFTNGP02.phx.gbl... > Win...

y-axis problems
Hello, When I create a bar chart (in powerpoint or excel) on my PC, the last letter on the X axis label gets cut off when I rotate the label to run sideways. It works fine on my laptop, but if I open the file on my PC and click on the chart area, the last letter disappears. This is a very common experience. Some uses add "hard-spaces" which is ANSI 0160 - hold ALT down and type 0160 on numeric pad Other add some nonsense text (three periods) and format it with a color and background to make invisible. Best wishes - Bernard Liengme www.stfx.ca/people/bliengme remove CAPS ...

counting same text from different cells
Hi I was Trying to count text such as "p" for pass from different cell in excel but i am not sure what is the formula for that. It will be really helpful if u could send me the solution thank you om Hi =COUNTIF(A1:A100,"p") -- Regards Frank Kabel Frankfurt, Germany "om" <om@discussions.microsoft.com> schrieb im Newsbeitrag news:96B25715-6B88-40F8-8360-9602A05E3B30@microsoft.com... > Hi I was Trying to count text such as "p" for pass from different cell in > excel but i am not sure what is the formula for that. > > It will be rea...

VFW webcam
Hi, I'm using Video For Windows to capture images from creative webcam in my application. This is giving me image in size 640x480. However, i need image in landscape resolution where width is less than height. I tried to squeeze the captured image but its distorting it badly. Any ideas how can we capture images from webcam in desired resolution? Thanks, neo see the amcap sample in DirectShow SDK -- Regards Sheng Jiang Microsoft Most Valuable Professional in Visual C++ http://www.jiangsheng.net http://blog.joycode.com/jiangsheng/ <dhapola@yahoo.com> ???? news:1160125564.284431....

[Bitmap] problem loading from array
Hello! I'm trying to load a bitmap from an array using the following code : CBitmap bmp1; bmp1.CreateBitmap(im_data.width,im_data.height,1,24,NULL); bmp1.SetBitmapBits(im_data.length,im_data.data_buf); CClientDC dc(this);//CPaintDC dc(this); CDC bmDC; bmDC.CreateCompatibleDC(&dc); CBitmap *pOldbmp = bmDC.SelectObject(&bmp1); dc.BitBlt(50,150,im_data.width,im_data.height,&bmDC,0,0,SRCCOPY); bmDC.SelectObject(pOldbmp); where im_data.width is the width of the image where im_data.height is the height of the image where im_data.length is the number of bytes in the image ...

Problem using a report from Access2003 to Access2007
I am trying to use a database created with Access 2003 in Access 2007 but have come up with a problem. The database contains a report which produces invoices for sales between two dates. When the report is run the user is asked to input sale date From and To (this is set up in the Invoice Query), Invoice Date and Invoice Start Number - these two are set up as text boxes on the report. Invoice Date conrol source has [Enter Invoice Date] and Invoice Start Number has [Page]+[Invoice Start Number]-1. This works fine in Access 2003 and the invoices come out correctly but in 2007 invoice ...

Getting Combo boxes to change options based on other Combo boxes.
Using Excel 2007. I am trying to create either Form Control Combo Boxes or ActiveX Combo Boxes that will change the selectable options based on selections made in previous combo boxes. I am having a hard time finding any information that will work for me. Can someone tell me how to make 5 combo boxes change available listed options when specific options are selected in another combo box? Please? I've been working on this for a very long time without success. I've tried using Data Validation, but my formulas are too long for all the different possible combinations. You are...

Importing a folder of text files
Hi I have a folder of text files (a few thousand) and I want to either take the relevant bits of them into Access (one per record, memo field) based on what's in the file or to import all of them into Access and then delete the irrelevant bits. As an example, I want to grab the data after <div class="ext_description"> (which appears in all of the files (they were html)) and put it into records in a table. I don't know whether to remove the irrelevant bits from the files before or after import. Thanks for any help you can suggest! On Feb 24, 12:56=A0pm, Andy &...

change font size of first character
I have a template that allows users to print business cards. It displays a form where they enter their name and three other lines of text. I would like to increase the font size of the first character in each word if the user enters the text in upper case. The code I'm using to capture the text from the form is: With ActiveDocument For intLoop1 = 1 To 10 .Bookmarks("Name" & Right("0" & intLoop1, 2)).Range.InsertBefore txtName .Bookmarks("Title" & Right("0" & intLoop1, 2) & "a").Range.InsertBef...

Need help with cursor spacing problem
> This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --B_3173510125_666133 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: 8bit Hello: I�m trying to fill in a form that�s MS Word based. I�m using MS Office X on a Mac G4 with system 10.3.4. When I type, the cursor seems to have an extra space between the last letter and the place where I want to put a new one. In other words, I�m unable to align the cursor exactly where I want it to go. It makes typing almost imposs...

change the color of non-printing characters in this version of Word?
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) How does one change the color of non-printing characters in Word 2008 12.2.3 for Mac?? <br><br>In previous versions, the non-printing characters were greyed, in the new version they are blue and are a *huge* distraction. <br><br>Thanks You can turn the Off by clicking the "=C2=B6" to turn them on click it ag= ain. KW@officeformac.com wrote: > Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) How does > one change the color of non-printing characters in Word 2008 12.2.3 fo...

How do I change the inverse colour in an Excel chart?
I want to have blue bars when the values are positive and yellow when they are negative. I have checked the box for "invert if negative" when choosing the colour in Format Data Series, but I still get blue for both. David, The concept described here should work: http://www.pdbook.com/index.php/excel/conditional_column_chart/ ---- Regards, John Mansfield http://www.pdbook.com "david_hutton.NO_SPAM" wrote: > I want to have blue bars when the values are positive and yellow when they > are negative. I have checked the box for "invert if negative" wh...

Exch 2003/Outlook 2003 inbox sync problem
Greetings, After several hardware problems on a SBS Server 2003 I can no longer sync my inbox with Outlook 2003? All other folders sync just fine. After a manual sync the properties of the inbox show 147 items on the server and 0 items in the off-line folder. I have deleted the .ost file and had outlook recreate it -- no change. I updated to sp1 -- no change. I logged in on a different box with OL2003sp1 -- no change. ANY help or pointers are appreciated. -Steve S. ...

Forms problem #2
Hi all, I've designed a form in MSO 2003 with a picture for a background and the bcc field having a default value. The problem is that when I send attachments through this form sometimes the recipient gets the attachment as a .dat file! I like the functionality of the form that I've created but the problem with the attachments is a big one! Any suggestions? -pnp Do not use Outlook Rich Text as the form message format. Use Plain Text or HTML instead. The winmail.dat is caused by clients that cannot understand RTF (all except Outlook/Eudora) wrapping the formatting in the .da...