MS Word, VBA, ShapeRange Transparency

Get help with using AutoHotkey and its commands and hotkeys
mslonik
Posts: 32
Joined: 21 Feb 2019, 04:38
Location: Poland

MS Word, VBA, ShapeRange Transparency

27 Mar 2019, 11:06

Dear Forum,

I try to convert the following code from VBA to AHK, which inserts water mark to chosen section of a document:

Code: Select all

     ActiveDocument.Sections(1).Range.Select
     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
     Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject73958794, "DRAFT", "Calibri", 200, False, False, 0, 0).Select
     Selection.ShapeRange.Name = "PowerPlusWaterMarkObject73958794"
     Selection.ShapeRange.TextEffect.NormalizedHeight = False
     Selection.ShapeRange.Line.Visible = False
     Selection.ShapeRange.Fill.Visible = True
     Selection.ShapeRange.Fill.Solid
     Selection.ShapeRange.Fill.ForeColor.RGB = RGB(217, 217, 217)
     Selection.ShapeRange.Fill.Transparency = 0.5
     Selection.ShapeRange.Rotation = 315
     Selection.ShapeRange.LockAspectRatio = True
     Selection.ShapeRange.Height = CentimetersToPoints(8.62)
     Selection.ShapeRange.Width = CentimetersToPoints(18.94)
     Selection.ShapeRange.WrapFormat.AllowOverlap = True
     Selection.ShapeRange.WrapFormat.Side = wdWrapNone
     Selection.ShapeRange.WrapFormat.Type = 3
     Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
     Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
     Selection.ShapeRange.Left = wdShapeCenter
     Selection.ShapeRange.Top = wdShapeCenter
     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
The following AHK code works except two lines which are commented out:

Code: Select all

	oWord := ComObjActive("Word.Application")
	 oWord.ActiveDocument.Sections(1).Range.Select
	oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader := 9
	oWord.Selection.HeaderFooter.Shapes.AddTextEffect(0, "DRAFT", "Calibri", 200, WordFalse, WordFalse, 0, 0).Select 
	oWord.Selection.ShapeRange.TextEffect.NormalizedHeight := WordFalse
	oWord.Selection.ShapeRange.Line.Visible := WordFalse
	oWord.Selection.ShapeRange.Fill.Visible := WordTrue
	oWord.Selection.ShapeRange.Fill.Solid
	oWord.Selection.ShapeRange.Fill.ForeColor := 0xD9D9D9 ; .RGB(217, 217, 217)
    ;~ oWord.Selection.ShapeRange.Fill.Transparency := 0.5
    ;~ oWord.Selection.ShapeRange.Fill.Transparency := ComObject(VT_R4, 0.5)
        oWord.Selection.ShapeRange.Rotation := 315
        oWord.Selection.ShapeRange.LockAspectRatio := WordTrue
        oWord.Selection.ShapeRange.Height.CentimetersToPoints(8.62)
        oWord.Selection.ShapeRange.Width.CentimetersToPoints(18.94)
        oWord.Selection.ShapeRange.WrapFormat.AllowOverlap := WordTrue
        oWord.Selection.ShapeRange.WrapFormat.Side := wdWrapNone := 3
        oWord.Selection.ShapeRange.WrapFormat.Type := 3
        oWord.Selection.ShapeRange.RelativeHorizontalPosition := wdRelativeVerticalPositionMargin := 0
        oWord.Selection.ShapeRange.RelativeVerticalPosition := wdRelativeVerticalPositionMargin := 0
        oWord.Selection.ShapeRange.Left := wdShapeCenter := -999995 
        oWord.Selection.ShapeRange.Top := wdShapeCenter := -999995
        oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument := 0
        oWord := "" ; Clear global COM objects when done with them
If the first line isn't commented out, I get the following error:

Code: Select all

Error: 0x80020005 - Type mismatch
Specifically: Transparency
If the second line isn't commented out, I get the following error:

Code: Select all

Error: 0x800401F3 - Improper string of class
What is wrong with those lines?

VBA object browser: FillFormat -> Transparency, Property Transparency As Single, Member of Word.FillFormat

Kind regards, mslonik
FanaticGuru
Posts: 1356
Joined: 30 Sep 2013, 22:25

Re: MS Word, VBA, ShapeRange Transparency

27 Mar 2019, 12:36

This seems to work for me.

Code: Select all

WordTrue := ComObj(0xB, -1) ; 0xB = VT_Bool || -1 = true
WordFalse := ComObj(0xB, 0) ; 0xB = VT_Bool || 0 = false


oWord := ComObjActive("Word.Application")
oWord.ActiveDocument.Sections(1).Range.Select
oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader := 9
oWord.Selection.HeaderFooter.Shapes.AddTextEffect(0, "DRAFT", "Calibri", 200, WordFalse, WordFalse, 0, 0).Select 
oWord.Selection.ShapeRange.TextEffect.NormalizedHeight := WordFalse
oWord.Selection.ShapeRange.Line.Visible := WordFalse
oWord.Selection.ShapeRange.Fill.Visible := WordTrue
oWord.Selection.ShapeRange.Fill.Solid
oWord.Selection.ShapeRange.Fill.ForeColor := 0xD9D9D9 ; .RGB(217, 217, 217)
oWord.Selection.ShapeRange.Fill.Transparency := 0.5
oWord.Selection.ShapeRange.Rotation := 315
oWord.Selection.ShapeRange.LockAspectRatio := WordTrue
oWord.Selection.ShapeRange.Height.CentimetersToPoints(8.62)
oWord.Selection.ShapeRange.Width.CentimetersToPoints(18.94)
oWord.Selection.ShapeRange.WrapFormat.AllowOverlap := WordTrue
oWord.Selection.ShapeRange.WrapFormat.Side := wdWrapNone := 3
oWord.Selection.ShapeRange.WrapFormat.Type := 3
oWord.Selection.ShapeRange.RelativeHorizontalPosition := wdRelativeVerticalPositionMargin := 0
oWord.Selection.ShapeRange.RelativeVerticalPosition := wdRelativeVerticalPositionMargin := 0
oWord.Selection.ShapeRange.Left := wdShapeCenter := -999995 
oWord.Selection.ShapeRange.Top := wdShapeCenter := -999995
oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument := 0
oWord := "" ; Clear global COM objects when done with them

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
mslonik
Posts: 32
Joined: 21 Feb 2019, 04:38
Location: Poland

Re: MS Word, VBA, ShapeRange Transparency

27 Mar 2019, 13:44

Thank you for your prompt reply. Except definitions for constants WordTrue and WordFalse your code is equal to mine. Sorry I didn't post definitions of constants initially. My definitions for these constants are simpler (I didn't yet check if it works with your definitions as it seems to me that those definitions shouldn't interfere with this particular line which is problematic):

Code: Select all

WordTrue := -1
WordFalse := 0
So for you this specific line works:

Code: Select all

oWord.Selection.ShapeRange.Fill.Transparency := 0.5
It doesn't work for me. Any suggestions what could be a reason for that?

My applications:
Microsoft Word 2010
Windows 7 Enterprise

Kind regards, mslonik
burque505
Posts: 1031
Joined: 22 Jan 2017, 19:37

Re: MS Word, VBA, ShapeRange Transparency

27 Mar 2019, 14:13

@mslonik, this code works for me, albeit on Word 2016 64-bi5, Win7 Pro 64-bit. ComObjActive() doesn't work for me so I use the attached class Word_Get.ahk. I changed the Line.Visible boolean to true

Code: Select all

oWord.Selection.ShapeRange.Line.Visible := -1
basically so I could tell what the heck was going on (watermarks are always hard for me to see).

Code: Select all

#Include Word_Get.ahk

oWord := Word_Get()
oWord.ActiveDocument.Sections(1).Range.Select
oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader := 9
oWord.Selection.HeaderFooter.Shapes.AddTextEffect(0, "DRAFT", "Calibri", 200, false, false, 0, 0).Select 
oWord.Selection.ShapeRange.TextEffect.NormalizedHeight := false
oWord.Selection.ShapeRange.Line.Visible := -1
oWord.Selection.ShapeRange.Fill.Visible := 0
oWord.Selection.ShapeRange.Fill.Solid
oWord.Selection.ShapeRange.Fill.ForeColor := 0xD9D9D9 ; .RGB(217, 217, 217)
oWord.Selection.ShapeRange.Fill.Transparency := 0.5
oWord.Selection.ShapeRange.Rotation := 315
oWord.Selection.ShapeRange.LockAspectRatio := -1
oWord.Selection.ShapeRange.Height.CentimetersToPoints(8.62)
oWord.Selection.ShapeRange.Width.CentimetersToPoints(18.94)
oWord.Selection.ShapeRange.WrapFormat.AllowOverlap := -1
oWord.Selection.ShapeRange.WrapFormat.Side := wdWrapNone := 3
oWord.Selection.ShapeRange.WrapFormat.Type := 3
oWord.Selection.ShapeRange.RelativeHorizontalPosition := wdRelativeVerticalPositionMargin := 0
oWord.Selection.ShapeRange.RelativeVerticalPosition := wdRelativeVerticalPositionMargin := 0
oWord.Selection.ShapeRange.Left := wdShapeCenter := -999995 
oWord.Selection.ShapeRange.Top := wdShapeCenter := -999995
oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument := 0
oWord := "" ; Clear global COM objects when done with them
Regards,
burque505
Attachments
Word_Get.ahk
(1.54 KiB) Downloaded 12 times
FanaticGuru
Posts: 1356
Joined: 30 Sep 2013, 22:25

Re: MS Word, VBA, ShapeRange Transparency

27 Mar 2019, 18:50

mslonik wrote:
27 Mar 2019, 13:44
Thank you for your prompt reply. Except definitions for constants WordTrue and WordFalse your code is equal to mine. Sorry I didn't post definitions of constants initially. My definitions for these constants are simpler (I didn't yet check if it works with your definitions as it seems to me that those definitions shouldn't interfere with this particular line which is problematic):

Code: Select all

WordTrue := -1
WordFalse := 0
So for you this specific line works:

Code: Select all

oWord.Selection.ShapeRange.Fill.Transparency := 0.5
It doesn't work for me. Any suggestions what could be a reason for that?

My applications:
Microsoft Word 2010
Windows 7 Enterprise

Kind regards, mslonik

The code exactly as I posted, works for me. Including the line with Transparency. I did the WordTrue := ComObj(0xB, -1) ; 0xB = VT_Bool || -1 = true thing because you were missing the constants and the fact that you did not just use the True and False built into AHK made me think the COM versions might be important. Sometimes they are and sometimes they are not.

I tested on:
Microsoft Word 2010
Windows 10

Used a new blank document with nothing else in it.

I don't really know why I would get a different result.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
mslonik
Posts: 32
Joined: 21 Feb 2019, 04:38
Location: Poland

Re: MS Word, VBA, ShapeRange Transparency

28 Mar 2019, 03:09

Thank you again for replies. I tried all versions of your tips. Problem is still not solved at my PC. Maybe it's time to show you a whole script which I run. I tried to constrain content as much as possible, so I cut out all other content. Even then and even if this script runs as the only one (standalone), still the same error occurs.

Code: Select all

#NoEnv  						; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  							; Enable warnings to assist with detecting common errors.
SendMode Input  				; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%		; Ensures a consistent starting directory.
#SingleInstance force 			; only one instance of this script may run at a time!

; --------------- SECTION OF GLOBAL CONSTANTS -----------------------------
;~ WordTrue := ComObj(0xB, -1) ; 0xB = VT_Bool || -1 = true
;~ WordFalse := ComObj(0xB, 0) ; 0xB = VT_Bool || 0 = false
; --------------- SECTION OF GLOBAL CONSTANTS ----------------------

#Include Word_Get.ahk

=:: 
	tooltip, [F24]  %A_thishotKey% 
	SetTimer, ClearTooltip, -5000
	;~ oWord := ComObjActive("Word.Application")
	oWord := Word_Get()
	oWord.ActiveDocument.Sections(1).Range.Select
	oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader := 9
	;~ oWord.Selection.HeaderFooter.Shapes.AddTextEffect(0, "DRAFT", "Calibri", 200, WordFalse, WordFalse, 0, 0).Select 
	oWord.Selection.HeaderFooter.Shapes.AddTextEffect(0, "DRAFT", "Calibri", 200, 0, 0, 0, 0).Select 
	;~ oWord.Selection.ShapeRange.Name := "PowerPlusWaterMarkObject73958794"
	;~ oWord.Selection.ShapeRange.TextEffect.NormalizedHeight := WordFalse
	oWord.Selection.ShapeRange.TextEffect.NormalizedHeight := 0
	;~ oWord.Selection.ShapeRange.Line.Visible := WordFalse
	oWord.Selection.ShapeRange.Line.Visible := 0
	;~ oWord.Selection.ShapeRange.Fill.Visible := WordTrue
	oWord.Selection.ShapeRange.Fill.Visible := -1
	oWord.Selection.ShapeRange.Fill.Solid
	oWord.Selection.ShapeRange.Fill.ForeColor := 0xD9D9D9 ; .RGB(217, 217, 217)
    oWord.Selection.ShapeRange.Fill.Transparency := 0.5
    ;~ oWord.Selection.ShapeRange.Fill.Transparency := ComObject(VT_R4, 0.5)
    oWord.Selection.ShapeRange.Rotation := 315
    ;~ oWord.Selection.ShapeRange.LockAspectRatio := WordTrue
    oWord.Selection.ShapeRange.LockAspectRatio := -1
    oWord.Selection.ShapeRange.Height.CentimetersToPoints(8.62)
    oWord.Selection.ShapeRange.Width.CentimetersToPoints(18.94)
    ;~ oWord.Selection.ShapeRange.WrapFormat.AllowOverlap := WordTrue
    oWord.Selection.ShapeRange.WrapFormat.AllowOverlap := -1
    oWord.Selection.ShapeRange.WrapFormat.Side := wdWrapNone := 3
    oWord.Selection.ShapeRange.WrapFormat.Type := 3
    oWord.Selection.ShapeRange.RelativeHorizontalPosition := wdRelativeVerticalPositionMargin := 0
    oWord.Selection.ShapeRange.RelativeVerticalPosition := wdRelativeVerticalPositionMargin := 0
    oWord.Selection.ShapeRange.Left := wdShapeCenter := -999995 
    oWord.Selection.ShapeRange.Top := wdShapeCenter := -999995
	oWord.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument := 0
	oWord := "" ; Clear global COM objects when done with them
return	


; ---------------------- SECTION OF LABELS ------------------------------------

ClearTooltip:
	ToolTip ,
return

When I run above code I get the following warnings / errors:

Code: Select all

Warning in #include file
(...)
This variable has not been assigned a value.

Specifically: pacc (a local variable)

Line#

---> 015: if (hr: DllCall("oleacc\ (...)
And then a familiar one error:

Code: Select all

Error: 0x80020005 - Type mismatch
Specifically: Transparency

---? 030: oWord.Selection.ShapeRange.FillTransparency := 0.5
To be more precise about my configuration of software:

Code: Select all

Microsoft Word version: Professional Plus 2010 14.0.7229.5000 (64-bits)

Operating system version: Windows 7 Enterprise, 64-bits

AHK version: 1.1.30.01
I started to think if some Microsoft Office COM extensions can not interfere with AHK? In fact what I discovered, I have numerous COM extensions installed. What do you think about this idea? Or maybe I overlook something else?

Kind regards, mslonik
mslonik
Posts: 32
Joined: 21 Feb 2019, 04:38
Location: Poland

Re: MS Word, VBA, ShapeRange Transparency

28 Mar 2019, 07:19

Dear Forum,

in the mean time I've switched off all COM extensions and then I've restarted Microsoft Word. It didn't solve the issue.

Kind regards, mslonik
mslonik
Posts: 32
Joined: 21 Feb 2019, 04:38
Location: Poland

Re: MS Word, VBA, ShapeRange Transparency

28 Mar 2019, 08:17

Dear Forum,

problem is SOLVED! I can't believe it, but here you go:

1. Code line which didn't work:

Code: Select all

oWord.Selection.ShapeRange.Fill.Transparency := 0.5
Error:

Code: Select all

Error: 0x80020005 - Type mismatch
Specifically: Transparency

2. Code, which works fine:

Code: Select all

oWord.Selection.ShapeRange.Fill.Transparency := 0,5
I didn't mentione earlier, that my operating system settings are country specific. In my mother language meaning of "." is symmetrically exchanged to ",". Also error message doesn't bring a hint in this regard. Even more surprisingly to me one of the next lines of code:

Code: Select all

oWord.Selection.ShapeRange.Height.CentimetersToPoints(8.62)
deals with dot sign "." without any error.

Hopefully my adventure will help somebody else. Thank you for your help and assistance! It's appreciated.

Kind regards, mslonik
burque505
Posts: 1031
Joined: 22 Jan 2017, 19:37

Re: MS Word, VBA, ShapeRange Transparency

28 Mar 2019, 09:48

@mslonik, glad you got it worked out. That's a good tip about country-specific settings! Also, thanks for the tip about the local variable. I've changed the class to include a def of pacc inside the function.
Regards,
burque505
Attachments
Word_Get.ahk
(1.63 KiB) Downloaded 12 times

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 41 guests