triogenerator.blogg.se

Vba word build range by paragraph
Vba word build range by paragraph












vba word build range by paragraph

Vba word build range by paragraph code#

You can make your code quicker still if you only change the paragraphs if your processing indicates that a change is necessary. It would be quicker to access the Paragraphs collection using a For Each loop, as shown here: The above code does, however, take quite a long time to run. When J is equal to 2, it is the second paragraph-and so on.)Īfter the processing of sMyPar is complete, then the next line sets the document text equal to the modified text in the sMyPar string. (When J is equal to 1, you are working with the first paragraph. I have finally managed to created a Word document and paste the contents of a sheet from my worksheet using: Sub CopyToWord () Dim objWord As Object Set objWord CreateObject ('Word.Application') Dim objDoc As Object Set objDoc CreateObject ('Word.Document') 'Copy the range Sheets ('sheet1').Range ('A1:L100').Copy objWord.Visible True With. The third line set the sMyPar string equal to the text within the specified paragraph. The loop starting in the second line then does the main work in the macro.

vba word build range by paragraph

The first line of the code sets iParCount equal to the number of paragraphs in the current document. SMyPar = ActiveDocument.Paragraphs(J).Range.TextĪctiveDocument.Paragraphs(J).Range.Text = sMyPar The following example returns a Range object that refers to the first paragraph in the active document. The Range property applies to many objects (for example, Paragraph, Bookmark, and Cell ). All of the paragraph objects are accessible as part of the Paragraphs collection. Set myRange ActiveDocument.Range (Start:0, End:10) Use the Range property to return a Range object defined by the beginning and end of another object. Since each paragraph is a distinct object in the document, this is relatively easy. In other words, you can manipulate paragraphs without ever needing to select them.įor instance, let's say you wanted to access each paragraph of a document, in turn, and do some processing on the text in that paragraph. This means that you can access every part of your document using objects and collections of objects. One of the nifty things about programming VBA macros is that the language is object-oriented.














Vba word build range by paragraph