Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-10-2022, 12:15 PM
mdr mdr is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2019
Novice
Transfer DocVariables from one template's userform to another template's userform
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default Transfer DocVariables from one template's userform to another template's userform

If a checkbox on Template1 is checked, then it will open another template by using Applications.documents.add ("c:\template2.dotm")



When template2 opens, I want the textboxes on the userform in template2 to populate with the information from the textboxes on the userform in template1. Basically, both templates use several of the same document variables and I want to pull the inputs from template1 to populate the userform in template2.

Is this possible and, if so, how do I go about doing it?

Thanks for any help!

-- Mike
Reply With Quote
  #2  
Old 02-10-2022, 05:11 PM
Guessed's Avatar
Guessed Guessed is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Are you expecting to have both userforms open at the same time? If so, you will need to make them modal.

If the userform reads document variables in order to populate the relevant userform on initialisation then you can simply read the document variables from one file and write them to the other file. For example
Code:
Private Sub VariableTransmission()
  Dim doc1 As Document, doc2 As Document, aVar As Word.Variable
  
  Set doc1 = ActiveDocument
  Set doc2 = Documents.Add
  
  doc1.Variables.Add "First", "Hi mum"
  doc1.Variables.Add "Second", "Hello world"
  
  For Each aVar In doc1.Variables
    doc2.Variables.Add Name:=aVar.Name, Value:=aVar.Value
  Next aVar

  Debug.Print doc2.Variables("Second").Value
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 02-10-2022, 08:08 PM
mdr mdr is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2019
Novice
Transfer DocVariables from one template's userform to another template's userform
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default

Thanks Andrew--I am off work until Monday, so I'll try then!
Reply With Quote
  #4  
Old 02-14-2022, 11:23 AM
mdr mdr is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2019
Novice
Transfer DocVariables from one template's userform to another template's userform
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default

Huh, that isn't working Andrew because it keeps saying the variable already exists in the second document
Reply With Quote
  #5  
Old 02-14-2022, 03:51 PM
Guessed's Avatar
Guessed Guessed is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This version doesn't error if the variable already exists and if the variable doesn't exist it creates it.
Code:
Private Sub VariableTransmission()
  Dim doc1 As Document, doc2 As Document, aVar As Word.Variable
  
  Set doc1 = ActiveDocument
  Set doc2 = Documents.Add
  
  doc1.Variables("First").Value = "Hi mum"    'resets value or creates if it doesn't already exist
  doc1.Variables("Second") = "Hello world"
  doc1.Variables("Second") = "Hello world"
  
  For Each aVar In doc1.Variables
    doc2.Variables(aVar.Name) = aVar.Value
  Next aVar

  Debug.Print doc2.Variables("Second").Value
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 02-14-2022, 04:00 PM
mdr mdr is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2019
Novice
Transfer DocVariables from one template's userform to another template's userform
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default

ok, i have gotten it so that the information i enter on the userform in document 1 will show up as documentvariables in document 2, but it will not show up on the userform for document 2. Any thoughts? This is the code I am using right now in document1

If chxIFPOrder = True Then
Set doc1 = ActiveDocument
Set doc2 = Application.Documents.Add("C:\XXXX.dotm")
doc2.Variables("myinitials").Value = Me.TxtYourInitials.Value
End If


When I do that, there is no myinitials information showing up on the userform itself in doc2, but when I hit OK on the userform in doc2, then the myinitials documentvariable populates with the myinitials information that I entered on the userform in doc1 shows up. I want the info to show up on the userform in doc2 also.

Any help would be greatly appreciated!

-- Mike
Reply With Quote
  #7  
Old 02-14-2022, 07:08 PM
Guessed's Avatar
Guessed Guessed is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I can't see the code you have executing but the question is WHEN does the userform associated with doc2 initialised? If it initialises and populates the userform prior to the next line of code running then I would expect the userform for doc2 to show the initial value rather than the updated line

Set doc2 = Application.Documents.Add("C:\XXXX.dotm")
'does the userform load here or does it wait for the next line before loading it?
doc2.Variables("myinitials").Value = Me.TxtYourInitials.Value
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 02-14-2022, 10:28 PM
gmayor's Avatar
gmayor gmayor is offline Transfer DocVariables from one template's userform to another template's userform Windows 10 Transfer DocVariables from one template's userform to another template's userform Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I am curious why you need two userforms for this when you appear to have both documents open. Wouldn't it make sense to provide the data to both documents from the same userform? In fact your code already appears to do that.

You could also read and write data via the registry using SaveSetting and GetSetting e.g.
Code:
Public Function appID() As String
     appID = "MyApp"
lbl_Exit:
    Exit Function
End Function
Code:
'Write
SaveSetting appID, "Config", "My Initials", txtInitials.Text
'Read
txtInitials.Text = GetSetting(appID, "Config", "My Initials", "Mr Dr")
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
UserForm with Combo Box from data in table within template jhansrod Word VBA 6 06-13-2019 09:02 AM
Excel Userform to Word Template Bookmarks JCrinage Excel Programming 1 11-02-2016 07:03 PM
Transfer DocVariables from one template's userform to another template's userform Agenda template - userform kennyD Word 4 04-07-2015 01:32 PM
userform to enter multiple lines of data in template callasabra Word VBA 0 06-27-2014 05:29 PM
How to get Outlook 2007 userform into template? Royzer Outlook 0 04-13-2012 10:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:23 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft