Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
dannutzza
Femeie
25 ani
Mehedinti
cauta Barbat
25 - 52 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [WORD] Change Header and Text in Document with UserForm [VBA] Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TRaP
Moderator

Inregistrat: acum 6 ani
Postari: 748
When using a file for multiple companies for example, or by multiple people, the need might arise to add a personalized name in the header and in multiple places of the document.

For example with a contract, you might want to insert the name of one company or another in multiple places inside contract form.

For this, I found the solution of making a simple UserForm stating: Please select the desired company then press OK.
The UserForm has 4 OptionButtons, one for each company, and an OK CommandButton to run the VBA code and fill the text inside the document.

To define the places in the document where to add the text, I used named bookmarks, created via Insert Bookmark in the Insert ribbon. Their names are then used in the CommandButton's VBA code.

I also added a UserForm.Show command in the code of ThisDocument, to show the company selection box on contract form open, like this:

Private Sub Document_Open()
    SelectCompany.Show
End Sub


Note that SelectCompany is my UserForm. You can define the look of your UserForm however you like, as I said mine has a Label saying what you must do, 4 OptionButtons each with a company name and 1 CommandButton too run the code corresponding to the selected OptionButton.

I will post below the code of the OK button (the OptionButtons don't need code). As I also said above, I added 2 bookmarks (called Hader and Company) with Insert Bookmark in the Insert ribbon.


Private Sub OKButton_Click()

Dim HeaderRng As Range
Dim CompanyRng As Range

Set HeaderRng = ActiveDocument.Bookmarks("Header").Range
Set CompanyRng = ActiveDocument.Bookmarks("Company").Range

If CMP1 Then
    HeaderRng.Text = "COMPANY 1"
    CompanyRng.Text = "Company 1"
    ActiveDocument.Bookmarks.Add Name:="Header", Range:=HeaderRng
    ActiveDocument.Bookmarks.Add Name:="Company", Range:=CompanyRng
    Unload SelectCompany
Exit Sub
Else

    If CMP2 Then
        HeaderRng.Text = "COMPANY 2"
        CompanyRng.Text = "Company 2"
        ActiveDocument.Bookmarks.Add Name:="Header", Range:=HeaderRng
        ActiveDocument.Bookmarks.Add Name:="Company", Range:=CompanyRng
        Unload SelectCompany
    Exit Sub
    Else

        If CMP3 Then
            HeaderRng.Text = "COMPANY 3"
            CompanyRng.Text = "Company 3"
            ActiveDocument.Bookmarks.Add Name:="Header", Range:=HeaderRng
            ActiveDocument.Bookmarks.Add Name:="Company", Range:=CompanyRng
            Unload SelectCompany
        Exit Sub
        Else

            If CMP4 Then
                HeaderRng.Text = "COMPANY 4"
                CompanyRng.Text = "Company 4"
                ActiveDocument.Bookmarks.Add Name:="Header", Range:=HeaderRng
                ActiveDocument.Bookmarks.Add Name:="Company", Range:=CompanyRng
                Unload SelectCompany
            Exit Sub
           
            End If
        End If
    End If
End If
End Sub


Modificat de TRaP (acum 5 ani)


pus acum 5 ani
   
TRaP
Moderator

Inregistrat: acum 6 ani
Postari: 748
If the company name is not known beforehand and want to type in the company name for each contract manually, then you can use an InputBox and you don't need a UserForm in this case.

The code above is inserted in a module and the name of the Sub must be changed. Here's how to use an InputBox to manually insert the company name:

TBA


The text in ThisDocument also must change, because you won't call a UserForm anymore.

TBA


pus acum 5 ani
   
Pagini: 1  

Mergi la