Need help on Word vba

VBABEGINER

Well-known Member
Joined
Jun 15, 2011
Messages
1,284
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I need help on vba but not this time on excel based. It is related to word vba coding. How do I get the solution from board experts, kindly guide me..

Just posting what I'm looking for..

I've word file and I've similar copy of word template. I want to develop a code where suppose.. my client name is ABC and hence the document name also ABC.docx.

I want copy entire data and paste it in template file and save as that template copy with client name. But while copying, every field should get copy exactly with that field against template file. Is this clear, what I'm trying to say?

Could anyone please guide me / help me..

sincerely regards..
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Done properly, you don't need any code to make a " similar copy of word template". What you should do is to create a proper Word template (i.e. a .dot, .dotx, or .dotm) file. Then, to create a new document based on that template, all you need do is double-click on it.

Saving a document as a new or existing template is as simple as choosing the appropriate template save type from the Save As dialogue.
 
Upvote 0
Respected Macropod Sir,

Very first, Thank You very much for responding to my query.
While dropping query for word vba, I'm little bit nervous thinking whether I will get any response to my query or not. This scenario is not in case of excel vba query. Whatever today I learn excel vba till today's date that is totally b'coz Mrexcel and I always Thankful for that. Sometimes, some mistakes happen from us, but I don't have any intention behind that to break the rule..

coming on question topic.. Sir solution above you guided that I know. I know this manual method. But my query here is, let me explain..

+ client name - ABC
+ word file name - ABC.doc or docx

currently we've list of such documents readily available and saved. Suppose, client ABC expire their contract on 30 Apr 2022 and we got email confirmation that ABC client will get renew. Then my back office team will start creating renewal document for ABC client.

format is as below (this may vary client to client) -

Logo

Client Name - ABC

Renewal date - 1 May 2022 - 30 Apr 2023

Type of contract - xyz

Another field name1 - aaaaa

Another field name2 - aaaaa

.
.
.
..
..
..
some pages carries table structure too..
.
.
it may go upto page number 25 or 30 or sometimes only 15 page of documents..
..
end of the page.
*********************

What we need to do in this case, that create vba code where create renewal or we can say new client document.
step 1 - source file which is kept on specific folder's, they are called as "Template" files, pick the client specific template file and save one copy of that.
step 2 - then data should copy from current client documents and paste exactly against new file word document.

This is something I'm looking for.. and hence requested if anyone can provide vba code for this?

Or how do I identify field name or position of that name from source file and find the same name to another file and paste over there..

this is very challenging and never done before.

Please give your valuable suggestion here.. ?‍♂️
 
Upvote 0
Can anyone help here in rectifying code.. I want to start direct from index number. Copy the text and paste it on another file given index number. Pls help
VBA Code:
Dim clientname_org, clientname_temp As String
Dim TexttoFind As Range
Dim index As Integer

'index = Selection.Start
'MsgBox index

Windows("Sample").Activate
ActiveDocument.Indexes(122).Select
Selection.MoveRight Unit:=wdCell
Selection.Copy
Windows("Sample - Copy").Activate
ActiveDocument.Range(122).Select
Selection.PasteAndFormat (wdFormatOriginalFormatting)
 
Upvote 0
Can anyone help here in rectifying code.. I want to start direct from index number. Copy the text and paste it on another file given index number. Pls help
VBA Code:
Dim clientname_org, clientname_temp As String
Dim TexttoFind As Range
Dim index As Integer

'index = Selection.Start
'MsgBox index

Windows("Sample").Activate
ActiveDocument.Indexes(122).Select
Selection.MoveRight Unit:=wdCell
Selection.Copy
Windows("Sample - Copy").Activate
ActiveDocument.Range(122).Select
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Hi All, Can anyone please guide me here correction on code?
 
Upvote 0
I have already told you the correct way to use a document template in Word. Merely calling a document a template does not make it one.

Without knowing precisely where the client name appears and how it's input, it's impossible to write any code that would ensure a document created from the template (which is where the code needs to reside) gets saved with the correct name. If, for example, you use a Content Control titled 'Company' to hold the name, the code in the template (in this case a .dotm file) might be as simple as:
VBA Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
If CCtrl.Title = "Company" Then
  With ActiveDocument
    If InStr(.Path, "\") = 0 Then
      If .SelectContentControlsByTitle("Company")(1).ShowingPlaceholderText = False Then
        .SaveAs2 FileName:=.SelectContentControlsByTitle("Company")(1).Range.Text & ".docx", _
          FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      End If
    End If
  End With
End If
End Sub
The above code, which you would add to the template's 'ThisDocument' code module, would save the document with the correct name as soon as the Content Control titled 'Company' is filled in and exited. If the user makes any other edits to the document, they'll be prompted again to save on exit, but they won't have to choose the filename.
 
Upvote 0
Better code:
VBA Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
  If .Title = "Company" Then
    If .ShowingPlaceholderText = False Then
      With ActiveDocument
        If InStr(.Path, "\") = 0 Then
          .SaveAs2 FileName:=CCtrl.Range.Text & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        End If
      End With
    End If
  End If
End With
End Sub
 
Upvote 0
Better code:
VBA Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
  If .Title = "Company" Then
    If .ShowingPlaceholderText = False Then
      With ActiveDocument
        If InStr(.Path, "\") = 0 Then
          .SaveAs2 FileName:=CCtrl.Range.Text & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        End If
      End With
    End If
  End If
End With
End Sub
Dear Sir,

Thank You so much for your reply and kind attention..!! I really appreciate your reply ?‍♂️

Till now I've gone through 2 times with your code and explanation and it is not going in my brain ?

Let me try and understand it clearly and will respond you asap.. Thank You again..

sincerely regards,
vbabeginer
 
Upvote 0
Better code:
VBA Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
  If .Title = "Company" Then
    If .ShowingPlaceholderText = False Then
      With ActiveDocument
        If InStr(.Path, "\") = 0 Then
          .SaveAs2 FileName:=CCtrl.Range.Text & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        End If
      End With
    End If
  End If
End With
End Sub
Dear Sir,

I tried to understand this code and I dont think so that this will fit my requirement.

Is it possible for you to guide me on my post no. #4 in this thread. If I able to get the position index for what I'm looking for then my problem might get resolved..

Pls suggest, if possible..
 
Upvote 0
Is it possible for you to guide me on my post no. #4 in this thread. If I able to get the position index for what I'm looking for then my problem might get resolved..
As I said in post #6:
Without knowing precisely where the client name appears and how it's input, it's impossible to write any code that would ensure a document created from the template (which is where the code needs to reside) gets saved with the correct name..
So far you haven't provided any detail from which a more workable answer could be constructed. The code I provided will work well, though, if properly implemented in a document using a content control titled 'Company'.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top