Cancel
Showing results for 
Search instead for 
Did you mean: 

Opening a Word document

Valued Contributor
Valued Contributor

 

hello,

 

 

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 9.0.3.4 and Win7

 

 

Tnx

 

 

 

9 REPLIES

Re: Opening a Word document

Siemens Phenom Siemens Phenom
Siemens Phenom

 

Maurizio,

 

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:

 

Imports Microsoft.Office.Interop.Word

 

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.

 

Regards,

 

Steve

 

Re: Opening a Word document

Valued Contributor
Valued Contributor

Steve,

 

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

 

 

Re: Opening a Word document

Honored Contributor
Honored Contributor

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.

Re: Opening a Word document

Siemens Phenom Siemens Phenom
Siemens Phenom

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.

山田
yamada

Re: Opening a Word document

Valued Contributor
Valued Contributor
very fine, thank you.

I didn't know that adding a reference to the Word library was impossible when running a journal, that was the main point of my question.

Re: Opening a Word document

PLM World Member Legend PLM World Member Legend
PLM World Member Legend
Why not use Application.followhyperlink (or something like that not sure if I recall correctly) it's opens the called file with the application normally used your system
Patrick Delisse
KMWE - DutchAero
NX11.0, CAMPOST V22, Vericut 8.1

Re: Opening a Word document

PLM World Member Legend PLM World Member Legend
PLM World Member Legend

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.

Patrick Delisse
KMWE - DutchAero
NX11.0, CAMPOST V22, Vericut 8.1

Re: Opening a Word document

Honored Contributor
Honored Contributor

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.

Re: Opening a Word document

PLM World Member Legend PLM World Member Legend
PLM World Member Legend
Thats correct. It just opens the program and you can edit the file in there, not through the journal
Patrick Delisse
KMWE - DutchAero
NX11.0, CAMPOST V22, Vericut 8.1