PDA

View Full Version : Print Macro Help!



lestng
02-23-2006, 04:15 PM
I have a Word 10 document (I will call it MAIN) over 100 pages and contains descriptions of kung fu styles and many hyperlinks to other Martial Art documents in Word. I am wondering if Word 10 VBA can genearte a form which list all the available hyperlinks and a check-box beside each of the item to select which hyperlinked document to print.

Sample Form:


Print Menu

A.1 Kung Fu book
A.2 Shao Ling Quan
Z.3 Karate 10


Note: Let the bullets be the check-box and underlined text be the hyperlinked items.

:thumb (Let this thumb up icon be the print button)

When I view the >100 pages MAIN document in outline view, I have a view that arrange the hyperlynks item nicely, which I can easily click on the hyperlink and jump to the corresponding document and go File Print. When I want to print 50 documents, instead of repeating 50 times the 1) Click Hyperlink 2) File 3) Print, I am wondering if Word VBA can generate a form like above, on the fly and allow people to check to select which document they want to print and click on the print button and have the selected docs printed.

I have done some VBA scripting before but this seems like a very ambitious goal to achieve. The only coneptual way I have in mind is to loop through the document and gether the hyperlink items and copy and paste the items in a FORM (is this even possible?) and I would appreciate if anyone know this has been done before in other forum so I can have a point to start.

Thanks.

XLGibbs
02-23-2006, 05:19 PM
Wow this is ambitious, but I am sure it is doable. This is quite a lot to ask though since you are looking for most of the work to be done for you. I don't have time to get into this, nor the needed word experience...although I am sure that others may help with pieces of it...

For clarification though, you have 1 document with 100 pages, or 100 documents?

Setting a reference to each page as a hyperlink might be possible by using the table of contents to populate the list of pages, but each page/document would have to have an identifiable header (I think) that would be used.

If you start by setting up a table of contents (not with code) and defining chapters...it may help get you started with something.

mdmackillop
02-23-2006, 05:51 PM
Hi,
Welcome to VBAX
Here's a rough bit of code to try. Create a userform with a command button. Add the following code to the userform
Regards
MD

Option Explicit
Dim MyLinks()
Private Sub UserForm_Initialize()
Dim i As Integer, hlink

ReDim MyLinks(500)
For Each hlink In Documents(1).Hyperlinks
i = i + 1
AddCheckBox i, "Forms.checkbox.1", 5, 25, 500, 25, hlink.TextToDisplay
MyLinks(i) = hlink.Address
Next
ReDim Preserve MyLinks(i)
End Sub
Sub AddCheckBox(intCount As Integer, strControl As String, intLeft As Integer, intTop As Integer, _
intWidth As Integer, intHeight As Integer, strCaption As String)
Dim mycmd As Control
Set mycmd = Controls.Add(strControl)
mycmd.Left = intLeft
mycmd.Top = intTop + 20 * intCount
mycmd.Width = intWidth
mycmd.Height = intHeight
If strCaption <> "" Then
mycmd.Caption = strCaption
End If
mycmd.Visible = True
End Sub
Private Sub CommandButton1_Click()
Dim ctl, j As Long
j = -1
For Each ctl In Me.Controls
j = j + 1
If ctl = True Then
ActiveDocument.FollowHyperlink MyLinks(j)
ActiveDocument.PrintOut
ActiveDocument.Close
End If
Next
End Sub

lestng
02-24-2006, 09:22 AM
XLGibbs, mdmackillop

Thanks for you quick responses.

XLGibbs, my model MAIN document has over a hundred pages which may contain numerous hyperlinks. I was thinking along the same line with my limited self learned knowledge of Word and VBA. Starting with the table of contents and then use VBA script to generate the list from the TOC. (Would be nice after I generated the TOC I can do a Save...as...frmTOC, haha!)

But mdmackillop has come up with code to generate the FORM on the fly, which with my limited knowledge of VBA could only dream of doing. Wow! Thanks for everything. Another eye opener for me in what VBA can achieve.

mdmackillop, I ported your sample script to my MAIN doc, I have reason to put the script in the ThisDocument object under the Microsoft Word Objects in the Project Explorer. That is I publish this MAIN doc to the Web so my users can open the doc and have the macro starting up in AutoOpen() event. All they need to do is to Check, Check and click the Print button and off they go to the printer.

In the code View, I pressed F8, and got a User Defined Type Not Defined message and it stopped at Dim mycmd As Control. I have done some quick research to no avail but will continue to try hard to resolve the issue. I need to get my hands dirty to learn this VBA skill afterall.

mdmackillop,
I assume you are doing this for hobby, I am wondering if, after I have tried all my best and failed, would you have time to take on project like this for a fee or point me to someone you know can take on this. Just to line up resource if all my attempts failed. But I am quite excited also to learn this new skill myself, yet I know I am not that up to par to take on this project.

Ok, I will go back to figure out that User Defined Type Not Defined msg.

mdmackillop
02-24-2006, 10:54 AM
I think you can only use this setup on a form. The option here would seem to be adding the checkboxes to a new/existing document.
My first thought would be to set up a template with an appropriate number of checkboxes and hidden bookmarks, the assign the display text to the caption names, the addresses to the bookmarks and run the code in a similar fashion.
I've not tried this sort of thing, so would hesitate to take it on "professionally", so anyone else with the know-how, please feel free.
Regards
MD

mdmackillop
02-26-2006, 06:34 AM
Try the following alternative. For the Main document


Sub PrintLinks()

Documents.Add Template:="C:\AAD\HLINKS", NewTemplate:=False, DocumentType:=0
Selection.EndKey Unit:=wdStory
For Each hlink In Documents("Main.doc").Hyperlinks
i = i + 1
With ActiveDocument.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormCheckBox)
.Name = "CB" & Format(i, "000")
.CheckBox.Value = False
End With
Selection.TypeText vbTab & hlink.TextToDisplay & TypeParagraph
Selection.TypeParagraph
'Set drive letter as required
Selection.TypeText "C:" & Right(hlink.Address, Len(hlink.Address) - 2)
Selection.Paragraphs(1).Range.Font.Hidden = True
With ActiveDocument.Bookmarks
.Add Range:=Selection.Paragraphs(1).Range, Name:="BK" & Format(i, "000")
.ShowHidden = False
End With
Selection.TypeParagraph
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields
End Sub

In a template document, containing a Command Button:

Private Sub CommandButton1_Click()
ActiveDocument.Unprotect
For i = 1 To ActiveDocument.FormFields.Count
If ActiveDocument.FormFields(i).Type = wdFieldFormCheckBox Then
If ActiveDocument.FormFields(i).CheckBox.Value = True Then
ActiveDocument.Bookmarks("bk" & Format(i, "000")).Select
Selection.Font.Hidden = False
ActiveDocument.FollowHyperlink Selection.Text
ActiveDocument.Printout
ActiveDocument.Close False
ActiveDocument.Bookmarks("bk" & Format(i, "000")).Select
Selection.Font.Hidden = True
End If
End If
Next
ActiveDocument.Protect wdAllowOnlyFormFields, Noreset:=True
End Sub

lestng
02-26-2006, 06:28 PM
MD
Both Solutions really gave me good opportunities to learn more VBA programming. All I had done in the past was to patch and fix VBA codes and learn at the same time. Your codes add vocabularies to my personal knowledgebase.

I have not decided which solution to proceed but I found out there are more hyperlinks in the sub-documents which the users are interested to print as well. I pictured something as follow before reading your latest solution.

Print Menu

+ A.1 Kung Fu book
+ A.2 Shao Ling Quan
- Z.3 Karate 10
Karate - Technic 1
Karate - Technic 2
Note: Let the bullets be the check-box and underlined text be the hyperlinked items.

When + is clicked the tree structure will expand, when - is clicked the tree will collapse. Theoretically, the checkboxes should on the right of the +/- symbol.

:thumb (Let this thumb up icon be the print button)

The flashes when opening the hyperlinked documents and loop through to bulid the tree like depicted above got my head spinning but I will work hard to achieve this task. I realize I also have to code the + and - object depicted, it may not be possible. I may have to look elsewhere, like .NET?

One may think why on earth would anyone would want to provide a print functions like above. For me, it will be a fun challenge. I really thanks for your support. If it proved to be too much for me to handle, I will post an entry in the Conculting section in VBAX Express.

I had stepped through your codes in the second solution and have already found something I had been trying to do without success in other part of my life. Thanks. Anyway I hope you don't mind me poking you from the back and beg for help.

lestng