VB macro in MS Word Inconsistently formatting Arabic Text - vba

I wrote the following macro for MS Word. It works sometimes, but not consistently. I would love some help identifying what the problem might be with the code. Thanks in advance.
The macro should perform the following tasks.
In a paragraph containing English text and non-English text:
Apply the Arabic font (Noto Naskh Arabic UI) to each instance of non-English text
Set each instance of non-English text to 18-point font size
In a paragraph containing only non-English text:
Apply the Arabic font (Noto Naskh Arabic UI) to each instance of non-English text
Set each instance of non-English text to 18-point font size
Right-align the entire paragraph
Issues
Non-English text doesn’t change font size in either scenario.
The right-alignment isn’t consistently applied in the second scenario.
Sub Arabic()
For Each myPara In ActiveDocument.Paragraphs
If myPara.Range.LanguageID = 9999999 Then
For Each myWord In myPara.Range.Words
If myWord.LanguageID <> wdEnglishUS Then
myWord.Font.Name = "Noto Naskh Arabic UI"
myWord.Font.Size = 18
Else
myWord.Font.Name = "Times New Roman"
End If
Next myWord
ElseIf myPara.Range.LanguageID <> wdEnglishUS Then
myPara.Range.Font.Name = "Noto Naskh Arabic UI"
myPara.Range.Font.Size = 18
myPara.Alignment = wdAlignParagraphRight
End If
Next myPara
End Sub

Related

Set the theme header font for text in a shape

I develop a VBA add-in for PowerPoint which can insert a table into a slide. I set the font family for the table's header cells to the ones defined in the theme fonts. I want it to change when I switch to another theme font.
However, if I use the following code the font will be "pinned" to the font family name of the theme's major font and does not change when I change the theme fonts.
Sub FormatTable(table As table)
Dim headerFont As ThemeFont
Set headerFont = ActivePresentation.SlideMaster.Theme.ThemeFontScheme.MajorFont(1)
For Each c In table.Rows(1).Cells
c.shape.TextFrame.TextRange.Font.Name = headerFont.Name
Next c
End Sub
How do I have to rewrite the code to keep the font exchangeable via theme changes?
' Theme fonts have special names
'Body font, Latin (ie main) +mn-lt
'Heading Font, Latin + mj - lt
'Body Font, Eastern + mn - ea
'Heading Font, Eastern + mj - ea
'Body font, complex scripts +mn-cs
'Heading font, complex scripts +mn-cs
Sub FormatTable(table As table)
Dim headerFont As ThemeFont
Dim c As Cell
Set headerFont = ActivePresentation.SlideMaster.Theme.ThemeFontScheme.MajorFont(1)
For Each c In table.Rows(1).Cells
' This sets the font to whatever the NAME of the theme font is
' c.Shape.TextFrame.TextRange.Font.Name = headerFont.Name
' This sets it to the actual theme font:
c.Shape.TextFrame.TextRange.Font.Name = "+mn-lt"
' And after running the code, you'll see that the font
' is set to e.g. Calibri (Body) rather than just Calibri
Next c
End Sub

VBA excell, change format of one word within a cell

I have a issue to change all bold text withing a couple of cells to ALLCAPS (or even to a different alcaps font)
I saw a couple of similar modules but I am not an expert to change the condition and the loop.
(so change all words from bold to allcaps withing a selection - no full cells)
try this one, works pretty well for me. Below code changes every bold letter into uppercase:
Sub BoldToUpper()
Dim i As Integer
Dim cell As Variant
For Each cell In Selection
'Check each character of a string
For i = 1 To Len(cell.Value)
With cell.Characters(i, 1)
'Possible adjustment due to regional settings
If .Font.FontStyle = "Bold" Or .Font.FontStyle = "Bold Italic" Then
.Caption = UCase(.Caption)
'Possible adjustment due to regional settings
.Font.FontStyle = "Regular" 'Deletes bold effect
End If
End With
Next i
Next cell
End Sub
Note, that perhaps you may adjust the code to your local language settings.
For example in polish:
If .Font.FontStyle = "Pogrubiony" Or .Font.FontStyle = "Pogrubiona kursywa" Then

Need to preserve initial paragraph and character styles in text selection after adding hyperlinks

I am adding hyperlinks to selected text in Word. The selected text may have formatted text in it, such as italics, small caps, bold, etc.
I am able to preserve the CHARACTER styles, i.e., small caps, if the ENTIRE selection is the same character style (e.g., small caps). However, if the text includes unformatted text along with the formatted text, then the character style is replaced with the hyperlink style.
I inserted screenshots of before and after adding the hyperlinks.
BEFORE (no hyperlinks added):
AFTER (each line has a hyperlink added):
What I'm trying to end up with is something like the "SMALL CAPS TEST" as it:
preserves the initial small caps style
does not get overwritten with the HYPERLINK style (blue underlined text)
Any help is much appreciated. I'm at a loss on how to step through the style applied to each word or even character in the text selection.
UPDATE:
I managed to get rid of the Hyperlink character style SHOWING up as blue and underlined. However, the character style will still overwrite the previous character styles (i.e., Bold, Small Caps, Italics). Here is my code that will provide the correct character style applied until the Hyperlink is attached. However, if the entire selection is all ONE character style, then it is still preserved (probably due to the code line:
Selection.style = MyStyle
I need to be able to apply the correct character style to each character.
Here is my code:
``Sub AddHLINK()
Dim myText, myISBN, myCurrISBN, myURL, myCharStyle, ochr As String
Dim NoCharSty As Boolean
Dim MyStyle, MyFont, mychar, mycharstyleb As String
Dim charcount, j, k As Integer
myURL = "http://www.google.com"
myText = Selection.Text
charcount = Len(myText)
For k = 1 To charcount
mychar = Selection.Characters(k).Text
MyStyle = Selection.Characters(k).Style
If ActiveDocument.Characters(k).Style = wdStyleTypeCharacter Then
myCharStyle = MyStyle
NoCharSty = False
Else
NoCharSty = True
End If
Next k
MsgBox "the current url is " & myURL
Stop
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=myURL, TextToDisplay:=myText
End Sub

I'm trying to simplify and automate formatting from multiple Word sources

I'm a very novice programmer (think code-enthusiastic English major) trying to do something that I think should be very straightforward. For work I have to incorporate materials from multiple Word-using sources, all of whom have different settings for various styles. So Alan will have Heading 1 be, for example, 14 pt Arial, while Beth's Heading 1 will be 16 pt Courier New, and the standard for my org for Heading 1 is 18 pt Calibri. The different styles can quickly pile up, and even if you change the imported materials to fit what is correct, there's a bunch of material in there that can cause issues.
What I'd like is to build a VBA macro that runs through the document and essentially says, "this is Body? Cool, all Body text is set to these settings. This is Heading 1? All Headings 1 should be XYZ," and then goes through and does those conversions. I got an O'Reilly book on writing macros for Word, but I quickly got in over my head. Any suggestions on how to start, or where to look?
Thanks in advance.
ETA
My code is now at this point:
Sub test()
Set objDoc = ActiveDocument
' First, highlight the entire document in the default color (i.e. the color to use for styles that aren't defined in our guide)
objDoc.Range.HighlightColorIndex = wdDarkYellow
' Need to figure out how to do color code (RGB) this works for now.
reformatStyle "Heading 1", False, "Calibri", wdBlack, 16
reformatStyle "Body"
' Call reformatStyle once for each style, setting the appropriate values for bold, fontName, color and size.
' valid colors are at https://learn.microsoft.com/en-us/office/vba/api/word.wdcolorindex
End Sub
Sub reformatStyle(style, bold, fontName, color, fontSize)
With objDoc.Content.Find
.ClearFormatting
.style = style
' this first bit selects the blocks that have the specified style
With .Replacement
.Font.bold = bold
.Font.Name = fontName
.Font.ColorIndex = color
.Font.Size = fontSize
.Highlight = False
' this modifies the selected blocks. Note that we remove the highlighting at this point, otherwise it would remain at the default color
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
End With
End Sub
What I'd like to do is be able to set the color to RGB values. Any suggestions?

Why is Selection.Find not returning all results in Word VBA?

I'm trying to extract text out of a document based on certain formatting rules, e.g. the font size equals 10.5. This is what I'm doing now:
Selection.Find.Font.Size = 10.5
Text = ""
Do While Selection.Find.Execute = True
Text = Text + Selection
Loop
Debug.Print Text
It works, but for some reason it doesn't seems to return all results. When I do a manual search, i.e. Ctrl+H, and use the same formatting rule many more results are returned.
What could cause this?
In VBA + is not used for concatenation. You have to replace it with &
Change Text = Text + Selection to Text = Text & Selection and try again. I have tested it and it works...