PDA

View Full Version : Copying bookmarks over to a new word document, then inserting hyperlinks.



KKinney
06-16-2015, 10:23 PM
Hello,

I am creating a Macro in Word that will run off a button in a current word file. Once the button is clicked, a new document will open and create a new document in word and then create a letter based upon bookmark fields from the original word file, (it is a questionnaire). Essentially, I want all of the bookmarks that I created in the first document to be copied over into the new document, and hyperlinks to be created within the new document (that opens off of the macro as well), that will fill in pertinent information into the new letter. I think that I have highlighted my weak points in the code. Thanks for the help.



Private Sub CreateForm_Click()

' Copying over information from Questionnaire...


Dim oLink As Hyperlink
Dim docCurrent As Document
Dim docNew As Document
Dim rngStory As StoryRanges
Set docCurrent = ActiveDocument
Set docNew = Documents.Add
For Each oLink In docCurrent.Hyperlinks
oLink.Range.Copy
docNew.Activate
Selection.Paste
Selection.TypeParagraph
Next

Set docNew = Nothing
Set docCurrent = Nothing


' New Document Opens

' Typing the Letter


' Title


Selection.TypeText Text:="Welcome to Our Company!"
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
'
' Declaration
'
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText Text:=vbTab & "We are glad you made the decision to do business with" & _
"our company. We look forward to serving you and all of your future needs!"

Selection.Font.Bold = False
Selection.TypeParagraph
Selection.TypeParagraph
'
Selection.Font.AllCaps = True
Selection.TypeText Text:="Your New Username: "
Selection.Font.AllCaps = False

Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _
wdContentText, ReferenceItem:="Username", InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

gmayor
06-17-2015, 01:45 AM
This seems a somewhat tortuous process you are creating, and without seeing the questionnaire it is difficult to understand how it would work. The usual approach would be to create a letter template with the fixed portions of text already in place and driven by a userform which serves as the questionnaire. Then complete the document based on the selections and/or texts from the userform. The basics are covered at http://www.gmayor.com/Userform.htm

As for the hyperlinks I cannot see what your intention is there?

If you want to pursue the path you have begun then at the very least you should show us the questionnaire so we can see what you are doing. 'Go Advanced' to attach it.

KKinney
06-17-2015, 09:27 PM
Hello, I have attached the example file that I am trying to accomplish. There is detailed descriptions within the file to hopefully explain my issue further. Sorry that I cannot post the exact file that I am working on. There is sensitive client information. Thanks again for the help.13721

gmaxey
06-18-2015, 03:44 AM
I agree with Graham, tortuous and brutally inefficient. You can write and interactive user form that will populate using data in the active document.
See: http://gregmaxey.mvps.org/word_tip_pages/interactive_userforms.html

If you plan to stay on the tortuous path perhaps this will help:

http://gregmaxey.mvps.org/word_tip_pages/doc_bundler.html

gmayor
06-18-2015, 04:19 AM
I have modified the document (now a template) (http://www.gmayor.com/Forum/Test Questionaire.dotm) Create a new document from the template. It uses a building block in the template to create the welcome document and is populated from the form. The template creates two new documents in the path indicated (which it will create) "C:\Path\LastName_FirstName_Middlename\". It should give you some idea where to go with it next.

KKinney
06-23-2015, 12:42 AM
Gentlemen,

I have to go with the hard way. I appreciate the code given by Mr. Mayor, and I have obtained some ideas about how to frame my code, but the team may only have to update one piece of information and generate an entirely different document. In the interest of expediency, they must be able to do this without filling out all of the form fields again. And I need to be able to create different buttons for different documents needed to be created from the same questionnaire. I had a thought of creating a page break with a new section where I add in all of the text that I want to throw onto the next document, and then have the entire selection of text cut and paste onto a new sheet. The only problem is that I cannot get the formatting to transfer over like I want to. I want the fields to paste only the text that is within the cross reference to the bookmark. My code now Creates a new section, types in the document and adds in the cross references to the code, copies the section, then opens a new document, and pastes as text only, but I lose the formatting. Therefore, it still requires a lot of work to finish the document, which defeats the purpose.

In full disclosure, I work for a non-profit law firm that helps clients who cannot afford regular attorneys. I am trying to streamline the processes so that we can end up helping more clients using the same amounts of grants. Again, I am sorry that I cannot post the document that I am working on as it is confidential. But I appreciate any help that you can give to the novice VBA guy like me.

Thanks Gentlemen...

gmayor
06-23-2015, 01:50 AM
The modified template was but an example of how to fill in the documents. As the Questionnaire document is saved with the client name, you can open that document and read back the information from it into the userform. I have updated the template in the previous link to do that. You can also use the form to create a variety of documents, provided each is saved as a building block in the template. I have included a few lines of code to show how that would work, though I have not added the extra building blocks.

KKinney
06-30-2015, 03:00 PM
Mr. Mayor, you have convinced me to come over to the dark side (or light side I guess). Sorry for the late response, it has taken me a week to comprehend what you have done and sort of how you did it. I like the new add ins where the form can be quickly updated without typing everything in again. I am starting to transcribe this over into the Firm Documents. Is there any way that I can make it all run on a button instead of running automatically when it opens? I was going to try and copy it all over to a button object, but will it error out?

Secondly, are there any word reference books that you would recommend? I purchased a VBA book written by Paul McFedries that helped a lot, but are there any others that you would recommend?

Thanks tremendously for your help with this. It will allow us to save time and end up helping more people rather than turning them away.

gmayor
06-30-2015, 09:48 PM
The example template has a main macro 'CreateDocs' you can associate that with a button on the ribbon - http://gregmaxey.mvps.org/word_tip_pages/customize_ribbon_main.html
The two automacros simply call this macro when a new document is created from the template or when the document is opened.

I regret I cannot help with the book. I have never read a VBA book so am not in a position to suggest one. I think you will learn far more by looking at code examples and the forums are great places to start with that. I find it best to decide what it is you want to do then to research ways of doing it. But do learn all about ranges as they are very important in VBA for Word. Greg Maxey has a useful VBA primer on his web site that is worth looking at - and you will find lots of VBA examples and pre-prepared functions on my web site to help you along.

KKinney
07-01-2015, 10:41 PM
I have printed off many things from your site Graham. They have all helped me tremendously. Thank you for posting your knowledge. I am the same way, I have looked at code and learn pretty fast. This book has broken down the simple parts for me so that I can start to piece more complex code that you provided.

If acceptable, I may have some questions on parts of your specific code. I may ask you more specific questions on this thread later on as I build the program for the Non-Profit.

Thanks again for your help thus far...

KKinney
07-01-2015, 11:45 PM
Case in point. Many of the documents have more than one First name listed or last name. Is there a way to get the filling in of the bookmarks into a loop so that it updates all of the fields that I have in a building block? Is there some way to take out some of the code so that the "New Client Information Form" is not declared as "Document 1" Right off the bat?

gmayor
07-02-2015, 12:57 AM
I am not sure what you mean, or the relevance of the building block. If you mean can you combine the values of several fields to one text string, then yes of course. Assuming three fields TextBox1, TextBox2 and TextBox 3 then e.g.


strText = TextBox1.Text
If Not TextBox2.Text = "" then
strText = strText & " " & TextBox2.Text
End If
If Not TextBox3.Text = "" then
strText = strText & " " & TextBox3.Text
End If

strText is the combined result of the fields - write it where you wish.

If you create a new document from a template it will always be called Document(x) until you save the document. So save it as soon after its creation as you have the name information available.