Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

MC hates VBS for-next


John T.
 Share

Recommended Posts

Heres a combination of code I've taken from the forum. The problem is, the tool list doesn't work. It will print the header info, but not the tool list. Any ideas why? Also, do you get formatting characters in the header in Word? I do, can't seem to turn it off.

 

John

 

code:

'///////////////// My Global Variables //////////

Dim wordDoc

 

 

 

' -- Start Script

Call Main()

 

 

' ////////////////////

' Sub Declaration

' ////////////////////

Sub Main()

' -- Add code here...

 

Dim nCount ' number of operations

Dim nToolNum ' tool number

Dim nOpNum ' operation number

Dim nIdx ' array index

Dim nX ' for loop counter

 

Dim arrToolNumber() ' array of tool numbers

Dim arrToolComment() ' array of tool comments

Dim arrToolDiameter() ' array of tool diameters

Dim arrToolFluteLength() ' array of tool flute lengths

Dim arrToolLength() ' array of tool lengths

 

On Error Resume Next

 

'open Word

Set wordDoc=CreateObject("Word.Application")

If Err Then

ShowString"could not get an instance of Word"

ShowString"Error #" & CSstr(Err.Number)& " "&Err.Description

Else

wordDoc.Documents.Add

With wordDoc

.Visible=True

.WindowState=wdWindowStateMaximize

.DisplayAlerts=wdAlertsNone

.ActiveDocument.Words(1).Font.Size=14

.ActiveDocument.Words(1).Bold=True

.ActiveDocument.Words(1).Text="Plastic Ingenuity Operations Sheet"&vbNewLine&"Tool Number"&vbTab&"Tool Desc"&vbTab&"Diameter"&vbTab&"Flute Length"&vbTab&"Overall Length"

End With

End If

 

' get the number of operations

nCount = GetOperationCount("")

 

' show us the number of operations

ShowString "Number of operations: " & nCount

 

' resize the arrays

ReDim Preserve arrToolNumber(nCount)

ReDim Preserve arrToolComment(nCount)

ReDim Preserve arrToolDiameter(nCount)

ReDim Preserve arrToolFluteLength(nCount)

ReDim Preserve arrToolLength(nCount)

 

' init the index

nIdx = 0

 

' get the id of the first operation

nOpNum = GetFirstOperationID("")

 

' loop as long as there are operations to get

Do

' get the tool number using the current operation

nToolNum = GetToolNumberFromOperationID("", nOpNum)

 

' copy tool data into our individual arrays

arrToolNumber(nIdx) = nToolNum

arrToolComment(nIdx) = GetToolComment(nToolNum)

arrToolDiameter(nIdx) = GetToolDiameter(nToolNum)

arrToolFluteLength(nIdx) = GetToolFluteLength(nToolNum)

arrToolLength(nIdx) = GetToolLength(nToolNum)

 

' increment the index

nIdx = nIdx + 1

 

' get the next operations

nOpNum = GetNextOperationID()

Loop While(nOpNum <> -1)

 

For nx=0 To (nCount-1)

WriteToWord(arrToolNumber(nX)&vbTab&arrToolComment(nX)&vbTab&arrToolDiamter(nX)&vbTab&arrToolFluteLength(nX)&vbTab&arrToolLength(nX)&vbLf)

Next

 

If askYesNo("close Word?")=mcMSG_YES Then Word.Quit

'End If

 

If IsObject(Word) Then

Set Word=Nothing

End If

 

' Next

End Sub

 


Link to comment
Share on other sites

John,

 

I made some edits to get this to run under Word 2003 for me and did a little re-arranging, it *might* work under Word 2000+ let me know.

 

I noticed in your script that you did not define the function WriteToWord() also there is a typo in that call for the variable arrToolDiamter(nX)should read arrToolDiameter(nX) and no constant values were declared for the Word enums.

 

code:

'///////////////// My Global Variables //////////

Dim wordDoc

 

Const wdAlertsNone = 0

Const wdWindowStateMaximize = 1

Const wdLine = 5

 

' -- Start Script

Call Main()

 

 

' ////////////////////

' Sub Declaration

' ////////////////////

Sub Main()

 

On Error Resume Next

 

Dim nCount ' number of operations

Dim nToolNum ' tool number

Dim arrToolNumber() ' array of tool numbers

Dim arrToolComment() ' array of tool comments

Dim arrToolDiameter() ' array of tool diameters

Dim arrToolFluteLength() ' array of tool flute lengths

Dim arrToolLength() ' array of tool lengths

Dim idx

 

 

' get the number of operations

nCount = GetOperationCount("")

 

If nCount = 0 Then ShowString "No operations found in current drawing": Exit Sub

 

 

For idx = 1 To nCount

' get the tool number using the current operation

nToolNum = GetToolNumberFromOperationID("", idx)

 

ReDim Preserve arrToolNumber(idx)

ReDim Preserve arrToolComment(idx)

ReDim Preserve arrToolDiameter(idx)

ReDim Preserve arrToolFluteLength(idx)

ReDim Preserve arrToolLength(idx)

 

' copy tool data into our individual arrays

arrToolNumber(idx) = nToolNum

arrToolComment(idx) = GetToolComment(nToolNum)

arrToolDiameter(idx) = GetToolDiameter(nToolNum)

arrToolFluteLength(idx) = GetToolFluteLength(nToolNum)

arrToolLength(idx) = GetToolLength(nToolNum)

 

Next

 

'open Word

Set wordDoc = CreateObject("Word.Application")

 

If Err Or wordDoc Is Nothing Then

ShowString"could not get an instance of Word"

ShowString"Error #" & CSstr(Err.Number)& " "&Err.Description

' -- Bail

Exit Sub

Else

wordDoc.Documents.Add

 

With wordDoc

.Visible=True

.WindowState=wdWindowStateMaximize

.DisplayAlerts=wdAlertsNone

.ActiveDocument.Words(1).Font.Size=14

.ActiveDocument.Words(1).Bold=True

.ActiveDocument.Words(1).Text="Plastic Ingenuity Operations Sheet" & vbNewLine & _

"Tool Number" & vbTab & _

"Tool Desc" & vbTab & _

"Diameter" & vbTab & _

"Flute Length" & vbTab & _

"Overall Length"

End With

 

End If

 

 

For idx = 1 To nCount

 

Dim strText

 

strText = arrToolNumber(idx)& vbTab & _

arrToolComment(idx) & vbTab & _

arrToolDiameter(idx) & vbTab & _

arrToolFluteLength(idx) & vbTab & _

arrToolLength(idx)

 

Call WriteToWord(strText)

 

Next

 

 

If IsObject(wordDoc) Then

If askYesNo("close Word?")= mcMSG_YES Then wordDoc.Quit

Set wordDoc=Nothing

End If

 

 

If Err Then

ShowString "Error raised " & Err.Description

End If

 

End Sub

 

 

' ////////////////////' Sub Declaration' ////////////////////

Public Sub WriteToWord(strOutText)

Dim wordCounts

 

With wordDoc

.Selection.MoveDown wdLine, .ActiveDocument.Words.Count

.Selection.TypeParagraph

.Selection.TypeText strOutText

End With

 

End Sub

Link to comment
Share on other sites

Thanks for the script, Mick. It does run under word 2000.

 

The Word code was copied from another user here who used a sample code from you, way back when the VBscript upgrade was coming out.

 

What are Word enums?

 

Now to study the script, and see what I can learn, and what my next steps will be.

 

Wish I knew more about object model programming. I'm just copying that too.

 

John

Link to comment
Share on other sites

quote:

What are Word enums?


They're the enumerated constants (but not enumerated variables) that you're using for the Word object. It just makes things less cryptic. For example, when you use this code with Mick's enumeration constants:

 

code:

Const wdWindowStateMaximize = 1

' lots...

' of...

' code...

With wordDoc

.Visible=True

.WindowState=wdWindowStateMaximize ' start Word maximized

it's a lot more readable than:

 

code:

With wordDoc

.Visible=True

.WindowState=1 ' start Word maximized...apparently
;)

HTH

Link to comment
Share on other sites

Mick,

 

I have question about the Op_Id

 

code:

  

' get the number of operationsnCount = GetOperationCount("")

If nCount = 0 Then ShowString "No operations found in current drawing": Exit sub

For idx = 1 To nCount ' get the tool number using the current operation

nToolNum = GetToolNumberFromOperationID("", idx)

ReDim Preserve arrToolNumber(idx)

ReDim Preserve arrToolComment(idx)

ReDim Preserve arrToolDiameter(idx)

ReDim Preserve arrToolFluteLength(idx)

ReDim Preserve arrToolLength(idx)

' copy tool data into our individual arrays arrToolNumber(idx) = nToolNum

arrToolComment(idx) = GetToolComment(nToolNum)

arrToolDiameter(idx) = GetToolDiameter(nToolNum)

arrToolFluteLength(idx) = GetToolFluteLength(nToolNum)

arrToolLength(idx) = GetToolLength(nToolNum)

Next

Is the op_Id(idx above) always in an incremental mode? If I delete a toolpath #2, will the Op_Id still return in 1,2,3,4,5 or 1,3,4,5?? headscratch.gif

 

Please advise. Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...