UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Vba To Work On Last 2 Lines Of A Word Document, Access 2013    
 
   
sxschech
post Jan 17 2020, 05:53 PM
Post#1



Posts: 19
Joined: 2-October 18



I've been putting together some code in Access to edit items in a word document. I can find/replace text and change point size. The issue is that there is some variability in the text to find and am concerned that it may find the text in another part of the document that should not be affected. The only consistency I think is that the area that potentially needs to be edited is on the last two lines of text. I found
QUOTE
.Forward = False 'False causes Word find operation to search backward through the document.
However, if the word isn't found, then I assume it will keep looking and if it happens to appear further up in the document, it will change the text there which is not desired.

The last two lines in question typically say:

Enclosure: xkxkxxk xk xk xkx
cc: aa, ab, ac, ad

However, variations are Enclosure, Enclosures, Attachment, Attachments and possibly the absence of colon symbol. Also sometimes there are additional hard returns (blank lines) after these two lines. Below is the code that I am using, which works under ideal conditions. Existing code can handle the issue of Enclosure vs Enclosures if I leave off the "s", so it's only if the wording is different. It is also possible that there are no attachments or enclosures, in which case that line/sentence would not be there at all.

call changepointsize(worddoc,"Enclosure",9)

CODE
Sub ChangePointSize(worddoc As Object, stext As String, sSize As Integer)
'based on unboldText (in this module) and response 2 from
'https://stackoverflow.com/questions/45757224/vba-changing-font-size-of-one-character-in-specific-string-msword-macro
'https://docs.microsoft.com/en-us/office/vba/api/word.find.forward
'Check the point size and if doesn't match change point size
'20200117
    Dim WordRange As Object
    
    'Define range.
    Set WordRange = worddoc.Content
    'Evaluate WordRange start and end points.
    'Debug.Print WordRange.Start & " " & WordRange.End
    With WordRange.Find
        .clearformatting
        .Text = stext
        .Forward = False 'False causes Word find operation to search backward through the document.
        .Execute
        If .found Then
            WordRange.expand unit:=3 'wdSentence
            If WordRange.Font.Size <> sSize Then
                WordRange.Font.Size = sSize
            End If
        End If
    End With
End Sub


Another thought, if can't really go by last two lines...is there a method to tell word to stop searching when it hits a specific phrase as the document seems to have the company name prior to those two lines. This too could be a problem because company name appears higher up as well.

If need further clarification, let me know. Thank you.
This post has been edited by sxschech: Jan 17 2020, 05:59 PM
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    19th February 2020 - 08:40 AM