Access vba to adjust point/font size in word (1 Viewer)

sxschech

Registered User.
Local time
Today, 14:26
Joined
Mar 2, 2010
Messages
793
I have vba that takes data from an access table and does a find and replace in word. The issue is that depending on how much text, it will wrap to two or three lines. Is there some code that could determine if it exceeds two lines to reduce point size? Alternatively, can the access vba code pause or allow user to manually adjust point size in the event it can't be coded? Apparently, the text in question is in an object. It is Arial Black 24. Screenshots are before vba replace, after replace (showing 3 lines), last is showing after reducing size from 24 to 20 so that text takes no more than two lines.
 

Attachments

  • TitleBefore.PNG
    TitleBefore.PNG
    7.1 KB · Views: 56
  • Title3Lines.PNG
    Title3Lines.PNG
    13.6 KB · Views: 48
  • Title2Lines.PNG
    Title2Lines.PNG
    12.7 KB · Views: 51

Cronk

Registered User.
Local time
Tomorrow, 07:26
Joined
Jul 4, 2013
Messages
2,772
You could try something like this to get the current number of lines, and after changing to a smaller font, repeat the process in a loop.

Code:
  dim lngNoLines As Long
   wdapp.Selection.Paragraphs(1).Range.Select
   with wdapp.Dialogs(wdDialogToolsWordCount)
      lngNoLines = .Lines
      .Execute
   End With
   msgBox lngNoLines
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:26
Joined
Feb 28, 2001
Messages
27,188
You are in essence trying to compute the width (in human's sizing units) for a text string in an arbitrary font. It is remarkably difficult to do this.

It is possible in the case of Access reports (with difficulty) but is elusive most of the time. (Or at least, it always was hard to compute for me.)

The difficulty comes about because the software doing the text rendering behind-the-scenes doesn't always pay precise attention to your font size choices. It FIRST checks whether the chosen PRINTER can handle what you are trying to do. OR if you have a low-resolution screen, the rendering routine checks whether it can handle that font size on your screen.

While searching for a solution to a related problem, I ran across this link. It MIGHT be useful to you, although it was designed for text box font size computations. It uses a Windows "hook" to get at a piece of rendering code. I have not tried this myself, though if I had found the trick a couple of years ago, I certainly would at least have done the experiment. If it helps, let us know.

https://social.msdn.microsoft.com/F...-and-length-of-characters-string?forum=isvvba
 

sxschech

Registered User.
Local time
Today, 14:26
Joined
Mar 2, 2010
Messages
793
Hi Cronk and the Doc Man, you made me realize that I was making it more complex than need be and gave me an idea of the direction to go. I can test the string length of the variable (bolded in code snippet) and if it is greater than 50, use point size 20 otherwise keep at 24. Probably not foolproof depending on the length of words and letters (a word with an "i" may fit in two lines while a word with an "a" may wrap), but at least in testing a few samples within the character range of 50 - 60, seems to produce the desired effect.

Code:
 For Each wdRng In wdDoc.StoryRanges
            With wdRng.Find
                .Text = FindText
                .Replacement.Text = ReplaceText
                [B]If Len(ReplaceText) > 50 Then
                    .Replacement.Font.Size = 20
                End If[/B]
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Next wdRng
 

Users who are viewing this thread

Top Bottom