I have used AHK and COM to do various things inside Microsoft word. For example, I have been able to write a script that finds and replaces or highlights words. However, I have not been able to find documentation on how to use COM to create or delete a comment in office.
I know that https://msdn.microsoft.com/en-us/library/ms178796.aspx is the documentation on how to add comments in C# and VBA, but how would I do this using COM.
When I answered this question I was unable to test the code at the time. I've edited it to better reflect what you asked and demonstrate a working example.
Create a COM Object Word Application
oWord := ComObjCreate("Word.Application")
Create a new Document.
oWord.Documents.Add
Add Text to our document.
oWord.Selection.TypeText("AutoHotkey is a pretty cool scripting language.")
Now lets add our comment
oWord.ActiveDocument.Comments.Add(oWord.ActiveDocument.Paragraphs[1].Range
, "This is a very true statement!.")
Make our document Visible
oWord.Visible := 1
Set a Hotkey to Delete the Comment. (Windows Key + F1)
#F1::oWord.ActiveDocument.Comments(1).Delete
Related
I want to add a footnote in word with a Word-JS Add-On. Unfortunately, I found out that the office.js API is not supporting this feature.
I found a way to do it with VBA here, but I found no option to run VBA code from office.js
The last thing I found is some VB and C# code here, but I have no Idea how to run it.
Private Sub DocumentFootnotes()
Dim text As Object = "Sample footnote text."
Me.Paragraphs(1).Range.InsertParagraphAfter()
Me.Paragraphs(1).Range.Text = "This is sample paragraph text."
Me.Footnotes.Location = Word.WdFootnoteLocation.wdBeneathText
Me.Footnotes.NumberStyle = Word.WdNoteNumberStyle.wdNoteNumberStyleLowercaseRoman
Me.Footnotes.Add(Me.Paragraphs(1).Range.Words(2).Characters(2), , text)
End Sub
VBA API is not supported to run in Office.js. The .NET API you've found are used in VSTO add-in. Here is a sample to create VSTO add-in.
The source situation: I have an Notes Application which uses MS Office 2000 under Windows XP. The new situation has to be MS Office 2010 under Windows 7. IBM Notes is 8.5.3FP3.
The old one uses a VBA template to communicate with Notes which works properly. At one time a Notes.NotesUiWorkSpace object is created to open a document, navigate to a richtext item, select all the content (formatted) and copy to the clipboard. Then the clipboard content ist pasted into the Word document via VBA. That works fine.
The same code in the second environment doesn't work anymore. I noticed that the Notes.NotesUIWorkSpace object cannot be instanciated in VBA. No errors, no hints. Only the runtime error when I reference the workspace-object later.
Here is a code excerpt:
' this is a profile document which is filled correctly
Call prof.Save(True, True)
Call prof.replaceItemValue("Form", "Profile")
' setting up the ui
dim WS as Object
set WS = CreateObject("Notes.NotesUiWorkSpace")
Set uiprof = WS.EditDocument(True, prof)
' Set uiprof = WS.currentDocument
If uiprof.editMode Then Call uiprof.gotofield("RT")
Call uiprof.SelectAll
Call uiprof.Copy
Call uiprof.Close
' later on the clipboard will be pasted into the word document
Any ideas what could be the cause here? I am setting up an environment with XP, MS Office 2010 and Notes tonight to check it out that it is not caused by Windows 7.
If the Windows 7 machine is 64 bit, take a look at the answers here. Note that those refer to the COM classes (lotus.), and you are using the OLE classes (notes.), but I believe the 64/32 bit issue applies to both.
I am using a program called mathtype to pull some equation objects out of a word document. I've written code in VBA that works perfectly using their API, but I have to translate it to a VBScript file. I have looked all over google, but have not found any solution on how (If it is even possible) to call a VBA library from VBScript.
VBScript can't see the MathTypeSDK Objects/Functions.
If not possible, how would I encase the macro I need to run in a globally available word file and call it from the VBScript?
Edit: Got it! Unfortunately the approaches below, while helpful, did not work for my situation. I found something closer: Embedding the macro in a global file and calling it through the Word Objects Run command.
objWord.Run "Normal.NewMacros.RunMain"
Here is an approach which might work for you. I tested this simple example.
Class "clsTest" in file "Tester.docm":
Public Sub Hello()
MsgBox "Hello"
End Sub
Class "Instancing" is marked "PublicNotCreatable".
Module in "Tester.docm":
Public Function GetClass() As clsTest
Set GetClass = New clsTest
End Function
In your vbscript:
Dim fPath, fName
fPath = "C:\Documents and Settings\twilliams\Desktop\"
fName = "Tester.docm"
Dim wdApp, o
Set wdApp = CreateObject("word.application")
wdApp.visible=true
wdapp.documents.open fPath & fName
Set o = wdApp.Run("GetClass")
o.Hello
Set o=nothing
Again - I only tested this simple example: you'll have to adapt it to your situation and try it out.
Word-VBA was not made to create reusable libraries, I suppose (for usage in external programs).
One way to reuse existing Word-VBA code is, however, run Word via WScript.Shell.Run using the /m<macroname> command line switch (see http://support.microsoft.com/kb/210565/en-us for details). This, has the restriction that evertime you need to call a specific macro, a Word process is started again, running that macro, and ends afterwards. Means, if you need just one call to your Word.VBA for a specfific task, this may be ok, but if you need a lot of interprocess communication between your VBScript and your VBA macro, you should look for a different solution.
I'm using Microsoft Office 2003 and creating a bunch of template documents to standardize some tasks. I asked this on Superuser.com and got no response so I'm thinking it's too program-y and hoping I'll have better luck here.
I need to automate a work flow that uses a bunch of Office (mostly Word) templates. What I want is to have "My Template Foo.dot" and "My Template Bar.dot", etc. in the "My Foo Bar Stuff" on a shared drive and have users double click on a template to create a new Foo or Bar.
What's I'd really like is for the user to double-click on the Foo template and be prompted for a couple of items related to their task (e.g., a project number) and have a script in the template change the name that Save will default to something like "Foo for Project 1234.doc".
I asked on Google Groups and got an answer that worked....for a while. Then my AutoNew macro stopped kicking in when I created a new document by double-clicking on the template. I have no idea why or how to debug it.
In Class Modules/This Application, I have:
Sub AutoNew()
Dim Project As String
Project = InputBox("Enter the Project Number")
ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
End Sub
In Microsoft Word Objects/ThisDocument, I have:
Private Sub Document_New()
End Sub
I really have no idea why or where that came from.
In Tools/Macro Security... I have Security Level set to "Low".
I'm a software engineering with 25+ years of experience but a complete Office automation noob. Specific solutions and pointers to "this is how to automate Word" FAQs are welcome. Thanks.
Update: If I create a new template (New..., Blank Document, Save As "My New Template.dot"), and insert the AutoNew() macro, it works. So what's inhibiting it from working on my existing template?
Update 2: Removing the module and function from my old template and adding it back works, too.
You can attach a template to a saved document in ordre to access the macros contained in the template in question.
You can do this with the AttachedTemplate property of a Document object (i.e. ActiveDocument).
Please note that I did not try this myself.
Sub AutoNew()
Dim Project As String
Project = InputBox("Enter the Project Number")
ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
ActiveDocument.AttachedTemplate = "\\path\to\templates\My Template Foo.dot"
End Sub
See MSDN - Word 2003 VBA Language Reference - AttachedTemplate Property
Hope that helps.
Check if the macro is still contained in your template. This sounds stupid but it happended to me, too, in Word 2003 under the following circumstances:
Create a template containing macro,
everything fine
Create a new document file based on
the macro, macro kicks in
Notice I could improve the template
here and there a bit, I do it in the
file created in 2) and SaveAs .DOT
Macro in .DOT GONE!
Why? Because the code stored in the .DOT doesn't go over to the doc file.
As per my application I want to write some Lines code in "ThisWorkbook" of Excel file using vb.net,before that we need to check the file for existance of code.
Please let me know any code or links for reference..
thank you...
It's possible but it's also very likely that the user's macro security settings will prevent this from working initially.
To adjust the security settings (all examples for Excel 2003):
(from a workbook): Tools > Macro > Security > Trusted Publishers
You now need to check the box which says "Trust access to Visual Basic project"
To read the code:
(from the VBA editor): Tools > References and add "Microsoft Visual Basic For Applications Extensibility 5.3" (the actual file is VBE6EXT.OLB)
To work out which VBProject is which, use the FileName property:
For Each vbpItem In Application.VBE.VBProjects
If (vbpItem.FileName = "C:\foo.xls") Then
Set vbpProject = vbpItem
End If
Next vbpItem
Once you have the project, you can refer to the module by name:
vbpProject.VBComponents("ThisWorkbook")
and you can check how many lines there are like this:
If (vbpProject.VBComponents("ThisWorkbook").CodeModule.CountOfLines <> 147) Then
With the CodeModule object, you can read back specific lines (via the Lines property) and change lines (with the ReplaceLine method)
The only thing I can find that would do this is ThisWorkbook.VBProject.VBComponents.Count which counts the number of Modules in your VB solution, which is Sheets + ThisWorkbook + anything additional. I can't find anything that would let you do a diff of the code.
Are you trying to do a security check of some sort? If random code was being inserted into your workbooks, wouldn't a black hat delete this coded in check? Why not just use signatures and digitally sign it?