open(.xls file, 'w') so that hyperlinks appear

Lemniscate d_blade8 at hotmail.com
Tue Nov 13 00:02:19 EST 2001


Okay, my other post should be up soon, but I thought I would throw
this out.  First off, I should say I am not a VB person, so I have to
work at converting VB code into win32client compatible commands. 
However, the Help File in the MS Excel Help has me stumpted.  Here is
the page I am using as a basis and afterwards is just a cut and paste
of my attempts (at an interactive prompt) to insert a hyper link.
Hyperlinks Collection Object
                        


 


Represents the collection of hyperlinks for a worksheet or range. Each
hyperlink is represented by a Hyperlink object.

Using the Hyperlinks Collection

Use the Hyperlinks property to return the Hyperlinks collection. The
following example checks the hyperlinks on worksheet one for a link
that contains the word “Microsoft.”

For Each h in Worksheets(1).Hyperlinks
    If Instr(h.Name, "Microsoft") <> 0 Then h.Follow
Next
Use the Add method to create a hyperlink and add it to the Hyperlinks
collection. The following example creates a new hyperlink for cell E5.

With Worksheets(1)
    .Hyperlinks.Add .Range("E5"), "http://www.gohere.com"
End With


Okay, here is my interactive attempts.  Hopefully, seeing these will
help you see what I am doing wrong (I am SURE it is something simple,
but it's ticking me off).  Also, as a side note, in PythonWin, I
cannot run a Commakepy on Excel 9 (I'll add that attempt to the bottom
of my code so you can all see what I mean).  Thanks a bunch.



fPythonWin 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH at ActiveState.com) -
see 'Help/About PythonWin' for further copyright information.
>>> ro
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
NameError: name 'ro' is not defined
>>> 
>>> from win32com.client import Dispatch
>>> xlApp = Dispatch("Excel.Application")
>>> xlApp.Workbooks.Add()
<COMObject Add>
>>> OK = xlApp.Workbooks(1).Sheets(1)
>>> OK.Range("B2:C5").Value = "Range Insert"
>>> xlApp.Visible = 1
>>> xlApp.Hyperlinks.Add.Cells("B5"), 'http://www.gohere.com"
Traceback (  File "<interactive input>", line 1
    xlApp.Hyperlinks.Add.Cells("B5"), 'http://www.gohere.com"
                                                                  ^
SyntaxError: invalid token
>>> xlApp.Hyperlinks.Add.Cells("B5"), 'http://www.gohere.com'
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python21\win32com\client\dynamic.py", line 438, in
__getattr__
    raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Excel.Application.Hyperlinks
>>> xlApp.Hyperlinks.Add.Cells("B5") =  'http://www.gohere.com'
Traceback (SyntaxError: can't assign to function call
>>> 
>>> xlApp.Hyperlinks.Add.Cells("B5").Value =  'http://www.gohere.com'
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python21\win32com\client\dynamic.py", line 438, in
__getattr__
    raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Excel.Application.Hyperlinks
>>> OK.Hyperlinks.Add.Cells("B5").Value =  'http://www.gohere.com'
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Cells
>>> OK.Hyperlinks.Add.Cells("B5"), 'http://www.gohere.com'
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Cells
>>> xlApp.Workbooks(1).Hyperlinks.Add.Range("B5"),
"http:\\www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python21\win32com\client\dynamic.py", line 438, in
__getattr__
    raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: <unknown>.Hyperlinks
>>> xlApp.Worksheets(1).Hyperlinks.Add.Range("B5"),
"http:\\www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Range
>>> xlApp.Worksheets(1).Hyperlinks.Add.Range("B5") =
"http:\\www.gohere.com"
Traceback (SyntaxError: can't assign to function call
>>> xlApp.Worksheets(1).Hyperlinks.Add.Range("B5").Value =
"http:\\www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Range
>>> xlApp.Worksheets(1).Hyperlinks.Add.Range("B5"),
"http:\\www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Range
>>> xlApp.Worksheets(1).Hyperlinks.Add.Ranges("B5"),
"http:\\www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Ranges
>>> xlApp.Worksheets(1).Hyperlinks.Add.Cells("B5"),
"http:\\www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Cells
>>> xlApp.Worksheets(1).Hyperlinks.Add("http:\\goher.com").Cells("B5")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<COMObject <unknown>>", line 2, in Add
ValueError: argument is not a COM object
>>> xlApp.Worksheets(1).Hyperlinks.Add(Cells("B5"),
"http:\\www.gohere.com")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
NameError: name 'Cells' is not defined
>>> xlApp.Worksheets(1).Hyperlinks.Add(.Cells("B5"),
"http:\\www.gohere.com")
Traceback (  File "<interactive input>", line 1
    xlApp.Worksheets(1).Hyperlinks.Add(.Cells("B5"),
"http:\\www.gohere.com")
                                       ^
SyntaxError: invalid syntax
>>> OK
<COMObject <unknown>>
>>> print OK
<COMObject <unknown>>
>>> print "%s" % OK
<COMObject <unknown>>
>>> OK.Hyperlinks.Add .Range("E5"), "http://www.gohere.com"
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: Range
>>> xlApp.Worksheets(1).Hyperlinks.Add(.Range("B5"),
"http:\\www.gohere.com")
Traceback (  File "<interactive input>", line 1
    xlApp.Worksheets(1).Hyperlinks.Add(.Range("B5"),
"http:\\www.gohere.com")
                                       ^
SyntaxError: invalid syntax
>>> 
>>> OK.Hyperlinks.Add("http:\\hek.com")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<COMObject <unknown>>", line 2, in Add
ValueError: argument is not a COM object
>>> OK.Hyperlinks.Add(OK.Cells("B4"))
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python21\win32com\client\dynamic.py", line 151, in __call__
    return self._get_good_object_(apply(self._oleobj_.Invoke,allArgs),self._olerepr_.defaultDispatchName,None)
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None,
0, -2147352571), 1)
>>> OK.Hyperlinks.Add(OK.Cells("B4"), "http://getalife.com")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python21\win32com\client\dynamic.py", line 151, in __call__
    return self._get_good_object_(apply(self._oleobj_.Invoke,allArgs),self._olerepr_.defaultDispatchName,None)
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None,
0, -2147352571), 1)
>>> Generating to C:\Python21\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x3.py
Failed to execute command:
from win32com.client import makepy;makepy.main()
Traceback (most recent call last):
  File "C:\Python21\Pythonwin\pywin\framework\toolmenu.py", line 103,
in HandleToolCommand
    exec "%s\n" % pyCmd
  File "<string>", line 1, in ?
  File "C:\Python21\win32com\client\makepy.py", line 350, in main
    GenerateFromTypeLibSpec(arg, f, verboseLevel = verboseLevel,
bForDemand = bForDemand, bBuildHidden = hiddenSpec)
  File "C:\Python21\win32com\client\makepy.py", line 254, in
GenerateFromTypeLibSpec
    gen.generate(fileUse, bForDemand)
  File "C:\Python21\win32com\client\genpy.py", line 692, in generate
    self.do_generate()
  File "C:\Python21\win32com\client\genpy.py", line 746, in
do_generate
    oleItems, enumItems, recordItems, vtableItems =
self.BuildOleItemsFromType()
  File "C:\Python21\win32com\client\genpy.py", line 604, in
BuildOleItemsFromType
    newItem = DispatchItem(info, attr, doc)
  File "C:\Python21\win32com\client\genpy.py", line 227, in __init__
    build.DispatchItem.__init__(self, typeinfo, attr, doc)
  File "C:\Python21\win32com\client\build.py", line 107, in __init__
    self.Build(typeinfo, attr, bForUser)
  File "C:\Python21\win32com\client\build.py", line 269, in Build
    self._AddFunc_(typeinfo,fdesc,bForUser)
  File "C:\Python21\win32com\client\build.py", line 176, in _AddFunc_
    typerepr, resultCLSID, resultDoc = _ResolveType(typerepr,
typeinfo)
  File "C:\Python21\win32com\client\build.py", line 440, in
_ResolveType
    subrepr, sub_clsid, sub_doc = _ResolveType(subrepr, itypeinfo)
  File "C:\Python21\win32com\client\build.py", line 452, in
_ResolveType
    resultTypeInfo = itypeinfo.GetRefTypeInfo(subrepr)
com_error: (-2147312566, 'Error loading type library/DLL.', None,
None)
>>> 
>>>



More information about the Python-list mailing list