Link to home
Start Free TrialLog in
Avatar of HKH1967
HKH1967Flag for Saudi Arabia

asked on

MS word 2007 protected form with a bookmark and macro

I need to build a protected from ms 2007 with a bookmark with a macro that capable to convert figure number to text inside the bookmark.
SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HKH1967

ASKER

Thanks
I have a macro to convert Arabic number figure to arabic text due to that i have to use the bookmark. what i would like is to have a macro that isnert the result in the bookmark.
I have the file if that contain the macro if any one interset?
Best regards,
Hussain
If you already have the macro to do the conversion, then all you need is to call it, reading the text from one bookmark and plugging the result into the other bookmark.

This code assumes that your macro is a function which returns the number in words and accepts the numeric text as an argument.
Sub CallConvertMacro()
    Dim strNumber as String
    Dim strText as string

    strNumber = ActiveDocument.BookMarks("bmkNumber").Range.Text
    strText = MyCodeConverterFunction(strNumber)
    ActiveDocument.BookMarks("bmkNumberText").Range.Text = strText
End Sub
    

Open in new window

Avatar of HKH1967

ASKER

Dear GrahamSkan
I have tried that but I could not succeed, working with ms word 2007 is differing. Can you help me? I need to input the number in one bookmark and get the result in another bookmark.
Many thanks,
NB attached ms word 2003.
 

AraAddIn1.dot
I now see that the field solution wasn't suitable because you need Arabic text. However, I don't think that the Word version makes much difference

I had trouble with the Macro security and couldn't get used to the right-to-left text, so I have created a new document and copied the macro code into it.
25006524.docx
Avatar of HKH1967

ASKER

Dear GrahamSkan
what I have understand from yr last comments that the file contain two bookmarks and macro. I found only 123456 and macro code..
Can you send them again thanks,
Are you sure that there are no bookmarks? There is an option to show bookmark delimiters, or you can select a bookmark by name from the dialogue for inserting bookmarks.
Avatar of HKH1967

ASKER

GrahamSkan:
sorry, the bookmark i found them but what about the marco, I could not found any?
thansk
Sorry, too.
I saved the document as .docx (no macros) instead of .docm and EE won't accept that, so here is a Word 2003 version
25006524.doc
Avatar of HKH1967

ASKER

Dear GrahamSkan
Thanks, in ms word 2007 inviorment protected document macro is working. While runing the macro i have added code to unprotect the dox. but I need to make a loop if I change the figure I need to check if the bookmark named 'strText' is exist or not, if not i need to creat it.

I have run the macro but it is deleting the bookmark 'strText', what I need is to insert the text inside

Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    LoadArrays
    strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
    strText = sDec2Text(strNumber)
    ActiveDocument.Unprotect
    ActiveDocument.Bookmarks("bmkNumberText").Range.Text = strText
   
    ActiveDocument.Protect wdAllowOnlyFormFields

End Sub

Thanks
I usually capture the bookmark range into a variable and use it to set the new text and to add the bookmark again.
Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    Dim rng As Range
    If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         LoadArrays
         strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
         strText = sDec2Text(strNumber)
         ActiveDocument.Unprotect
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
         ActiveDocument.Protect wdAllowOnlyFormFields
    Else
        MsgBox "Bookmark 'bmkNumberText' is missing"
    End If
End Sub

Open in new window

Avatar of HKH1967

ASKER

Dear GrahamSkan

I have input the macro in the strnumber bookmark that result in the getting the text in the 'bmkNumberText' and strNumber show no figure (disappear).

What I need if you can help is that input the figure in 'bmkNumberText' bookmark (figure must show & not disapper) and get the reult in 'bmkNumberText' bookmark.


Can the following code help with getting right-to-left text:

   'Save the cursor movement settings (either logical or visual)
    lCursorMovement = Options.CursorMovement
    'Force the cursor movement to be logical if it is not
    If Options.CursorMovement = wdCursorMovementVisual Then Options.CursorMovement = wdCursorMovementLogical
    LoadArrays
   
    lRange = Selection.MoveWhile(cset:="0123456789.,¡", Count:=wdBackward)
    'Save the alignment of the paragraph representing the digits
    lParaAlignment = Selection.ParagraphFormat.Alignment
    'Force the reading order to be RTL
    Selection.ParagraphFormat.ReadingOrder = RtlPara
    'Reset the alignment to what it originally was
    Selection.ParagraphFormat.Alignment = lParaAlignment
thanks
Hussain
 
I suggest that you set the numeric data into the bookmark range in the same way that we set the text into its bookmark.


    Dim strNumber As String
    Dim rng As Range
    strNumber = "123456" 'for example
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
         rng.Text = strNumber 
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If

Open in new window

Avatar of HKH1967

ASKER

GrahamSkan:

I could not understand the last point, Can you guide me how to merge both code together?
Thanks
Hussain
Sorry, Hussain.

I can't interpret what your snippet is trying to do. This code will insert the number, and then read it out again before converting it to text.
Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    LoadArrays
    strNumber = "012345" 'for example
    If ActiveDocument.ProtectionType <> wdNoProtection Then
         ActiveDocument.Unprotect 'password
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
         rng.Text = strNumber
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
         strText = sDec2Text(strNumber)
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
    Else
        MsgBox "Bookmark 'bmkNumberText' is missing"
    End If
    ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
End Sub

Open in new window

Avatar of HKH1967

ASKER


Dear GrahamSkan

I have done slit change to the macro, I have replace sDec2Text with sNum2Text. but stil I have a problem of:
1. number is being rounded up e.i. 14.50 (fifteen)
2. i need to the fraction in the following format xx/100
I appreciate your great assiatance, thanks,
Hussain


Sub CallConvertMacro()
Dim strNumber As Long
Dim strText As String
LoadArrays
strNumber = "012345" 'for example
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect 'password
End If
If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
' rng.Text = strNumber
'Add bookmark again to ensure that it exists and the new text is contained within it.
ActiveDocument.Bookmarks.Add "bmkNumber", rng
Else
MsgBox "Bookmark 'bmkNumber' is missing"
End If
If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text

strText = sNum2Text(strNumber) & " " ' *** Decimal to be add & "/100" *******************


Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
rng.Text = strText
'Add bookmark again to ensure that it exists and the new text is contained within it.
ActiveDocument.Bookmarks.Add "bmkNumberText", rng
Else
MsgBox "Bookmark 'bmkNumberText' is missing"
End If
ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
End Sub
I can't read the output, but I think that sDec2Text is designed to take a number with a decimal point and to pass it in parts to the sNum2Text function, so if you want to have a number like 12345.67, you would have to use sNum2Text.

If the number that has been supplied as a percentage, you can divide by 100 in the procedure.

This snippet does that, but I can't tell if the result is correct.

Sub CallConvertMacroPercent()
    Dim strNumber As String
    Dim strText As String
    LoadArrays
    strNumber = "012345" 'for example
    If ActiveDocument.ProtectionType <> wdNoProtection Then
         ActiveDocument.Unprotect 'password
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
         rng.Text = strNumber
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         strNumber = CStr(Val(ActiveDocument.Bookmarks("bmkNumber").Range.Text / 100)) '<-- New
         strText = sDec2Text(strNumber)
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
    Else
        MsgBox "Bookmark 'bmkNumberText' is missing"
    End If
    ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HKH1967

ASKER

it work fine