Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-29-2015, 08:35 AM
Marco Marco is offline from text to table, with conditions depending on format Windows Vista from text to table, with conditions depending on format Office 2007
Novice
from text to table, with conditions depending on format
 
Join Date: Apr 2015
Posts: 15
Marco is on a distinguished road
Default from text to table, with conditions depending on format

Hello, is there anyone willing to help me with this problem?

I have a text formed by several short lines.
Each line start with either one word in italic or two words in italic, non italic words follows.
For example:

word1 word2 Word3 [rest of text]
word4 Word5 [rest of text]
word6 Word7 [rest of text]

What I need to do is to transform this text in a table where
- the first column will always contain the first word
- the second column will either contain the second word in italics, if this is present, or will be empty if a second word in italic is not present


- the third column will always contain the first word not in italic (which can be the second or the third word on the line

I was hoping to do this with a clever combination of find and replace, but I did not succeed.
My knowledge of VBA applied to word is next to zero, so I ask for some assistance.

One idea I had is to select rows starting with only one word in italics, and apply a particular find and replace condition only on those. In this way I think I can correctly place the special characters that I will be use to part the columns of the table.
Reply With Quote
  #2  
Old 10-29-2015, 04:33 PM
gmaxey gmaxey is offline from text to table, with conditions depending on format Windows 7 32bit from text to table, with conditions depending on format Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If each line is a separate paragraph then select the text to process and:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oTblRng As Word.Range
Dim oTbl As Table
Dim lngIndex As Long
  Set oRng = Selection.Range
  Set oTblRng = ActiveDocument.Range
  oTblRng.Collapse wdCollapseEnd
  Set oTbl = ActiveDocument.Tables.Add(oTblRng, oRng.Paragraphs.Count, 3)
  For lngIndex = 1 To oRng.Paragraphs.Count
    oTbl.Cell(lngIndex, 1).Range.Text = Trim(oRng.Paragraphs(lngIndex).Range.Words(1))
    If oRng.Paragraphs(lngIndex).Range.Words(2).Font.Italic = True Then
      oTbl.Cell(lngIndex, 2).Range.Text = Trim(oRng.Paragraphs(lngIndex).Range.Words(2))
      oTbl.Cell(lngIndex, 3).Range.Text = Trim(oRng.Paragraphs(lngIndex).Range.Words(3))
    Else
      oTbl.Cell(lngIndex, 3).Range.Text = Trim(oRng.Paragraphs(lngIndex).Range.Words(2))
    End If
  Next lngIndex
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-29-2015, 06:40 PM
Marco Marco is offline from text to table, with conditions depending on format Windows Vista from text to table, with conditions depending on format Office 2007
Novice
from text to table, with conditions depending on format
 
Join Date: Apr 2015
Posts: 15
Marco is on a distinguished road
Default

Hello Greg, thanks for your help.
It works greatly

However, I exaggerated a bit with the simplification:

what I called "first word non in italic" is actually a combination of 1-n words going from the first word non in italic to a special character (§) that I can easily add to the original text by find and replace.

That's beacause my first idea was to find a way to add "§§" between the first and the second word of each line only when the second word is not in italic.
Then I could convert text into table parting columns with §, and get an empty cell when there is not a second word in italic.

Also, the table should have: a fourth column whose content can be defined in one of the following ways (they will give the same results, choose the simplest to code)
- the word comprised between the previous § and the next §
- the first 4 characters after the previous §

The rest of the line will be contained, unbroken, in a fifth column

Thanks for your suggestions
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
from text to table, with conditions depending on format Format tab leader in “Table of figures” separate from text? WTR_girl12 Word 1 09-03-2015 10:48 AM
from text to table, with conditions depending on format How to Subtract value in table, based on two different conditions nitemath2 Excel 1 06-19-2015 01:28 AM
Hyperlink Format varies, depending on whether Target file was saved or unsaved on Hyperlink Copy RichardDavey Word 0 05-26-2015 05:26 PM
Help - Run-time error 91 - VBA for inserting and formatting text depending on style mtrborges Word VBA 2 02-08-2015 11:35 PM
3x1 Table: text in fields loses paragraph format when being bulleted madasco Word 3 06-07-2013 06:44 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft