PDA

View Full Version : Creating two documents from a selection



cavil
05-24-2011, 08:55 AM
Hi

I need to update a vba macro someone else created and I'm not that great at word vba.

Basically I want everything that the codes does to say the same, but I need it to create two documents instead of one.

Right now the code will create one document based upon user selection and then also pull from multiple documents to create the end product.

This is what it currently says

If strDoc = "ADCHistorical.doc" Then
Selection.InsertFile FileName:=strInsertDocPath & "ADCHistoricalPricing.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
Else
Selection.InsertFile FileName:=strInsertDocPath & "StatutesHistoricalpricing.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
End If

This is what i think it ought say


If strDoc = "ADCHistorical.doc" Then
Selection.InsertFile FileName:=strInsertDocPath & "ADCHistoricalPricing.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False

Selection.InsertFile FileName:=strInsertDocPath & "ADCHistoricalPricing.WLN.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
Else
Selection.InsertFile FileName:=strInsertDocPath & "StatutesHistoricalpricing.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False

Selection.InsertFile FileName:=strInsertDocPath & "StatutesHistoricalpricing.WLN.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
End If


Help, please?

Thanks

cavil
05-24-2011, 12:25 PM
OK I realize that's entirely wrong and why, but what I don't know is how to insert two documents at once using the same selections already made.

Basically there is a dialog box where a user enters in stuff, and that stuff is then populated accordingly in the resulting document. I need it populated in two resulting documents simultaneously. In the meantime I made it so it opens one, forces you to save it, then changes the content of the one thing I need changed, then forces you to save it again with a different name and explains to the user why its doing that.

Frosty
05-24-2011, 03:08 PM
Nothing in a computer happens simultaneously. It happens in a sequence. There is no "SaveAsTwoDocumentsSimultaneously" command.

So it sounds like you've already come up with a solution: save one document, then save another document. Why don't you describe why that isn't what you want. You want it to perform the action without user input?

If so, then you need to identify what the names of the two different documents will be.

It's harder to "fix" someone else's code when you don't understand it. Can you outline the steps starting with the moment *before* any documents are opened and ending with the moment *after* all new documents are saved and closed.

If you do that, you may be able to record a macro which does what you want (filling in the gaps which need to be filled). And from there, it should be simple enough to add a variable or two so you don't actually have to see the Save As dialog twice... it will just do it behind the scenes.

But it still won't happen simultaneously ;)

cavil
05-24-2011, 03:44 PM
I suppose bad choice of words.

I wanted it to open up two different documents so the user would then see that two different documents were being made. As in this particular use case they need to save the document and then attach it to an email then send it to someone else (I don't know why, it just is that way) there is not value in opening them both up "at the same time" (to which I really meant that the user would see two separate word documents) other than it might be easier on a user to understand what is being created.

But my other solution seemed to work alright. It forces the user to save, tells the user its going to change something, makes the change, then forces the user to save again, thus creating the two documents that need to be emailed off to someone in the span of 10 seconds. I thought this might be adequate.

But then you mentioned something about not having to see the save dialog twice. So there is a way to have a user open something, save it, have the program change something, then automatically save it again with a different (but similar) filename?

Frosty
05-24-2011, 04:41 PM
Absolutely... for example, the following code will take the open document, save as "This Is A New Document.docx" to a temp folder on the root of your C:\ drive, and then add some text, and then save it as "As is this one.docx", also to your C:\Temp (You will need to create a folder named Temp at the root of your C:\, or modify the code to point somewhere else).

No user interaction necessary, except for the first click of the button.

However, you're talking about creating emails and attaching documents, etc... so there could be a lot more involved, and this simple sample code may not prove useful at all.

Depending on what version of Word you're using, you may need to change .SaveAs2 to .SaveAs... but the concept is the same.

Public Sub SaveTwoDocumentsButNotSimultaneously()

ActiveDocument.SaveAs2 "C:\Temp\This Is A New Document"
ActiveDocument.Paragraphs(1).Range.InsertAfter "This is some new text" & vbCr
ActiveDocument.SaveAs2 "C:\Temp\As is this one"
End Sub


And if you wanted to close the document at the end, you could use
ActiveDocument.Close

This is the most rudimentary stuff... and it's probably better practice to simply record macros which get close to what you want done. You'll find it's not as hard to read the stuff as it might seem.

cavil
05-24-2011, 10:21 PM
Thank you so much.

I actually did it a bit differently, but you got me thinking about how to do what I wanted to do so you were quite helpful. The existing code walked the user through the save so they saved in the correct directory. Then, after that was done, a pop up told them where it was saved. I modified said pop up to say it was going to make a new document, then added in this after the code for the changes to the document:

strFileNameWithoutExtension = Left$(strFileName, InStrRev(strFileName, ".") - 1)
ActiveDocument.SaveAs strFileNameWithoutExtension & "WLN"


Basically the document is slightly different so I wanted a specific extra "WLN" added at the end of the document name (whatever it was that the user put in). It works beautifully. Thank you!