How to use VBScript to lock a specific region of a Word document? - ms-word

So I want to make a specific part of a Word document read-only using VBScript.
The version of Word I am using is from Office 16.
How to do this manually (recorded a macro for this to see if I would get a better idea on how to do this) I found here.
I know how to lock the entire document, and know that the following works:
'Protect Document
objDoc.Protect wdAllowOnlyReading, True, "password"
I saw this question being asked (on an old MS forum), but nothing more than this.
Any help with this sort of issue on an example I would appreciate.

While you can't do this using the Protect() method directly the answer appears to be in the Question you linked from the MS Forum.
' Protect Document
objDoc.Protect wdAllowOnlyReading, True, "password"
' Select last paragraph of the document
objDoc.Paragraphs.Last.Range.Select
' Make an exception for current selection
objWord.Selection.Editors.Add(-1) ' -1 = everyone
Using the Selection object you can modify the Editors on a protected document selection to "everyone" which unprotects that selection only leaving the rest of the document still protected.

Related

How can I find and edit/delete links from a Word document before JS requirement set 1.4?

I'm developing a MS Word Add-In using the JS API. Currently, I need to find and edit or delete specific links inside the entire document. I know that this works using context.document.body.fields and then item.result.delete() and item.result.insertText('new text', 'Replace'). But context.document.body.fields is only available in the latest versions of Word (technically since requirement set 1.4).
I just want to know, are there alternative ways to do this in older Word versions before rs 1.4? And if so, what are those?
I think you can use document.body.search() method to find the hyperlink and this method will return a Range object, you can futher call something like InsertText() to replace it or call range.hyperlink = "" to remove the hyperlink.

Word addin, set filename

If one starts a blank file on Word you can usually see in the top bar a name such as "Document1", "Document2" and so on.
Yet, if you attempt to open a file using the Word JS API like this:
Word.run((context) => {
context.application.createDocument(documentB64).open()
return context.sync()
})
The top bar comes out like this:
No filename is set.
To set a filename/handle(?), I tried using the code given here Office JS - Add customProperty to new document but that didn't help.
My addin is usually used in conjunction with another (VSTO) add-on and that add-on can't work properly with the documents opened by my addin and I believe the lack of a filename (/handle?) explains it to some extent.
Is there something I can do about this?
Thank you
Currently you can't do this because the newly created document is just a temporary file and not saved. can you try to call the following code to make sure the newly created file is saved?
const documentCreated = context.application.createDocument(externalDoc);
documentCreated.save();
documentCreated.open();

Blinking effect missing

About one year ago I developed an app that, among other things, had to select parts of word documents and let them blink... just one line like this
Selection.Range.Font.Animation = MSWord.WdAnimation.wdAnimationBlinkingBackground;
The app worked pretty: if you open those docm documents (they have some macro inside) you can see some part blinking...
Now my customer tried it on a brand new laptop using Office 2013 and, surprise, when you open same word documents blinking effect is missing.
Is there a known reason for this?
Is there a way I can solve it?
According to MSDN, the WdAnimation enumeration has been deprecated:
This object, member, or enumeration is deprecated and is not intended
to be used in your code.
I can't find any other reference as to why it was dropped, or suggested replacement.
As a workaround, I just changed the background color. Non-blinking.

Setting / accessing the Application.CutCopyMode property via UI

We are facing an error of "This method or property is not available because the clipboard is empty or not valid" in one of our Lotus Notes applications. The code basically opens a document, selects all the text, copies it and then pastes it in MS Word document. It does this continuously for a bunch of documents. I found this link which basically suggests adding Application.CutCopyMode = False to the code to resolve the issue.
I need to know can this property be accessed and set via UI for MS Word?
In the example you reference, that Keyword is actually part of the Excel object model, not the Word object model. So you are unlikely to find it in Word's UI. Perhaps you are doing something with Excel as well, or perhaps it is a keyword in the relevant Lotus object model as well?
[
The OP mentioned a link, http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-06/msg00446.html
that suggested changing variables names, but that that did not work.
The linked article also suggests "sleeping for a few milliseconds between copying and pasting"
]

how to stop macros running when opening a Word document using OLE Interop?

As the title suggests, I have a .Net application which uses interop to open documents in Word. I have set
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
before opening the document. According to the documentation, thhis "Disables all macros in all files opened programmatically, without showing any security alerts"
However, when I attempt to open one specific document I get a dialog box on the screen that says "could not load an object because it is not available on this machine". It's a customer document but I believe it contains a macro with references to a COM object which I don't have installed.
Am I doing something stupid? is there any way to actually disable macros when opening a Word document?
Try:
WordBasic.DisableAutoMacros 1
Bizarrely, this relies on a throwback to pre-VBA days, but still seems to be the most-reliable way to ensure that no auto macros are triggered (in any document - you may want to turn it back using the parameter "0").
I recently had a project where I had to process 6,000 Word templates (yes, templates, not documents) many of which had oddball stuff like macros, etc. I was able to process all but 6 using this technique. (I never did figure out what the problem was with those 6).
EDIT: for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx
For c# you can use
(_wordApp.WordBasic as dynamic).DisableAutoMacros();
The whole code I'm using is:
using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
_wordApp = new Word.Application
{
Visible = false,
ScreenUpdating = false,
DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
FileValidation = MsoFileValidationMode.msoFileValidationSkip
};
_wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
(_wordApp.WordBasic as dynamic).DisableAutoMacros();