Problems with Python for Windows extensions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KK

    #1

    Problems with Python for Windows extensions

    the code below is taken from M$ technet as an example on using vb
    script to do a replace all in word:

    Const wdReplaceAll = 2

    Set objWord = CreateObject("W ord.Application ")
    objWord.Visible = True

    Set objDoc =
    objWord.Documen ts.Open("K:\Dev elopment\Fabric base\prod\Test. doc")
    Set objSelection = objWord.Selecti on

    objSelection.Fi nd.Text = "Contoso"
    objSelection.Fi nd.Forward = True
    objSelection.Fi nd.MatchWholeWo rd = True

    objSelection.Fi nd.Replacement. Text = "Fabrikam"
    objSelection.Fi nd.Execute ,,,,,,,,,,wdRep laceAll




    I did a rewrite and made it pythonic:

    from win32com.client import *

    wdReplaceAll = 2

    objWord = Dispatch("Word. Application")
    objWord.Visible = True

    objDoc =
    objWord.Documen ts.Open("K:\Dev elopment\Fabric base\prod\Test. doc")
    objSelection = objWord.Selecti on

    objSelection.Fi nd.Text = "Contoso"
    objSelection.Fi nd.Forward = True
    objSelection.Fi nd.MatchWholeWo rd = True

    objSelection.Fi nd.Replacement. Text = "Fabrikam"
    objSelection.Fi nd.Execute (Replace = wdReplaceAll)


    However, the document juz loaded up in word but no action was taken. I
    am using Word 2003. Any ideas?

  • vincent wehren

    #2
    Re: Problems with Python for Windows extensions

    "KK" <RayFLau@gmail. com> schrieb im Newsbeitrag
    news:1125760475 .488680.17070@z 14g2000cwz.goog legroups.com...
    | the code below is taken from M$ technet as an example on using vb
    | script to do a replace all in word:
    |
    | Const wdReplaceAll = 2
    |
    | Set objWord = CreateObject("W ord.Application ")
    | objWord.Visible = True
    |
    | Set objDoc =
    | objWord.Documen ts.Open("K:\Dev elopment\Fabric base\prod\Test. doc")
    | Set objSelection = objWord.Selecti on
    |
    | objSelection.Fi nd.Text = "Contoso"
    | objSelection.Fi nd.Forward = True
    | objSelection.Fi nd.MatchWholeWo rd = True
    |
    | objSelection.Fi nd.Replacement. Text = "Fabrikam"
    | objSelection.Fi nd.Execute ,,,,,,,,,,wdRep laceAll
    |
    |
    |
    |
    | I did a rewrite and made it pythonic:
    |
    | from win32com.client import *
    |
    | wdReplaceAll = 2
    |
    | objWord = Dispatch("Word. Application")
    | objWord.Visible = True
    |
    | objDoc =
    | objWord.Documen ts.Open("K:\Dev elopment\Fabric base\prod\Test. doc")
    | objSelection = objWord.Selecti on
    |
    | objSelection.Fi nd.Text = "Contoso"
    | objSelection.Fi nd.Forward = True
    | objSelection.Fi nd.MatchWholeWo rd = True
    |
    | objSelection.Fi nd.Replacement. Text = "Fabrikam"
    | objSelection.Fi nd.Execute (Replace = wdReplaceAll)
    |
    |
    | However, the document juz loaded up in word but no action was taken. I
    | am using Word 2003. Any ideas?

    KK,

    Your example seemed to work fine for me (Python2.4, Pythonwin build 204,
    Word 2003)

    One thing: since you say your document loads up fine I don't know if it is
    just a typo, but make sure you follow the rules of backslash literals in
    path names. In other words:
    "K:\Development \Fabricbase\pro d\Test.doc" should read either

    r"K:\Developmen t\Fabricbase\pr od\Test.doc" (note the leading r for raw
    string
    or

    "K:\\Developmen t\\Fabricbase\\ prod\\Test.doc"
    or

    "K:/Development/Fabricbase/prod/Test.doc"

    --

    Vincent Wehren





    Comment

    • KK

      #3
      Re: Problems with Python for Windows extensions

      thx for ur reply
      u r rite that i should use a raw string, but that doesn't solve the
      problem
      i am q annoyed by this strange behaviour.
      i tried to run the script on my friend's pc, which is python2.4 + pywin
      204 + office 2000, but same thing happened

      now i am thinking to generate a vbs from python and run it. i know it
      is dumb but i dont know other solutions...

      Comment

      • Kartic

        #4
        Re: Problems with Python for Windows extensions

        Hi,

        Invoking Execute as shown below works.. I have no explanation why your
        VB to Py converted code did not work.

        wdFindContinue = 1
        objSelection.Fi nd.Execute('Con tosa', False, True, False, False, True,
        True, wdFindContinue, True, 'Fabrikam', wdReplaceAll, False, False,
        False, False)

        This finds 'Contosa' and replaces all occurances with 'Fabricam'.

        For a full explanation about the arguments to Execute, look it up in the
        VBA Help.

        Thanks,
        -Kartic


        The Great 'KK' uttered these words on 9/3/2005 11:14 AM:[color=blue]
        > the code below is taken from M$ technet as an example on using vb
        > script to do a replace all in word:
        >
        > Const wdReplaceAll = 2
        >
        > Set objWord = CreateObject("W ord.Application ")
        > objWord.Visible = True
        >
        > Set objDoc =
        > objWord.Documen ts.Open("K:\Dev elopment\Fabric base\prod\Test. doc")
        > Set objSelection = objWord.Selecti on
        >
        > objSelection.Fi nd.Text = "Contoso"
        > objSelection.Fi nd.Forward = True
        > objSelection.Fi nd.MatchWholeWo rd = True
        >
        > objSelection.Fi nd.Replacement. Text = "Fabrikam"
        > objSelection.Fi nd.Execute ,,,,,,,,,,wdRep laceAll
        >
        >
        >
        >
        > I did a rewrite and made it pythonic:
        >
        > from win32com.client import *
        >
        > wdReplaceAll = 2
        >
        > objWord = Dispatch("Word. Application")
        > objWord.Visible = True
        >
        > objDoc =
        > objWord.Documen ts.Open("K:\Dev elopment\Fabric base\prod\Test. doc")
        > objSelection = objWord.Selecti on
        >
        > objSelection.Fi nd.Text = "Contoso"
        > objSelection.Fi nd.Forward = True
        > objSelection.Fi nd.MatchWholeWo rd = True
        >
        > objSelection.Fi nd.Replacement. Text = "Fabrikam"
        > objSelection.Fi nd.Execute (Replace = wdReplaceAll)
        >
        >
        > However, the document juz loaded up in word but no action was taken. I
        > am using Word 2003. Any ideas?
        >[/color]

        Comment

        • KK

          #5
          Re: Problems with Python for Windows extensions

          Hello,
          I guess you could reproduce my problem, Kartic. I have tried the one u
          suggested, but sadly it didn't work for me. I think the COM of pywin is
          quite tricky, or it might be a bug. I have some friends who also had
          experience of weird behaviors of pywin32, which makes me skeptical of
          using it in real app.

          Thanks
          KK

          Comment

          • Kartic

            #6
            Re: Problems with Python for Windows extensions

            The Great 'KK' uttered these words on 9/7/2005 7:57 AM:[color=blue]
            > Hello,
            > I guess you could reproduce my problem, Kartic. I have tried the one u
            > suggested, but sadly it didn't work for me. I think the COM of pywin is
            > quite tricky, or it might be a bug. I have some friends who also had
            > experience of weird behaviors of pywin32, which makes me skeptical of
            > using it in real app.
            >
            > Thanks
            > KK
            >[/color]

            Actually, I have created some robust win32com applications (using Word
            and Excel) that work consistently on all installed machines.

            Could it be an Office 2003 quirk? Did some other app of yours crash
            while using COM? Such crashes could produce unpredictable results in COM
            related code, from my experience.

            So, if you think it is worth your time, you could probably investigate
            it a bit more or better contact Mark Hammond to see if he can help. I
            don't know if there is a win32com mailing list; if there is one, please
            post your question there too.

            Thanks,
            -Kartic

            Comment

            Working...