Word VBA: Convert Superscripts in Footers/Headers - vba

I'm using code like this to output all the headers/footers per section to a text file:
Word VBA - getting text file output to look right
I've been able to convert all the special characters in the sHeader/sFooter string variables using Replace() without a problem.
Example: sFooter = Replace(sFooter, ChrW(8804), "^R'\ {\uc2\u8804 <=}'")
All is great until I try to replace a superscript. Alt+x only gives me the value of the letter itself. I've tried looking up the hex value (may not even be a thing) with no success. I've even tried the different Latin character values.
I recorded a macro to see how Word would replace it and got:
Selection.Find.ClearFormatting
With Selection.Find.Font
.Superscript = True
.Subscript = False
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "A"
.Replacement.Text = "^super{a}"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
The problem is I don't know how to use them together.
I tried to use oSec.Footers(wdHeaderFooterFirstPage).Range.Find... instead of Selection.Find... but that results in a null string.
Can I make a String object somehow from sHeader/sFooter so I can utilize the recorded macro code? I tried googling, converting string to object, but didn't get anything help. Is there a better approach?
Thanks in advance.

I know this isn't exactly answering your question, but the problem is that the character is the same regardless of whether it is superscript or not, i.e. superscript is formatting, just like bold or italics.
For example, a bold A, a normal A and a superscript A all have code 65.
If you are saving to a text file, you won't be able to save formatting, so you would need to invent your own representation for a superscript character.
The representation from the Find/Replace code, i.e. "^super{a}" is what Word uses to allow you to do Find/Replace on text with a specific formatting.
Note: There are some special characters such as ² which have a separate character code to 2, so these would have a different code.

Thank you M1chael for your input.
I was able to get the recorded macro above to work by setting the selection to the current header/footer section.
Example:
oSec.Footers(wdHeaderFooterPrimary).Range.Select
Selection = ActiveDocument.ActiveWindow.Selection
Thanks

Related

Is it possible to use a Word macro to replace one mail merge field with another?

I have Word Mail Merge document with merge codes inserted. I was wondering if I can write a Word macro to search for one mail merge code and replace it with another? I have tried, but the merge code I replace reverts back to the original value.
I create a Word macro and use CTRL + H, put the data in to search and replace for(The merge fields) and click replace. It replaces the data, but after I save and return to document the old merge code is still there. The following vba code is generated:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "F_400"
.Replacement.Text = "F_901"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
I am replacing merge field F_400 with merge field F_901.
In order to replace the name of a Mergefield you need to work with the underlying field code. What you normally see is a "pretty view" presented by Word, that can be switched between "field name" and the data preview provided by the Mailings tab in the Ribbon.
In order to use Find/Replace directly on the field codes, use Alt+F9 to toggle to the field code view. You should then see { MERGEFIELD fieldname }. In this state, you can Find fieldname and Replace it with a different, valid field name from the data source.
In order to toggle the field codes display as part of a macro so that you can use Find/Replace in VBA, use a Window object with the View.ShowFieldCodes property, for example:
ActiveWindow.View.ShowFieldCodes = True 'False to turn them off
This works fine when using Selection.Find. For Range.Find there's an alternate approach, by changing the TextRetrievalMode for the Range object being used for the Find:
rngFind.TextRetrievalMode.IncludeFieldCodes
With rngFind
'and so on
The IncludeFieldCodes property picks up the "hidden" field codes for the code processing, so it doesn't matter whether the field codes are displayed on-screen, or not.

Programmatically change a certain font+size to a different font+size

I have a ms-word document where Helvetica 13.5 is the main font for paragraphs. Unfortunately, it's not tied to any particular pre-defined style in the document (the text was copied into the document from a website). Additionally, you will see embedded in the paragraphs a few words italicized and some words which are 'Courier New'.
What I'd like to do is walk through the document looking for snippets of text that have a single font/size. If that text is Helvetica 13.5, I would like to change it to Times New Roman 12. I don't want to change the embedded courier new. Italicized words should stay italicized (but their font should change if it's Helvetica/13.5)
Is there an "easy" way to do this?
Thanks
I don't know how to do this programmatically, but I know Word has the ability to Find and Replace based on Formatting. Here is how you do it:
In your document for Windows do Ctrl-G, for Mac do Command-Option-G. This will pull up the Go To window.
Select the Replace tab
In the Replace tab you should see for Windows "More >>", for Mac a "v" (Down arrow). Hit that button, to pull up the extra functions.
Now choose the format you are looking for by hitting the format button at the bottom.
To choose the new format you want, select the "Replace with" text field and then do the same as the previous step. Your options should show up below the respective text field.
When ready, hit Replace All
Note: keep the text fields empty if you want the replace to only match text based on formatting
Sub Helv_to_TNR
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Text = ""
.Replacement.Text = ""
.Font.Size = 13.5
.Replacement.Font.Size = 11
.Font.Name = "Helvetica"
.Replacement.Font.Name = "Times New Roman"
.Execute Replace:=wdReplaceAll
end with
end sub

Word 2010, Move cursor to specific text, replace the text with file content

I have a line of text that says "[EmbeddedReport]report goes here[/EmbeddedReport]".
I want to replace the "report goes here" with an empty string and put the cursor right after the [EmbeddedReport] marker. I will then run the following code...
With Selection.InsertFile ('c:\Temp\Report.rtf')
That should put the text of the report between the markers. I tried to locate the cursor with the following code. It does not seem to work.
.Selection.Find
.ClearFormatting
.MatchWholeWord = False
.MatchCase = False
.Execute FindText:="report goes here"
Only problem is the cursor is not between the [EmbeddedReport] and [/EmbeddedReport] and the file is inserted wherever the cursor was located before the macro is run.
you will possibly need to call Find object twice:
FIRST- to find the whole phrase [EmbeddedReport]report goes here[/EmbeddedReport],
SECOND- to find a report goes here text within result of step first. Important- you don't need to replace that phrase- it will be selected and replaced with the text you import using Selection.InsertFile method.
Here is proposed code (tested for sample file):
'FIRST- find [EmbeddedReport]report goes here[/EmbeddedReport]
With Selection.Find
.Text = "(\[EmbeddedReport\])text goes here(\[\/EmbeddedReport\])"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'...and select it
Selection.Find.Execute
'SECOND- find only text to replace 'text goes here'
With Selection.Find
.Text = "text goes here"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'end select it
Selection.Find.Execute
'now you could insert your file
Selection.InsertFile "c:\Temp\Report.rtf"
The usual way to fix these kind of problems is to record a macro - and perform the tasks you need to move the cursor and highlight text, etc. - and then examine the code that the macro wrote to perform those tasks. This will usually give you the insight to tailor your own code to perform the same tasks.

writing word macro -- how do I write a Find to locate the next Korean character on a line?

I'm trying to take a large file of Korean vocabulary and set it up to import smoothly into a flashcard program. The format of the file is [Korean word/phrase] [English translation] [Korean sample sentence]. Example:
너무 피곤해서 Because I’m tired 너무 피곤해서 잤어요.
I can write a macro to look for the first English letter and replace the space before it with a tab. I identified the first English letter by searching for the range [a-Z]. After that I want to locate the beginning of the sample sentence by searching for the next Korean character encountered, but what is the range for Korean characters?
I found a unicode FAQ on Korean characters which seemed to suggest that each character is really just a combination of individual letters, and in some kinds of programming can be treated as the separate letters, but I probably misunderstood. The idea was that something like "식" is really the three letters "ㅅ" + "ㅣ" + "ㄱ". So I tried a search on just the one letter "ㅅ" (which appears in tons of characters in my input file) and got no hits. That sure had the potential to make things simple, but no dice.
Okay, got it -- found the range here:
http://en.wikipedia.org/wiki/Korean_language_and_computers#Hangul_in_Unicode
The below code in my macro finds the next Korean character in a Word document:
With Selection.Find
.Text = "[" & ChrW(44032) & "-" & ChrW(55203) & "]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

MS Word VBA: How to replace each word with its translation while keeping formatting

I have a word document and want to replace each single word with its translation while keeping all the formatting intact. I cannot use "Find And Replace" dialog because I am not trying replace a particular set of words but I am replacing all the words. How do I do that using VBA?
Update
I am using Word 2010. So far, I can loop through the words using ActiveDocument.Range.Words property but I don't know how to replace those words with its translation? While replacing, I want to keep all the formattings like font name, size, color, background color, underline, bold in short all the formatting options as it is.
I guess you have an array of words ("apple", "book", "cat") and the array of their translation ("pomme", "livre", "chat").
Your goal is to bulk change words one by one.
So you need a loop. Here is the loop that may help (if I understand your problem correctly).
Bulk change upon two arrays:
Option Explicit
Sub replaceArrayForArray()
'
'to create array use prefix\suffix and replacing tool http://textmechanic.com/
'
'
findArray = Array("apple", "book", "cat")
replArray = Array("pomme", "livre", "chat")
For i = 0 To UBound(findArray)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = findArray(i)
.Replacement.Text = replArray(i)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
Next i
End Sub
The more diffucult issue could have been if you worked with figures and you had to bulk change figures without overlap. The method with the use of the same macro more or less described here: MS Word Macro to increment all numbers in word document.