in reply to InsertSymbol usage to modify Word document

You don't tell us how the insertsymbol line failed. By using Google for document.insertsymbol, I found Win32::ole and MSWord, which seems to suggest that the method would be called InsertSymbol, and this seems also to be the usage in this Microsoft reference. To pass named arguments to a method call, the documentation says to use a hash reference. You also pass the string True for the Unicode parameter. That's certainly not wha the translation steps say. You should try passing a 1 or -1 as that parameter. So you should try the following method call, and maybe consider providing us with more exact information on how things fail for you so we can help you better:

$selection->InsertSymbol( { Font=> "Wingdings", CharacterNumber => -40 +63, Unicode => 1 });

Replies are listed 'Best First'.
Re^2: InsertSymbol usage to modify Word document
by merrymonk (Hermit) on Jan 01, 2009 at 12:13 UTC
    I am sorry that I did not explain how it failed but simply it did not work and I did not get any error message!
    However, many thanks for your comments because I tried your suggestion and for the first time I got a wingding character in my Word document!!
    Almost of course, this leads to the next problem. How to get the character in the correct place?
    I can ‘see’ VBA word examples of inserting text at a particular position but I am struggling both to understand how to convert this to Perl and more importantly how to adapt this to get the wingding at the right place.
    What I could use is something like
    1. have a unique character string in the Word document;
    2. find that string;
    3. remove the string (perhaps by simply replacing it by ‘nothing’)
    4. add the wingding at that position;
    I have already got code that works for steps 2 and 3. I did hope that this would make the ‘current’ position (or whatever is required) to be where the original string was in the documents (so that the wingding symbol would be inserted at this point).
    Therefore I simply added the now working InsertSymbol line after these steps as shown next.
    $search_res = $search-> {Text} = "\x{0021}"; $replace-> {Text} = "\x{0022}"; $exec_res = $search-> Execute({Replace => wdReplaceAll}); $selection->InsertSymbol( { Font=> "Wingdings", CharacterNumber => -40 +63, Unicode => 1 });
    I used the lines on a Word file that contained an ! (the hex code 0021). This did replace the ! with a “ (hex code 0022). However, the wingding character was placed at the beginning of the document.
    How do I ‘set’ the position for the wingding character to be where the changed character string was in the original document?

      I'm not an expert on the Word Object Model, but I did find Word Object Model Reference, which looks worth a read (if only as a cure for insomnia).

      I cannot see a property of the Find Object which gives the position of a successful find... I do, however, note that you can set a Font property. You can also set the Font property of the Replacement Object. My guess is that you can use the Unicode "private use" character values used for Wingdings (ie 0xF000 + 0x21 ("\x{F021}") for Wingdings "pencil at 45 degrees") plus the font name for find or replace.

      I note also that a Selection Object can have a Find property... which looks plausible ?

        Thank you for everyone’s suggestions. I have looked at the various references and also ‘googled' for a solution.
        Frustratingly I found places where the insertion point was mentioned and even questions about how to ‘set’ it
        but no answers using either VBA or Perl code.
        Therefore I used the recording macro facility with Word to locate a character hoping that this would give a clue about what to do. This gave the following
        Selection.Find.ClearFormatting With Selection.Find .Text = "!" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.MoveRight Unit:=wdCharacter, Count:=1 Application.Run MacroName:="Normal.NewMacros.insertsymbol"
        I was not really sure what to do but I did make an attempt and included it in another Perl test.
        I ignored the last line and simply used what I knew should work to add a wingding character. The code is below
        use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; # set $filepath to the full path to the direcotry where the file to be + changed, test.doc, is stored my $filepath = 'C:\tmp'; my $oldfile = $filepath . '\wingding1.doc'; my $newfile = $filepath . '\wingding1-edit.doc'; my $word = Win32::OLE-> GetActiveObject('Word.Application') || Win32::OLE-> new('Word.Application','Quit'); my $doc = $word-> Documents->Open("$oldfile"); # is application visible 0=no 1=yes $word-> {visible} = 0; my $selection = $word->Selection; # select current position # start of Word macro conversion $selection->Find->ClearFormatting; $selection->Find ( Text => "!", ReplacementText => "", Forward => 1, Wrap => wdFindContinue, Format => 0, MatchCase => 0, MatchWholeWord => 0, MatchWildcards => 0, MatchSoundsLike => 0, MatchAllWordForms => 0); $selection->Find->Execute; $selection->MoveRight ( Unit => wdCharacter, Count => 1); $selection->InsertSymbol( { Font=> "Wingdings", CharacterNumber => -40 +63, Unicode => 1 }); # save word file $word-> ActiveDocument->SaveAs($newfile); # close word file $doc-> Close(); $word-> Quit();
        I used this with a document with a ! towards the end of the first line. Sadly this still put the inserted
        wingding at the beginning of the document. I even tried with just Text => "!" in the ->Find
        (partly because I was not at all sure that Replacement.Text should be converted as
        ReplacementText) but this also did not work.
        Has any Monk an idea how to make this work?

      You need to set $selection to be where you want to insert your symbol. However you do that with Word. I suggest reading the Word documentation on how to set the selection to a specific position, or even better, how to set the selection to a string that was searched.