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:

using VBscript to format Word Document


Recommended Posts

I'm now experimenting to format WORD documents to the way I want them to look.

Here's what I want:

A single line of text centered at the top, that simply says "Program info" (a different color would be nice too). What is the object command to center text in word? I couldn't find anything specific in the microsoft site.

 

Then I want it to be followed by 2 short columns, which contains essential information, such as customer name, description, program name, item number, etc. Then a seperate two columns that shows the tool list, 10 tools per column. I have the printout of the TextColumn Object here, but I think VBScript wants me to declare InchesToPoints, but I get a error message if I try to declare it as a constant or array.

 

Then, under the tool list columns, a single line that tells the TL0 location. (or one very short pagewide column)

 

Lastly, the rest of the page blank for a graphic of the part. Or a single column, whichever works.

 

I think this list hinges on the TextColumn Object.

 

John

Link to comment
Share on other sites
  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

John,

 

One of the quickest ways to learn office vba is to record a new macro doing the things you mentioned, then open the VBA editor (right click toolbar check Visual Basic) and look in the Normal.dot bas module. Press F2 to view the object browser, select Word to only see word objects, you can also copy and paste any const and enum declares you may need for the script.

Link to comment
Share on other sites

quote:

A single line of text centered at the top, that simply says "Program info" (a different color would be nice too). What is the object command to center text in word?


Here's a small snippet to get you started.

 

code:

' vars

Dim objWord As Word.Application ' Word app object

Dim objDOC As Word.Document ' Word document object

 

' create an instance of Word

Set objWord = CreateObject("Word.Application")

appWord.Visible = True

Set objDOC = objWord.Documents.Add

 

' make John T's title

objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

objWord.Selection.Font.Size = 14

objWord.Selection.Font.Bold = True

objWord.Selection.Font.Name = "Arial"

objWord.Selection.Font.Color = wdColorRed

objWord.Selection.TypeText Text:="Program info"

objWord.Selection.TypeText Text:=Chr(11) ' <-- this is a new line

 

' more wicked awesome code to create the rest of the DOC

 

' clean up

Set objWord = Nothing

Set objDOC = Nothing

You may need to enumerate some of those values for VBS. The Office Developer Section on MSDN comes in handy.

 

Hope this gets you started.

Link to comment
Share on other sites

Thanks for that snippet bulllines.

 

I think I'm confused about enumerate. Doesn't that mean to make a code "shortcut", such as:

 

Set wordDoc=CreateObject("word.Application")

 

So instead of typing the whole thing out, I can just type:

 

With wordDoc

.Visible=True

 

etc, etc.

 

Is that correct?

 

I've been reading up at the MSDN library a good bit now.

 

Mick, I'll send some code shortly.

 

John

Link to comment
Share on other sites

Mick,

Heres the code in question. I've noted the 2 macros I've been trying, and are causing problems. The first one is supposed to set margins, the second one a vertical, rotated text box on the upper right. The margin code seems to be asking to have the InchesToPoints defined first, and the text box code keeps insisting there needs to be a right parentheses ")"

 

John

 

code:

Dim wordDoc

 

'///////////////// My Constants /////////////////

Dim companyname

Dim Program

Dim Description

Dim Post

Dim programmer

 

Const wdAlertsNone=0

Const wdWindowStateMaximize=1

Const wdLine=5

 

 

 

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

 

 

 

' -- Start Script

Call Main()

 

 

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

' Sub Declaration

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

Sub Main()

 

 

 

WriteString("Company Name?")

companyname=AskString("company Name?")

Program=AskString("program number?")

Description=AskString("description")

Post=AskString("fadal")

programmer=AskString("programmer?")

 

Set wordDoc=CreateObject("Word.Application")

 

wordDoc.Documents.Add

With wordDoc

.Visible=True

.WindowState=wdWindowStateMaximize

.DisplayAlerts=wdAlertsNone

 

.Selection.PageSetup.LeftMargin = InchesToPoints(0.5) 'start of margin setting macro

.Selection.PageSetup.RightMargin = InchesToPoints(0.5)

.Selection.PageSetup.TopMargin = InchesToPoints(0.5)

.ActiveWindow.ActivePane.VerticalPercentScrolled = 20

.Selection.PageSetup.BottomMargin = InchesToPoints(0.5) 'end of margin setting macro

 

.ActiveDocument.Words(1).Text="Company "&companyname&vbTab&"Program "&Program&vbTab&"Dir "&GetCurrentFileName()&vbNewLine & _

"Description "&Description&"Post "&post&" Stock Size "&GetJobSetupStockSizeX&" X "&GetJobSetupStockSizeY&" X "&GetJobSetupStockSizeZ

 

.ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 513#, _

108#, 45#, 117#).Select 'start of macro code

.Selection.ShapeRange.TextFrame.TextRange.Select ' should put the "Program" string in a vertical box

.Selection.Collapse

.Selection.ShapeRange.Select

.Selection.ShapeRange.IncrementLeft 36#

.Selection.ShapeRange.IncrementTop -18#

.Selection.Orientation = wdTextOrientationDownward

.Selection.Font.Size = 24

.Selection.TypeText Text:=Program 'end macro code

End With

 

End Sub

 


 

[ 03-11-2004, 11:58 PM: Message edited by: John T. ]

Link to comment
Share on other sites

John T.

 

Is this more what you're after?

 

code:

Dim wordDoc

 

'///////////////// My Constants /////////////////

Dim companyname

Dim Program

Dim Description

Dim Post

Dim programmer

 

Const wdAlertsNone=0

Const wdWindowStateMaximize=1

Const wdLine=5

 

' add these

Const msoTextOrientationHorizontal = 1

Const wdTextOrientationDownward = 3

 

 

 

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

 

 

 

' -- Start Script

Call Main()

 

 

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

' Sub Declaration

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

Sub Main()

 

 

 

WriteString("Company Name?")

companyname=AskString("company Name?")

Program=AskString("program number?")

Description=AskString("description")

Post=AskString("fadal")

programmer=AskString("programmer?")

 

Set wordDoc=CreateObject("Word.Application")

 

wordDoc.Documents.Add

With wordDoc

.Visible=True

.WindowState=wdWindowStateMaximize

.DisplayAlerts=wdAlertsNone

 

'start of margin setting macro

.Selection.PageSetup.LeftMargin = .InchesToPoints(0.5) ' resolve back to Word object

.Selection.PageSetup.RightMargin = .InchesToPoints(0.5) ' resolve back to Word object

.Selection.PageSetup.TopMargin = .InchesToPoints(0.5) ' resolve back to Word object

.ActiveWindow.ActivePane.VerticalPercentScrolled = 20 ' resolve back to Word object

.Selection.PageSetup.BottomMargin = .InchesToPoints(0.5) ' resolve back to Word object

'end of margin setting macro

 

.ActiveDocument.Words(1).Text="Company "&companyname&vbTab&"Program "&Program&vbTab&"Dir "&GetCurrentFileName()&vbNewLine & _

"Description "&Description&"Post "&post&" Stock Size "&GetJobSetupStockSizeX&" X "&GetJobSetupStockSizeY&" X "&GetJobSetupStockSizeZ

 

.ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 513, 108, 45, 117).Select 'start of macro code

.Selection.ShapeRange.TextFrame.TextRange.Select ' should put the "Program" string in a vertical box

.Selection.Collapse

.Selection.ShapeRange.Select

.Selection.ShapeRange.IncrementLeft 36

.Selection.ShapeRange.IncrementTop -18

.Selection.Orientation = wdTextOrientationDownward

.Selection.Font.Size = 24

.Selection.TypeText Program 'end macro code

End With

 

End Sub

Link to comment
Share on other sites

John,

 

I have put a comment (mag) where I made changes to get your script to run.

 

code:

Dim wordDoc

 

'///////////////// My Constants /////////////////

Dim companyname

Dim Program

Dim Description

Dim Post

Dim programmer

 

Const wdAlertsNone=0

Const wdWindowStateMaximize=1

Const wdLine=5

 

 

' -- Were missing mag

Const msoTextOrientationHorizontal = 1

Const wdTextOrientationDownward = 3

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

 

 

 

' -- Start Script

Call Main()

 

 

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

' Sub Declaration

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

Sub Main()

 

'WriteString("Company Name?")

companyname=AskString("company Name?")

Program=AskString("program number?")

Description=AskString("description")

Post=AskString("fadal") ' -- You can call GetPostName() mag

programmer=AskString("programmer?")

 

 

Set wordDoc=CreateObject("Word.Application")

 

wordDoc.Documents.Add

 

With wordDoc

 

.Visible=True

.WindowState=wdWindowStateMaximize

.DisplayAlerts=wdAlertsNone

 

' -- InchesToPoints is an App method mag

.Selection.PageSetup.LeftMargin = .InchesToPoints(0.5) 'start of margin setting macro

.Selection.PageSetup.RightMargin = .InchesToPoints(0.5)

.Selection.PageSetup.TopMargin = .InchesToPoints(0.5)

.ActiveWindow.ActivePane.VerticalPercentScrolled = 20

.Selection.PageSetup.BottomMargin = .InchesToPoints(0.5) 'end of margin setting macro

 

.ActiveDocument.Words(1).Text= "Company "& companyname & vbTab & "Program " & Program & vbTab & "Dir " & GetCurrentFileName() & vbNewLine & _

"Description " & Description & " Post " & post & " Stock Size " & GetJobSetupStockSizeX & " X " & GetJobSetupStockSizeY & " X " & GetJobSetupStockSizeZ

 

.ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 513, 108, 45, 117).Select 'start of macro code

.Selection.ShapeRange.TextFrame.TextRange.Select ' should put the "Program" string in a vertical box

.Selection.Collapse

.Selection.ShapeRange.Select

.Selection.ShapeRange.IncrementLeft 36 ' -- Removed # not valid in VBS mag

.Selection.ShapeRange.IncrementTop -18

.Selection.Orientation = wdTextOrientationDownward

.Selection.Font.Size = 24

 

' -- Removed Text:= can't use this type of assignment in VBS

.Selection.TypeText Program 'end macro code

 

End With

 

 

If Not wordDoc Is Nothing Then Set wordDoc = Nothing

 

ShowString "Script has ended"

 

 

 

 

End Sub

Link to comment
Share on other sites

Mick, How can I find information as to what those Const wdXxxxxx = X values should be? That seems to be a stumbling block for me.

I'm at home now (4 day workweek!), so will give your code a try Monday.

 

A bit eerie how Bullines and Mick came up with the same program

 

It really seems to be the little things like the # sign and text:= that aren't allowed in VBS that are tripping me up because I don't know that.

 

You guys have been a lot of help!

Link to comment
Share on other sites
Guest CNC Apps Guy 1

Hey Mick,

 

So where do it find the

quote:

...Tea by which all other teas are judged ...

here in the States? I watched all the commercials. I'd try it just on those alone! Kewl!

Link to comment
Share on other sites

Mick, Browsing through Word's object browser screen, with the "Word" selected in the upper left dropdown. Using the example you wrote, I looked up "selection (classes column on left)-oriention (members of "selection" on right), and on the bottom, it tells me "Property Oriention as WdTextOriention".

 

Now, I can get the "wdTextOrientionDownward" from the recorded macro, but how do you know to define the constant wdTextOriention as 3 in the constant definitions?

This is what puzzles me in this code. how do I find out what these number mean, or which numbers are required in defining these constants?

 

In playing around with centering a pargraph, I discovered that the constant for orienting a paragraph (forgot what it was right now, have it noted) to =1 seems to "turn it on". Obviously, not all definitions are 0/1 or on/off.

 

John

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...