Answered by:
Macros for Word

Question
-
Hi,
I need to split the number of bits in Word. For example, 1000000 => 1 000 000, 00. I don't found standart feature. But i found this code:
Dim SelectionStart As Long
Dim SelectionEnd As Long
SelectionStart = Selection.Start
SelectionEnd = Selection.End
Selection.EndKey wdStory, wdMove
With Selection.Find
.Text = "[0-9]{4;}"
.MatchWildcards = True
.Forward = False
While .Execute
If Not .Parent.Previous Is Nothing Then
If .Parent.Previous.Text <> "," Then
.Parent.Text = FormatNumber(CDbl(.Parent.Text), 2, GroupDigits:=vbTrue)
Selection.Collapse wdCollapseStart
End If
Else
.Parent.Text = FormatNumber(CDbl(.Parent.Text), 2, GroupDigits:=vbTrue)
End If
Wend
Selection.SetRange SelectionStart, SelectionEndIt's not bad. And I also want to add to this code at the bottom: "ActiveDocument.SaveAs" - no problem.
Then, i enter my number, click on the button, which contain this code, number has transformed. SaveDialog has also opend, then click "save" and have "Run-time error: 4198. Command error".Then click "Debug", file closed and appear on my Desktop.
Can you tell me, what is cuase of this problem, how to hide this Error???
Thank you for reply :)
- Edited by Ivan.T Saturday, December 6, 2014 11:54 AM
Saturday, December 6, 2014 11:38 AM
Answers
-
The following works fine for me:
Sub Demo()
Application.ScreenUpdating = False
Dim StrPath As String
StrPath = "C:\Users\" & Environ("Username") & "\Documents\"
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<[0-9]{4;}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
.Text = Replace(Format(.Text, "#,##0"), ",", " ") & ",00"
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
.SaveAs FileName:=StrPath & "MyFile.docx", FileFormat:=wdFormatXMLDocument
End With
Application.ScreenUpdating = True
End SubThe new file's name is 'MyFile.docx'.
Cheers
Paul Edstein
[MS MVP - Word]- Edited by macropodMVP Sunday, December 7, 2014 6:05 AM
- Marked as answer by Ivan.T Tuesday, December 16, 2014 9:53 AM
Sunday, December 7, 2014 6:05 AM
All replies
-
Hi Ivan,
Tried your code and it seems to work just fine with one exception. I don't know which version you are using but i tried Word 2010. That doesn't have ActiveDocument.SaveAs but ActiveDocument.SaveAs2
Althought you should have seen that i guess because otherwise it would not have compiled. When you choose SaveAs in the dialog do you save the document as .docm? because that might cause the issue.
Maurice
Saturday, December 6, 2014 12:20 PM -
Hi Maurise,
Thank you for reply. I use Word 2013. I tried to save as .docm and as .docx, but one result.
Saturday, December 6, 2014 6:14 PM -
The following works fine for me:
Sub Demo()
Application.ScreenUpdating = False
Dim StrPath As String
StrPath = "C:\Users\" & Environ("Username") & "\Documents\"
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<[0-9]{4;}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
.Text = Replace(Format(.Text, "#,##0"), ",", " ") & ",00"
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
.SaveAs FileName:=StrPath & "MyFile.docx", FileFormat:=wdFormatXMLDocument
End With
Application.ScreenUpdating = True
End SubThe new file's name is 'MyFile.docx'.
Cheers
Paul Edstein
[MS MVP - Word]- Edited by macropodMVP Sunday, December 7, 2014 6:05 AM
- Marked as answer by Ivan.T Tuesday, December 16, 2014 9:53 AM
Sunday, December 7, 2014 6:05 AM