is there a chance to open and modify a Word document while within a VB journal ?
I have managed to open and save a document, but functionalities such as
Find and Replace seem not to be working, the Word namespace seems just not
to be there.
What am I missing ?
Running NX 18.104.22.168 and Win7
Solved! Go to Solution.
Good afternoon. If you are building your program into a .DLL or a .EXE, you can use the appropriate Imports statement and Add a Reference to the correct Office .DLL.
If you are running as a Journal, without compiling, you may be limited to a much smaller number of libraries. I have seen a list for those, but I cannot lay my hands on it at the moment.
I just added this to a Journal:
I did not have any code actually processing a Word doc, but I did not get any complaints about the Imports statement. So try that and see if it helps.
I did add that, but it didn't have any effect.
Also installed PIA to no avail, the following journal still results in "Word not declared" message.
Option Strict Off Imports System Imports NXOpen Imports NXOpen.IO Imports Microsoft.Office.Interop.Word Module NXJournal Sub Main () Dim theSession As Session = Session.GetSession() Dim oWord = CreateObject("Word.Application") oWord.Visible = True oWord.Documents.Open ("k:\file.doc") 'Find and replace some text. oWord.Content.Find.Execute(FindText:="foo", ReplaceWith:="bar", Replace:=Word.WdReplace.wdReplaceAll ) End Sub End Module
If you have an author license and are compiling the code, you can add a reference to the Word library; if you are running the code as a journal, you cannot.
You can still work with Word, but you will have to use late binding. You might not have access to some of the constants defined in the Word library, but these can easily be recreated in your journal code.
When I ran the journal it complained about not finding the constant "wdReplaceAll". To find the value of this constant: open the VBA editor in Word, then open the object browser; and search for "wdReplaceAll". Near the bottom of the object browser window it will say something like:
Const wdReplaceAll = 2 Member of Word.WdReplace
This tells us that "wdReplaceAll" is an integer constant with a value of "2". In your journal code, you can use the value of "2" in place of the "wdReplaceAll" keyword. However, using the value directly tends to make the code look confusing when you revisit it later. A better option would be to recreate the constant in your journal code to help make the code self-documenting.
Dim theSession As Session = Session.GetSession() Dim oWord = CreateObject("Word.Application") oWord.Visible = True oWord.Documents.Open("k:\file.doc") Const wdReplaceAll As Integer = 2 'Find and replace some text. oWord.ActiveDocument.Content.Find.Execute(FindText:="foo", ReplaceWith:="bar", Replace:=wdReplaceAll)
Note that I also replaced "oWord.Document" with "oWord.ActiveDocument" because .Document was not a member of the Word application object.
This question is about MS Word, really, and is completely unrelated to NX (except that programs run in the Journal Editor can't have references to the Word PIA assembly). So, you'll probably get better help in a place like StackExchange or MS help sites.
Looked it up. Should be:
Dim UISes As UI = UI.GetUI() Dim FileName as String = "File.docx" Try System.Diagnostics.Process.Start(FileName) Catch ex As Exception UISes.NXMessageBox.Show("ERROR", NXMessageBox.DialogType.Error, "ERROR! Can't open " & FileName) End Try
This method uses whatever program is default used for the specified filetype on your computer.
Kinda like double clicking it in explorer.
No need to worry about witch program, where it's located and how to fire it up.
This will open and display the document,but not give you the ability to edit it (programmatically). It appears the OP wanted to open and edit the document through the journal.