Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-21-2024, 05:35 AM
mdlewis mdlewis is offline Read into a variable a block of Word Document Windows 7 64bit Read into a variable a block of Word Document Office 2010
Novice
Read into a variable a block of Word Document
 
Join Date: Jan 2019
Posts: 11
mdlewis is on a distinguished road
Default Read into a variable a block of Word Document

I have a word file like this format. I wish to read the text starting with a "#" into a variable. At next #, load the current accumulated lines into a table record field. My current process is just picking up the first paragraph.

For Each singleLine In ActiveDocument.Paragraphs



LineText = TriM(singleLine.Range.Text)

If Len(LineText) >= 32767 Then
Debug.Print Len(LineText)
' Stop
End If

If Left(LineText, 1) = "#" Then
' new record

'// parse the text here...
LineText = Right(LineText, Len(LineText) - 1)
LineText = Replace(LineText, Chr(34), "") ' single double quotes

else
' update stored record and get ready for next record or end

end if

' ETC


Next singleLine


Text example 2 "logical" records - but I pick up only 1st paragraph in each.

EOP
#à BECKETT, SIR THOMAS (1836-1919), judge, was born in London on 31 August 1836. His father, Thomas Turner à Beckett (1808-92), brother of Sir William à Beckett (q.v.), was educated at Westminster School. He came to Australia on a visit to his brother, arrived at Melbourne in January 1851, and, deciding to stay, practised as a solicitor. He was nominated to the legislative council in 1852, and after responsible government came in was elected for the Central Province in 1858. He held this seat for 20 years, was a minister without portfolio in the Heales (q.v.) ministry from November 1860 to November 1861, and commissioner of trades and customs from April 1870 to June 1871 in the third McCulloch (q.v.) ministry. He was the author of several pamphlets on legal and other subjects, and was registrar of the diocese of Melbourne from 1854 to 1887, a member of the council of the university, and a trustee of the public library.EOP
His eldest son, Thomas, came to Australia with his father in 1851, returned to London in 1856, and entered as a student at Lincoln's Inn. He won a studentship and was called to the bar in November 1859. Returning to Victoria in 1860 he quickly established a practice, specializing in equity. He was lecturer in the law of procedure for several years at the university of Melbourne from 1874 onwards, and had been leader of the equity bar for some time when he was appointed a supreme court judge in September 1886. He was just 50 years of age and did not retire until 31 July 1917, nearly 31 years later. In 1916 the bar of Victoria presented his portrait by Max Meldrum to the supreme court library, and the opportunity was taken to express the affection in which à Beckett was held. He died at Melbourne on 21 June 1919. He married in 1875 Isabella, daughter of Sir Archibald Michie (q.v.), who survived him with two sons and three daughters. He was knighted in 1909. A younger brother, Edward à Beckett (1844-1932), was a portrait painter. Examples of his work are at the supreme court, Melbourne.EOP
à Beckett was an active man and continued to play tennis until an advanced age. Like other members of his family he had a keen sense of humour, and many stories are told of him and his sayings, both on and off the bench. He was very popular with the bar, though counsel did not always appreciate his direct methods, which were aimed at preventing the unnecessary prolongation of cases. Occasionally he would deliver what he called an "interim judgment" when he considered one party had a hopeless case. Though good-tempered, obliging and courteous, he could be called a strong judge, and he was never afraid to dissent from his colleagues in the full court. It was found that no judge of the period had his decisions less often upset by the high court or the privy council, and he ranks as one of the finest equity judges Australia has known.EOP
The Age, Melbourne, 23 June 1919; The Argus, Melbourne, 23 June 1919; P. Mennell, The Dictionary of Australasian Biography, Burke's Colonial Gentry, 1891.EOP
EOP
#à BECKETT, SIR WILLIAM (1806-1869), first chief justice of Victoria, son of William à Beckett and brother of Gilbert A. à Beckett of Punch, was born in London on 28 July 1806, and educated at Westminster School. He was called to the bar at Lincoln's Inn in 1829 and for some years was much engaged in literary work. He was responsible for A Universal Biography, a substantial work in three large volumes, published about 1835, which, however, seems to be largely based on previous compilations. He also wrote many of the biographies in The Georgian Era, published in four volumes in 18324. He went to New South Wales in 1837, in March 1841 was appointed acting solicitor-general, and in March 1843 solicitor-general. He became an acting-judge in July 1844, in 1846 was appointed a judge of the supreme court at Port Phillip, and in January 1851 chief justice of the newly formed colony of Victoria. His health had not been good for many years, and he retired on this account early in 1857. He returned to England in 1863 and died at London on 27 June 1869. He was knighted in 1851. He was married twice and was survived by four sons by the first marriage. His eldest son, W. A. C. à Beckett, was a member of the legislative council of Victoria from 1868 to 1876. In addition to the works already mentioned à Beckett published a youthful volume of verse, The Siege of Dumbarton Castle, in 1824, The Magistrates' Manual for the Colony of Victoria (1852), Out of Harness, an account of a tour on the Continent (1854), The Earl's Choice and other Poems (1863).EOP
à Beckett was a man of culture and refinement and an excellent judge, who, in spite of his delicate health, carried out his duties with ability.EOP
EOP
The Times, 1 July 1869; P. Mennell, The Dictionary of Australasian Biography, Historical Records of Australia, ser. I, vols XXI to XXV; British Museum Catalogue; Burke's Colonial Gentry, 1891.EOP
Reply With Quote
  #2  
Old 08-21-2024, 09:13 AM
vivka vivka is offline Read into a variable a block of Word Document Windows 7 64bit Read into a variable a block of Word Document Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi, mdlewis! If I understand the task correctly, I would:
1) use Find to find # in a rng, then move the rng's end until the next #, add the rng to a collection. Cycle the actions, type the collection itms in a proper place.
Or
2) use Find to find #*# in a rng, then move the rng's End 1 chr backwards, add the rng to a collection. Cycle the actions, type the collection itms in a proper place.
Reply With Quote
  #3  
Old 08-21-2024, 05:01 PM
mdlewis mdlewis is offline Read into a variable a block of Word Document Windows 7 64bit Read into a variable a block of Word Document Office 2010
Novice
Read into a variable a block of Word Document
 
Join Date: Jan 2019
Posts: 11
mdlewis is on a distinguished road
Default Looks good

Thanks, I understand your logic, but I am deficient in Word VBA. I've usually generated it and fiddled.

Could you please provide me some code snippets for the tasks, please.

I've worked out how to find/replace the offending characters at Word Level and then let my code "rip" which would work as there are not many files to process and it occurs infrequently but that is very "inelegant".
I'd prefer to do it in code.
Reply With Quote
  #4  
Old 08-21-2024, 06:59 PM
Guessed's Avatar
Guessed Guessed is offline Read into a variable a block of Word Document Windows 10 Read into a variable a block of Word Document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This should get you started. Note that your concept might miss the last entry if it doesn't end with a # (which in your sample code appears to be a start point rather than a bracketing)
Code:
Sub Macro1()
  Dim aRng As Range, sFound As String
  
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "#*#"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWholeWord = False
    .MatchWildcards = True
    Do While .Execute = True
      aRng.MoveStart Unit:=wdCharacter, Count:=1
      aRng.MoveEnd Unit:=wdCharacter, Count:=-2
      If Len(aRng.Text) > 32766 Then
        Debug.Print "Oversize Block starting: " & aRng.Paragraphs(1).Range.Text
      Else
        'Do something with the found content
        MsgBox aRng.Text
      End If
      aRng.Collapse Direction:=wdCollapseEnd
      If aRng.End = ActiveDocument.Range.End Then
        Exit Do
      Else
        aRng.End = ActiveDocument.Range.End
        aRng.Select
      End If
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 08-21-2024, 07:04 PM
mdlewis mdlewis is offline Read into a variable a block of Word Document Windows 7 64bit Read into a variable a block of Word Document Office 2010
Novice
Read into a variable a block of Word Document
 
Join Date: Jan 2019
Posts: 11
mdlewis is on a distinguished road
Default Thanks, I'll try it.

That looks great.
Reply With Quote
  #6  
Old 08-21-2024, 08:52 PM
Guessed's Avatar
Guessed Guessed is offline Read into a variable a block of Word Document Windows 10 Read into a variable a block of Word Document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

A simpler method of dealing with this data is to use split to break the entire document into a string array.
Code:
Sub Macro2()
  Dim arrClauses() As String, i As Integer
  arrClauses = Split(ActiveDocument.Range.Text, "#")
  For i = 1 To UBound(arrClauses) 'intentionally excludes entry before first #
    MsgBox arrClauses(i)
  Next i
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read into a variable a block of Word Document error 91 object variable or with block variable not set ubrielbryne Word VBA 1 02-25-2019 11:15 AM
Run Time Error '91': Object variable or With block variable not set using Catalogue Mailmerge Berryblue Mail Merge 1 11-13-2014 05:36 PM
Read into a variable a block of Word Document Run-time error 91 object variable or with block variable not set JUST ME Word VBA 4 03-25-2014 06:56 AM
Read into a variable a block of Word Document object variable or with block variable not set MJP143 Excel 1 02-11-2013 05:07 AM
Read into a variable a block of Word Document Run-time error '91': Object variable or With block variable not set tinfanide Excel Programming 2 06-10-2012 10:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:25 PM.


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