I'm trying to run a spell check on a string using word, but I don't want to have to go through each word manually and determine the correct spelling. Is there a way to do this automatically, so that it corrects all the misspelled words on its own? Here is my code so far, this works but I have to go through each word and hit change...
If Address.Length > 0 Then
Dim word_server As New Word.Application
word_server.Visible = False
Dim doc As Word.Document = word_server.Documents.Add()
Dim rng As Word.Range
rng = doc.Range()
rng.Text = Address
doc.Activate()
doc.CheckSpelling()
Address = doc.Range().Text
doc.Close(SaveChanges:=False)
word_server.Quit()
doc = Nothing
rCell.Value = Address
End If
Use the GetSpellingSuggestions function to bypass the GUI. to see if there are any spelling suggestions, and then you can set it to the first suggestion (which could be a bad idea).
How do you want to determine what the correct spelling is? Should "spon" be spoon, span, spin, spun, or son? This code optimistically assumes that the first suggestion (if one exists) is the correct solution.
Declare:
Dim oError As Word.Range
And then replace:
doc.Activate()
doc.CheckSpelling()
with:
For Each oError In rng.SpellingErrors
If oError.GetSpellingSuggestions.Count > 0 Then
oError.Text = oError.GetSpellingSuggestions().Item(1).Name
Else
'Uh oh, no suggestions, do something here.
End If
Next
These websites might help. The example code in the website shows how to call Word to spell check. You should be able to modify it, to use with your code.
http://www.vb-helper.com/howto_net_spellcheck.html
http://www.vbforums.com/showthread.php?307151-SPELL-CHECK-and-WORD
Related
Using VBA, how do you find the name of the range of an active cell (merged cells)? I have a worksheet that I have established a few named ranges. i.e. K7:R28 is a range of merged cells and is named "LocM11". I typed that into the Name box and it is already established. In my VBA code, if a user clicks on the merged cells of K7:R28 and presses a command button I want the code to return the name "LocM11" and save it as a variable to be used later in the code. I've looked everywhere but this has me stumped. Any advice is appreciated.
I've tried some code sniplets I found on the internet to no avail. I've found code that uses VBA to name a range, but I do not want to do that. I have the name established. I want to know the already existing Name and save it as a variable.
try this
Dim activeRange As Range
Dim rangeName As String
Set activeRange = ActiveCell.Range
rangeName = Application.Range(activeRange.Address).Name
Debug.Print rangeName
More details - https://learn.microsoft.com/en-us/office/vba/api/excel.application.range#example
How can I get the range of the 'whole' document including all stories as wdMainTextStory, wdFootNotesStory, wdEndNotesStory etc. And also I need to get the range of wdFootNotesStory.
I have tried sevaral ways.
ActiveDocument.Range(0, 0).Select
Selection.WholeStory
This only select the whole wdMainTextStory. If anybody could, please help me to go through this.
I have found a way to get the range of FootNotes.
dim objRange As Word.Range
For Each storyRange In ActiveDocument.StoryRanges
If storyRange.StoryType = wdFootnotesStory Then
objRange = storyRange
End If
Next storyRange
I'm new to programming and I'm trying to copy the content of a form field to another form field in the same Word document like this:
Sub Copyfield()
Dim Temp As String
Temp = ActiveDocument.FormFields("Field1").Result
ActiveDocument.FormFields("Field2").Result = Temp
End Sub
My problem is that my "Field1" is a piece of text of more than 255 characters which seems to be a problem with "Result". I know there is a very similar topic here: Passing MS-Access string >255 characters to MS-Word field but I still don't have the 50 reputation to comment on that thread.
Could anyone please help me understand how to implement the changes in my code?
Well, here's one possibility. Since I don't have your environment it was easier for me to test text in the document rather than another form field with so much content. You'll need to adjust the code accordingly.
The key is to get the Selection "inside" the form field so that it doesn't hit the "protection barrier". Just using FormField.Select puts the focus at the beginning of the field, which VBA is seeing as "protected". Moving one character to the right corrects that and long text can then be assigned to the Selection. But the field needs to have content.
So what my code is doing is "slicing off" the first word of the text to go into the form field. That's short enough to assign to the Result property and lets the Selection move to its right. Then the rest - the long text - can be assigned to the Selection.
You'll probably want to assign the entire FormField.Result to a string variable, then manipulate that string.
Sub WriteLongTextToFormField()
Dim ffld As word.FormField
Dim doc As word.Document
Dim rng As word.Range
Dim s1 As String, s2 As String
Set doc = ActiveDocument
'Get the long text
Set rng = doc.Range(doc.Paragraphs(1).Range.Start, doc.Paragraphs(6).Range.End)
'Split off a bit to go into FormField.Result
s1 = rng.Words(1)
rng.MoveStart wdWord, 1
'The rest of the long text, to be assigned to Selection.Text
s2 = rng.Text
Set ffld = doc.FormFields("Text1")
ffld.result = s1
ffld.Select
Selection.MoveRight wdCharacter, 1
Selection.Text = s2
End Sub
Ok, after 3 days at the border of madness, finally thanks to the help of #Cindy Meister (and some serious personal digging), I made it work. Maybe it's not a big deal for you geniouses out there but believe me for me it was like seeing everything in Matrix code (from the movie guys, the movie).
I want to post it and share because I tried to find it in every corner of our Internet and part of the extraterrestrial one and I couldn't. So hopefully it will be useful for another programming illiterate / dumb person (as myself).
Here is the code:
Sub CopyField()
Dim ffld As Word.FormField
Dim doc As Word.Document
Dim rng As String
Dim s1 As String, s2 As String
Set doc = ActiveDocument
'Get the long text
rng = ActiveDocument.FormFields("Field1").Result
'Split off a bit to go into FormField.Result
s1 = Left(rng, 4) 'Keeps the first 4 characters of the rng string starting from left to right this can be adapted
'The rest of the long text, to be assigned to Selection.Text
s2 = Mid(rng, 5) 'Starting from the 5th character from the left keeps the rest of the string
Set ffld = doc.FormFields("Field2")
ffld.Result = s1
ffld.Select
Selection.MoveRight wdCharacter, 1
ActiveDocument.Unprotect 'Unprotects the document!
Selection.Text = s2
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True 'Protects the document again!
ActiveDocument.Bookmarks("Field1").Select ' "Sends cursor" back to Field1
End Sub
Big part of the code is originally by #Cindy Meister... I just adapted it to my situation where I had 2 form fields instead of paragraphs. I also had to add some lines to unprotect the document at a certain point in order to make it work (ask the Pros for the reason) and a final instruction to come back to "Field1" (which is some pages up) after the process. Finally just a note for my dumb fellows: I added the macro "on exit" on the "Field1" properties to automatize the process.
Huge thanks Cindy again and I hope you help in my dark programming moments again ! (please do)
:)
I'm trying to convert a Word VBA procedure to an AppleScript and only having partial luck.
Here's the first version of the VBA procedure:
Public Sub postprocessMerges1()
Dim rng As Range
Selection.HomeKey unit:=wdStory
With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
Do
.Text = "..."
.Execute
If .Found Then
.Parent.Select
Set rng = Selection.Range
rng.MoveStart unit:=wdParagraph, Count:=-1
rng.MoveEndUntil cset:=Chr(13)
rng.Text = formatAmounts(rng.Text)
End If
Loop While .Found
End With
End Sub
And here's my corresponding AppleScript:
on postprocessMerges()
tell application "Microsoft Word"
home key selection move unit a story extend by moving
set selFind to find object of selection
clear formatting selFind
set foundIt to true
repeat while foundIt
set foundIt to execute find selFind find text "..." wrap find stop with match forward without find format
if foundIt then
set foundRng to text object of selection
set foundRng to move start of range foundRng by a paragraph item count -1
set foundRng to move range end until foundRng characters {return}
set tt to (content of foundRng)
set (content of foundRng) to my formatAmounts(tt)
end if
end repeat
end tell
end postprocessMerges
Okay, so that works just fine. However, I'd like it to be better. The way the script is currently written, it actually jumps from hit to hit, highlighting the found text and performing the replacement generated by the formatAmounts subroutine. Not bad, but when you're working with a 200+ page document, it gets a little tedious to see that happening onscreen.
So in VBA, I can do this:
Public Sub postprocessMerges2()
With ActiveDocument.Content.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
Do
.Text = "..."
.Execute
If .Found Then
With .Parent
.MoveStart unit:=wdParagraph, Count:=-1
.MoveEndUntil cset:=Chr(13)
.Text = formatAmounts(.Text)
.Collapse direction:=wdCollapseEnd
End With
End If
Loop While .Found
End With
End Sub
This will perform the exact same action as the first procedure, but it does so on the document's content range rather than the selection range and so I don't have to watch Word jump around from page to page to page. Much more preferable, but not something I've been able to emulate in AS.
Specifically, I can't seem to get the hit range each time through the loop without selecting it first. The find object in Word's AS dictionary doesn't have a Parent property I can access like I do in VBA.
Is there anything I'm missing? Is what I do in the second VBA proc actually replicable in AS?
This is using Word 2011 and AppleScript 2.3 on OS X 10.9.3.
This should probably be a comment, but...
Is what I do in the second VBA proc actually replicable in AS?
Let me put it this way. I think you will probably end up using the Selection object a lot more in AS than you are probably used to doing in VBA. In this case, in VBA, the Find.Execute should return a new Range, but in AS it just returns true/false. If you look it up in the AS dictionary, it looks as if the execute find should return a "text range/insertion point" but what it actually returns is what is stated in the description at the top of the entry for "execute find", i.e. a boolean. The find object does not have a range. So it is difficult to see how to get the range of the thing you just found. As far as I can tell, using execute find in AS with a find object derived from a range object is only useful in the situation where you can specify the replacment object and do the entire replace in the execute.
It might also be useful to read Matt Neuberg's comments in this conversation
I am trying to create a VB script that searches through a column of dates and returns the address of the cell with todays date.
For some reason I keep getting an "Object required: 'FoundCell'" error.
Could someone have a look at my code and correct me?
I can read out the date using WScript.Echo, but once I use it in the find function it immediately gives me the error.
Set oExcel = CreateObject("Excel.Application")
Set wshShell = CreateObject("Wscript.Shell")
File_Path = "D:\Work\Personal Timemanagement\test.xlsx"
Set oData = oExcel.Workbooks.Open(File_Path)
WHAT_TO_FIND = Date()
WScript.Echo WHAT_TO_FIND
Set FoundCell = oData.Worksheets("tst2").Range("A1:A40").Find(WHAT_TO_FIND)
oExcel.Cells(4,4) = FoundCell.Address
oExcel.ActiveWorkbook.SaveAs "D:\Work\Personal Timemanagement\test2.xlsx"
oExcel.ActiveWorkbook.Close
oExcel.Application.Quit
WScript.Quit
Thanks for the help!
WHAT_TO_FIND1 returns value like #14/10/2014#.So replace the # with nothing using WHAT_TO_FIND1=Replace(WHAT_TO_FIND,"#","Nothing").
Once replaced the above code will work
I tried your script, works perfect for me.
For testing purposes I suggest you add the following line to the script
oExcel.Visible = True
Also make sure all the privies instances of Excel are closed so your script could get write access. (open task manager and end all Excel process - unless you have other excel files open)
Now make sure the Worksheet is spelled correct "tst2" also ensure that the range is correct "A1:A40"
Keep us posted.
Find() returns Nothing when the given value isn't found in the given range. Make sure that the Range A1:A40 on sheet tst2 in the workbook D:\Work\Personal Timemanagement\test.xlsx actually contains a cell with the current date, and that the cell is also formatted as a date. Find() won't return a match if for instance you're looking for a date 7/11/2013 and the range contains a cell (formatted as text) with the string 7/11/2013. Modify your statement like this for finding "text" cells:
Set FoundCell = oData.Worksheets("tst2").Range("A1:A40").Find(CStr(WHAT_TO_FIND))