How can i add endnotes to a word doc using Powershell? - powershell

Hello I'm looking for a way to search for a word in a word doc and add an endnote(special type of footnote) with a definition of the word as the endnote text. This would allow me to hover over that word and then the definition would pop up like a tool tip.
I know i need to use reflection, but i'm new to the whole reflection thing and all my attempts have fallen flat.
I've found the reference for endnotes here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.endnotes.add%28office.11%29.aspx
I've tried loading C:\WINDOWS\Assembly\Gac\Microsoft.Office.Interop.Word\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll using reflection, but i don't know what to do once i've loaded it. When i try to create an new-object, it still asks me if i've loaded the appropriate dll.
Additionally i tried to fix the problem with a diff method by loading the MS word application as a comobject, but i wasn't able to figure out how to select the text i wanted and then set and endnote.
Any suggestions for this would be greatly appreciated!
-Skyler

I am not too familiar with the Word object model, but if you can handle that part I can tell you how to get an instance of Word running and automated. It's quite simple actually.
$Application = New-Object -ComObject Word.Application
$Application.Visible = $true
$Document = $Application.Documents.Add()
The key is Visible = $true otherwise it will be running but hidden. Now you can use all the methods of the Word Application object to create a new doc and automate it. Now if you're using Word 2007's docx format, you can investigate ZIP file extraction cmdlets and access the xml directly in the word doc. But dealing with namespaces in XML is a hassle and may not be as straightforward.
Word Object Model Stuff

ScriptingGuy recently posted a solution to this: http://blogs.technet.com/heyscriptingguy/archive/2009/10/14/hey-scripting-guy-october-14-2009.aspx

Related

Upgrading from iTextSharp to iText7

I'm in the process of updating our scripts to ensure they remain functional, and discovered iText7 has replaced iTextSharp. My needs are simple; read form fields. Rather, I know how to read a form field, I'm just checking to see if there's a more streamlined way to do it, as it seems like it was easier in iTextSharp.
Here's the old code we're using with iTextSharp (the $form is being fed to the $reader via a foreach loop):
#create pdf reader object and load form
$reader = New-Object iTextSharp.text.pdf.PdfReader -ArgumentList $form.PSPath.Replace("Microsoft.PowerShell.Core\FileSystem::","")
#Get the data I need
$First = $reader.AcroFields.GetField("FirstName")
Simple. When playing with iText7 though, it seems to lose its simplicity. Here's what I have for iText7:
#Create pdf reader and load form
$Reader = [iText.Kernel.Pdf.PdfReader]::new("C:\temp\TestForm.pdf")
#Create PDFDoc object?
$PdfDoc = [iText.Kernel.Pdf.PdfDocument]::new($Reader)
#What? Why?
$Form = [iText.Forms.PdfAcroForm]::getAcroForm($PdfDoc, $True)
#Get the data I need. Oh wait, I am unable to read it.
$fName = $Form.GetField("FirstName")
#Finally...
$First = $fName.GetValue()
I'm afraid I don't have any luck researching simple code; everyone seems to be creating web forms on the fly, or parsing thousands of PDFs for data analytics. I'm also just a lowly SysAdmin, not a dev. Please tell me there's an easier way to read a single form field in iText7. Thanks in advance!
The simplicity is not necessarily measured by the number of lines of code. Your way of reading form fields in iText 7 is correct. The reason you need a couple of more lines is that iText 7 has a much clearer separation of different parts of the code across modules. This has big advantages compared to iText 5 and gives a greater room for flexibility in user code.
Inability to call $Form.GetField("FirstName").GetValue() is a PowerShell limitation by the way and has nothing to do with iText - you are able to use that kind of chaining in C# or Java.

Simulating a Mouse Click in Powershell

I already know Powershell is not really meant for GUI automation, but just the same I have a pretty large Powershell script that needs to perform a single GUI operation, that being a left mouse click. I have searched for hours and have not found a satisfactory solution. The closest I have found is an add on called Wasp, but it was written in 2009 and appears to be incomplete anyway.
Is there another add in available to solve this problem?
I cannot condone using this as a first resort, but it should work. Generally when dealing with web pages you want to use URL parameters as much as possible since webpage elements are volatile.
With that said you should be able to create an IE comobject and then select the element that way. For example:
First create an IE object:
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.navigate("YourPageURLHere.com")
$ie.visible = $true
You can manipulate certain elements by ID (ex: name="username") or even type. These can be easily found be viewing the source code of a webpage. Here are a few examples:
$Username = $ie.document.getElementByID("Username")
$Username.value = $UsernameValue
$SearchButton = $ie.Document.getElementsByTagName("button")
$SearchButton.click()
Even if the webpage changes, since we have it all attached to the IE object you can then grab the output elements that generate after the submit button is clicked and feed them back into your script.

Create a Hyperlink in Word using Matlab activeX

Currently I am trying to write some Text to Word using ActiveX and Matlab. This file: http://www.mathworks.com/matlabcentral/fileexchange/9112-writetowordfrommatlab
helped me a lot. But I can't figure out how to insert an Hyperlink to Word.
e.g. I want to add the the word "test" connected with "www.test.de".
I've tried this:
ActXWord = actxserver('Word.Application');
ActXWord.Visible = true;
trace(ActXWord.Visible);
word_handle = invoke(ActXWord.Documents,'Add');
ActXWord.ActiveDocument.Hyperlinks.Add('test','www.test.de');
and also some other combinations with the
ActXWord.ActiveDocument.Hyperlinks.Add
method. But Matlab doesn't know the .Add method. I found some Excel examples which are working like this, but for Word it doesn't work. Somebody has an idea whats the problem could be?
Your issue is not really related to MATLAB. You are not calling the Add method of the ActiveX component correctly.
This should work:
link = 'www.test.de';
ActXWord.ActiveDocument.Content.InsertAfter(link);
ActXWord.ActiveDocument.Hyperlinks.Add(word_handle.Range(0, length(link)), link);
Anyway, this is not related to MATLAB; for more information you should refer to MS Word VBA reference.

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();

Add a "Hyperlink" item type to a list using PowerShell in Sharepoint

I've been a SharePoint admin for a while, and now have been tasked with a bit more of a developer role - which I'm still very much learning. Most things I've been able to figure out on my own or through Google, but this one has me stumped.
For one particular task I need to use PowerShell to script adding items to a list. Normally - not a difficult task. These steps are all over the web. However, I have yet to find anywhere that will tell you how to add a "Hyperlink" type of item to a list.
I can add one using the following code:
$NewItem = $MyList.Items.Add()
$NewItem["My Hyperlink Column"] = $($url.url)
$NewItem.Update()
But I want to set the name/title of the link as well and that's what stumps me. I don't want to have to create a separate column in the list and populate that with the link name, and use code similar to above to populate the url/link.
Does this work for you? I don't have a Sharepoint install available to test on, this is from memory:
$NewItem = $MyList.Items.Add()
$NewItem["My Hyperlink Column"] = "$($url.url), <Title>"
$NewItem.Update()
james
Thanks James! That was very close and I'm thinking would work if I was specifying a single item?
Here's my full solution (with some extra bits):
$enumsite = new-object microsoft.sharepoint.spsite($SubWebUrl)
foreach ($url in $enumsite.allwebs)
{
$NewItem = $MyList.Items.Add()
$NewItem["My Hyperlink Column"] = "$($url.url), $(url.title)"
$NewItem.Update()
}
$enumsite.Dispose()
Perhaps this will help someone else out in the future.