COM Workbook_Close subroutine

COM Workbook_Close subroutine

I am unsure about how to use some of the features in the Workbook_Close subroutine of the COM interface that is generated by the Fortran Module Wizard. Specifically, the subroutine is defined as:SUBROUTINE $Workbook_Close($OBJECT, SaveChanges, Filename, RouteWorkbook, $STATUS)I would like to set the SaveChages variable to false. I think that this will avoid an Excel prompt that asks whether I would like to save changes to an excel file each time I access it from Fortran.
The SaveChanges variable is defined as:TYPE (VARIANT), INTENT(IN), OPTIONAL :: SaveChangesI realise that I should add code into my mainprogram that sets up a pointer, but am unsure what this code should be. The AutoDice example indicates that the following code should be used in relation to a string variant:vBSTR1%VT = VT_BSTR bstr1 = ConvertStringToBSTR("A2") vBSTR1%VU%PTR_VAL = bstr1Unfortunately, I suspect that the SaveChanges variable should be defined as a logical, and I have been unable to find an example of how to code this up.Many thanks in advance for any help.

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

If you look at the definition of type VARIANT in IFWINTY you'll see this:

          TYPE VARIANT
            SEQUENCE
                INTEGER*2       VT
                INTEGER*2       RESERVED1
                INTEGER*2       RESERVED2
                INTEGER*2       RESERVED3
                RECORD /VARIANT_UNION/ VU
          END TYPE VARIANT
          INTEGER*2, PARAMETER :: VARIANT_BOOL_TRUE = -1
          INTEGER*2, PARAMETER :: VARIANT_BOOL_FALSE = 0
          TYPE (VARIANT) :: OPTIONAL_VARIANT

This tells me that you want something like this:

SaveChanges%VT = VARIANT_BOOL_FALSE

I have not tested this.

Retired 12/31/2016

Worked perfectly - thanks, once again, Steve.

Best Reply

Correct me if I'm wrong, but I think the VARIANT_BOOL_xxx constant should be assigned to the component that represents the variant's value, with the type being set to VT_BOOL, i.e.

SaveChanges%VT = VT_BOOL
SaveChanges%VU%BOOL_VAL = VARIANT_BOOL_FALSE

Ha - I was just about to update this, as the code compiled, but didn't work. You are right - a shame I didn't read your message sooner, as I have just spent 30 minutes sorting this out.

SUBROUTINE CloseExelwithoutSaving(status) 

	    IMPLICIT NONE

	    INTEGER(4),intent(out)               :: status

	    type(VARIANT)                        :: SaveChanges

	    status=-1

	    if(workbook.eq.0) return

	    SaveChanges%VT = VT_BOOL

	    SaveChanges%VU%BOOL_VAL = VARIANT_BOOL_FALSE

	    !call $Workbook_Close(workbook, SaveChanges, Filename, RouteWorkbook, $STATUS)

	    call $Workbook_Close(workbook, SaveChanges=SaveChanges, $STATUS=status)

	    if(status.ne.0) return

	    call Workbooks_Close(workbooks, status)

	END SUBROUTINE CloseExelwithoutSaving

Anyone solve this problem?  With the code above (the undeclared stuff is from the module variables BTW) the $Workbook_Close closes the worksheet leaving and empty excel window. Adding the Workbooks_Close closes the excel session but *only* when I exit the application that created it!

aNY

Leave a Comment

Please sign in to add a comment. Not a member? Join today