how to programmatically check protection on a word document vbscript - vbscript

I'm using a vbscript to programmatically run through a list of word documents, open each one, modify it, then save it using ms word 2010. My problem is there are protected and unprotected documents in the list. When I reach a protected document, I get this error: This method or property is not available because the object refers to a protected area of the document.
So I did some research and found this code on the ms website:
If objDoc.ProtectionType <> wdNoProtection Then
objDoc.Unprotect
End if
Now the problem is a new error: The unprotect method or property is not available because the document is already unprotected. Is there a another way to check if the document is protected or unprotected, when you know there will be both in the list, to avoid the errors?

If you automate MS Office applications using VBScript (and use VBA sample code as a starting point), you may overlook the necessity of defining the wd*, xl*, or ad(?)* constants that are predefined in VBA, but missing in VBScript. Use the Docs and/or Debug output to add lines like:
Const wdNoProtection = <correct value>
to your script.
If you start your script with Option Explicit and either avoid the evil global On Error Resume Next entirely or at least disable it until the program is tested, you won't miss any of these beasts.
An even better approach is to write your script as a .wsf file. The <reference> tag 'includes' the definitions (so you can't be blamed for an incorrect Const line).
POC/Demo code:
<?xml version="1.0" standalone="yes" encoding="iso-8859-1" ?>
<package>
<job id="QEC">
<reference object="Excel.Sheet" reference="true"/>
<script language="VBScript">
<![CDATA[
' ############################################################################
a = inputbox ( "Name of an Excel Constant?" )
msgbox a & " = " & eval(a)
' ############################################################################
]]>
</script>
</job>
</package>

Related

VBScript passing value by reference (VarPtr) to ActiveX function

I'm trying this:
Dim oApp
Dim iReturnedResult
Set oApp = CreateObject("Some.Application")
Set F_Ord = oApp.Documents.Open("Window 1", VarPtr(iReturnedResult))
The ActiveX control expects the second parameter to be a Long by reference.
This works perfectly well inside Excel VBA.
I can run this step by step, and see the result is returned like it should.
But, when I move this code to a VBS file and run it from the command line (CScript.exe), I get an error 800A000D, meaning it's the wrong type.
I have also tried creating an array instead, and tested with these commands, without any luck:
Set F_Ord = oApp.Documents.Open("Window 1", VarPtr(iReturnedResult(0)))
Set F_Ord = oApp.Documents.Open("Window 1", iReturnedResult(0))
Does anyone know how to pass a long variable by reference to an ActiveX control from VBScript?
The simple answer is VarPtr() is not supported by VBScript.
To my knowledge, there is no equivalent that allows you to pass a pointer to a variables memory address.
Useful Links
Visual Basic for Applications Features Not In VBScript

Scripting Word from vbs

I'm trying to get Word to fill in cells in a table. The script works when run as a macro from within Word, but fails when saved as a .vbs file and double-clicked, or run with wscript. This is a part of it.
set obj = GetObject(,"Word.Application)
With obj
With .Selection
MsgBox .text
If (.Information(wdWithInTable) = True) Then
.Collapse Direction:=wdCollapseStart
tCols = .Tables(1).Columns.Count
tRow = .Information(wdStartOfRangeRowNumber)
tCol = .Information(wdStartOfRangeColumnNumber)
For I = 2 To 5
.Tables(1).Cell(tRow, I).Range.Text = "fred" & Str(I)
Next
` now make new row
For I = 1 To tCols - tCol + 1
.MoveRight unit:=wdCell
Next
End If
End With
End With
I have three problems. First, it won't compile unless I comment out the .Collapse and .MoveRight lines. Second, although the MsgBox .text displays the selected text, I get "out of range" errors if I try to access any .Information property.
I'm sure I'm missing something very simple: I usually write software for Macs, and I'd do this using AppleScript. This is my first attempt at getting anything done under Windows.
VBScript and VBA are different languages.
They are a bit similar, but not very. Moreover, VBScript is not like AppleScript; it doesn't let you easily interface with running programs.
The interfaces you'll get from VBScript can behave subtly differently in VBA and VBScript. However, I think you've got two problems here:
:= is invalid syntax in VBScript; you'll need to find an alternative way of calling the function. Try just using positional arguments.
You've no guarantee that this will open the expected file; there could be another instance of Word that it's interacting with instead.
Since your code is not running within the Word environment it would require a reference to the Word object library in order to use enumeration constants (those things that start with wd).
VBScript, however, cannot work with references, which means the only possibility is to use the long value equivalents of the enumerations. You'll find these in the Word Language References. Simplest to use is probably the Object Browser in Word's VBA Editor. (In Word: Alt+F11 to open the VBA Editor; F2 to start the Object Browser; type in the term in the "Search" box, click on the term, then look in the bottom bar.)
The code in the question uses, for example:
wdWithInTable
wdCollapseStart
wdStartOfRangeRowNumber
wdStartOfRangeColumnNumber
wdCell
The reason you get various kinds of errors depends on where these are used.
Also, VBScript can't used named parameters such as Unit:=. Any parameters must be passed in comma-delimited format, if there's more than one, in the order specified by the method or property. If there are optional parameters you don't want to use these should be left "blank":
MethodName parameter, parameter, , , parameter

KeyMatch not working

I have inherited a GSA installation (with no handover notes or instructions, just the manuals). I am trying to enable some features on the test appliance. I have setup some KeyMatch "ExactMatch" items, but when I search for the trigger terms the KeyMatch results do not appear.
I changed the show_keymatch to "1" in the XSLT and saved it (it was set to "0"). Here is that section of the XSLT as it appears now:
<!-- *** keymatch suggestions *** -->
<xsl:variable name="show_keymatch">1</xsl:variable>
<xsl:variable name="keymatch_text">KeyMatch</xsl:variable>
<xsl:variable name="keymatch_text_color">#2255aa</xsl:variable>
<xsl:variable name="keymatch_bg_color">#e8e8ff</xsl:variable>
Is there another area in the XSLT file, or in the admin interface, that I need to look at in order to enable keymatch?
I feel that I have followed all the instructions in the docs, but the lack of results tell me that I've missed something!
Any advice greatly appreciated.
It is always possible that the previous developer modified the XSLT and took out the code to display it or your search URL may not be using those keymatches.
Two things:
1) I recommend to create a new Front End and put your keymatches in there and test with that Front End.
2) In your search URL are proxystylesheet and client the same values? If not, make sure they are the same and pointing to the Front End that contains the keymatch entries.

VBScript Syntax Highlighting in Web Page

Single Code Comment for Visual Basic Script (VBScript) seems to be not working , i am using
Google-code-prettify
https://code.google.com/p/google-code-prettify/
For more details please see this fiddle :
http://jsfiddle.net/dekajp/27skU/1/
<div>
<pre class="prettyprint lang-vb">
' Variable Declaration
Dim szToken
Dim oXMLHTTP
Dim szBaseUri, szUri
szToken ="Please see Logon code example for how to obtain a Token"
' set the base URI path
</pre>
</div>
Looks like the single quote comment is not working or am i missing something ?
I'm not familiar with the tool. I am familiar with VB and VB.net you're using but see if this syntax works better for you. AHH... also... you can't use ' unless you're using straight VB programming. You must exit your ' with an addition ' at the end. Looking at Fiddle... all your Declarations and szToken were all highlighted as comments.
<div>
<pre class="prettyprint lang-vb">
<!--Variable Declaration-->
Dim szToken as String
Dim oXMLHTTP
Dim szBaseUri, szUri
szToken.text = ("Please see Logon code example for how to obtain a Token")
<!--set the base URI path-->
</pre>
</div>
I have similar situation. I removed run-prettify.js script and added prettify.js + lang-vb.js, and it helped. It's still not perfect, so I'm figuring out how to modify it, but. Comments are working great.

VBS - Replace certain text entries in various files

I created a template for my test suite in QTP where the level of abstraction (parameterization) is sufficiently good.
I would now need to populate a new test suite from the existing pattern, thus replacing certain entries with other ones in various files.
For example one of the words I deliberately put in the script suite pattern is [Template], therefore I would need to copy and paste the template with a different name, change all the entries by [Template] to the new string and so forth.
Any code would be appreciated as my VBScript skills are not optimal ;)
Thanks in advance!
Use this demo script:
Option Explicit
Dim gMap : Set gMap = Createobject("Scripting.Dictionary")
Function replGMap(sM, nPos, sSrc)
replGMap = gMap(sM)
End Function
Dim reMap : Set reMap = New RegExp
reMap.Global = True
reMap.Pattern = "\[\w+\]"
gMap("[A]") = "abra"
gMap("[B]") = "cadabra"
WScript.Echo reMap.Replace("1[A]2[A]3[B]4[A]5", GetRef("replGMap"))
output:
abra2abra3cadabra4abra5
as a list of keywords to look up in the VBScript Docs. For using a function in .Replace, see here.
The FileSystemObject provides the means (Open/CreateTextFile, ReadAll, Write) to read and write files.

Resources