Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA in Word, I need to be able to access the actual text of the

Status
Not open for further replies.

IVIr

Programmer
Apr 14, 2006
7
US
Using VBA in Microsoft Word, I need to be able to access the actual text of the document, not just some of the properties of the document. How can I both access and change the text, as well as move the cursor around in the document.

If possible I would like to be able to change the document via section. I wrote a VBA "program?" that is currently able to count the sections. And I would like to be able to either delete by section or copy and paste as a new section if possible.

I can't figure that out. Sorry this is probably a newbie question.
 


hi,
Code:
    Dim se As Section
    
    For Each se In ThisDocument.Sections
        MsgBox se.Range.Text
    Next

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You rather seem to be asking for a short course in the Word Object Model!

The Document Object is where you start from, and it contains Section Objects. Both contain Range Objects, which are really the main Objects when working with content. You shouldn't need to move the cursor when in code - just use Range Objects. Your question is really too broad to provide much more - you really need to try a few things out.

Working with Sections can be awkward at times. Why do you want to move them around?

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Hey thanks for the code, SkipVought. It was pretty successful! Though I'm having a bit of a problem. If I copy one section to overwrite an existing section, it turns the section break into a page break.

Example:
ThisDocument.Sections(1).Range.Text = ThisDocument.Sections(2).Range.Text

I'm assuming it's some sort of ascii character that's out of range or an escape character problem or something. Any ideas?
 



Did you see the funny character in the MsgBox?

check out the Left() function.

Or use the Split function using whatever THAT character is as the separator.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
SkipVought, the Left() function is a good idea! Though, I just discovered that the code I was using before was deleting all my formatting. So I had to bail on that idea. I'm trying to preserve all formatting.

I found something that works perfectly for what I needed previously:

ThisDocument.Sections(4).Range.Copy

ThisDocument.Sections(1).Range.Paste

This overwrites section one with section four.
Example:

Section 1
Section 2
Section 3
Section 4
Section 5
Section 6

->

Section 4
Section 2
Section 3
Section 4
Section 5
Section 6

If I use Cut:

ThisDocument.Sections(4).Range.Copy

ThisDocument.Sections(1).Range.Paste

Example:

Section 1
Section 2
Section 3
Section 4
Section 5
Section 6

->

Section 4
Section 2
Section 3
Section 5
Section 6

But what if I want to just move section 4?
I still can't figure that out.
I tried Insert that seems to only use text. So I lose my formatting.
Is there another way to use paste so it doesn't overwrite?
 
If I could just figured out how to duplicate a selection before pasting over it that would solve the whole problem right there.
 
Or a way to paste without overwriting.
 
I should also note I'm open to a method that doesn't use the clipboard this is was just the only way I could find to preserve formatting.
 
What is the purpose of messing around with Sections like this?

As general purpose code this is very hard indeed, there are so many things to check for. For example (and it sounds as though you may be falling foul of this), the last character of a section may or may not be a paragraph mark and if it isn't, then there is an implicit paragraph mark (holding paragraph formatting) in the section break. I would recommend you experiment in the UI to see what happens in different circumstances before you try and code up whatever it is that you want.


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top