Populate Local Branch Address in Footer - vba

I'd like a Word document template that, when opened, requests office location name then enters the proper address and phone number for that location in the footer.
I thought to use a drop down list to select the location, I.E. Denver, LA, SF, NY etc. then have the code enter the address and phone number in the footer.
I can get the code to put an address or phone number in the footer but not stacked. The last one overwrites the first.
Sub FooterAddress()
FooterAddress Macro
With ActiveDocument.Sections(1)
.Footers(wdHeaderFooterPrimary).Range.Text = "Local Office City"
.Footers(wdHeaderFooterPrimary).Range.Text = "123 My Street | City, ST 12345-6789"
.Footers(wdHeaderFooterPrimary).Range.Text = "Phone 800.123.4567"
End With
End Sub
I tried to use formatting codes for headers and footers to choose the font and center the text but just got errors. Didn't try changing the color yet.
I can't get the dropdown to launch the script and I can't get the template to launch the dropdown on open either.

For example:
Sub FooterAddress()
Application.ScreenUpdating = False
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
"Local Office City" & vbCr & _
"123 My Street | City, ST 12345-6789" & vbCr & _
"Phone 800.123.4567"
Application.ScreenUpdating = True
End Sub

In the footer, create bookmarks for each line (Insert>Bookmark). Then write to the bookmarks with:
ActiveDocument.Sections(1).Footer(wdHeaderFooterPrimary).Range.Bookmarks("Bookmarkname").Range.Text = "AddressText"

You can set the footer up in the template and format it as you require so that you don't need to apply formatting in your code.
Place your cursor Where you want the office address to be and go to Insert | Quick Parts | Document Property | Company Address. Without changing the selection go to the Developer tab and click on Properties to display the Content Control Properties dialog. Click the box marked "Allow carriage returns", as shown below, and click OK.
Now move your cursor to where the phone number should be and from the same menu as before add Company Phone. You don't need to do anything else with this content control.
You can use the code below to set the values of the content controls.
Public Sub WriteOfficeAddress(ByVal officeAddress As String, ByVal officePhone As String)
Dim cxpTarget As CustomXMLPart
Dim strNamespace As String
strNamespace = "http://schemas.microsoft.com/office/2006/coverPageProps"
strNodeName = "/ns0:CoverPageProperties[1]/ns0:"
Set cxpTarget = ActiveDocument.CustomXMLParts.SelectByNamespace(strNamespace).Item(1)
cxpTarget.SelectSingleNode(strNodeName & "CompanyAddress").Text = officeAddress
cxpTarget.SelectSingleNode(strNodeName & "CompanyPhone").Text = officePhone
End Sub
For your code to respond to a new document being created from your template you need to add code into the ThisDocument module. Use the drop downs at the top of the code window to create a Document New event, as shown below.

Related

VBA to add hyperlink to bookmark in MS-Word template

I need to add a hyperlink to a bookmark in an MS-Word template. The hyperlink (which changes depending on user input) points to an entry in a database on the web. I have no trouble building the hyperlink as a string variable, but I can't figure out how to put it in the bookmark so the user winds up with a Word document containing a link that can be clicked to go to the database entry. My code (below) just deletes the bookmark. What am I missing?
Dim databaseURL As String
' get databaseURL from an existing variable--this part works OK
databaseURL = ActiveDocument.Variables("databaseLink")
' put the hyperlink in a bookmark named "linkToDatabase"
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks("linkToDatabase").Range
BMRange.Text = "Database link"
ActiveDocument.Hyperlinks.Add Anchor:=BMRange, _
Address:=databaseURL, _
SubAddress:="", ScreenTip:="", TextToDisplay:=BMRange.Text
What you could do to simplify things is to have a bookmarked 'default' hyperlink field at the bookmarked range, then simply change the hyperlink field's code. For example:
ActiveDocument.Bookmarks("linkToDatabase").Range.Fields(1).Code.Text = "HYPERLINK " & databaseURL

Is there a way to programatically add Form Controls?

I'm working on a spreadsheet with a Form Control that opens a Userform used for data entry purposes. The submit button of the form fills a row of cells and adds two buttons on the last two cells of the row. It will insert as many rows as the user does and each time the two new buttons are created with their own distinct name. However, these are ActiveX controls and they are giving me compatibility problems with other Windows/Office versions once colleagues open the file and try to use it on their laptop.
This is the code I'm using to add one of the command buttons on the spreadsheet (it is essentially the same for the other button, just different variables):
Dim i As Long, Hght As Long
Dim Name As String, NName As String
i = 0
Hght = 305.25
NName = "cmdAction" & i
For Each OLEObject In ActiveSheet.OLEObjects
If Left(OLEObject.Name, 9) = "cmdAction" Then
Name = Right(OLEObject.Name, Len(OLEObject.Name) - 9)
If Name >= i Then
i = Name + 1
End If
NName = "cmdAction" & i
Hght = Hght + 27
End If
Next
Dim UpdateEntry As OLEObject, N%
Set UpdateEntry = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=Selection.Offset(0, 23).Left, Top:=Selection.Offset(0, 23).Top, Width:=72, Height _
:=24)
UpdateEntry.Name = NName
UpdateEntry.Object.Caption = "Edit Entry"
With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
N = .CountOfLines
.InsertLines N + 1, "Private Sub " & NName & "_Click()"
.InsertLines N + 2, vbTab & "Code for button goes here"
.InsertLines N + 3, vbTab & "End Sub"
End With
I was wondering if it was possible to do the same thing, but that the buttons created are form control instead of ActiveX?
The error that is displayed is Run-time error 32809: Application-defined or object-defined error. After extensive research, I've found that it happens due to the sheet getting corrupted once a different version of Windows has altered it. The only way to fix it is to create a new sheet, copying all of the contents to the new sheet, deleting the corrupted sheet and renaming the new one to the name it had previously. This works, but it is not possible to copy the ActiveX Controls, because they will be renamed and the appropriate code will not be in them, however the Form Control on the spreadsheet used to open the UserForm will work just fine, which is why I think my only solution would be to change all ActiveX to Form Control.
Would appreciate some help.
Instead of using ActiveX buttons, use Shapes and set the Shape.OnAction property to call the macro that you would normally place behind the ActiveX button. In the macro you can use Application.Caller to get the name of the shape that was clicked, thus allowing you to know which button was clicked and to branch your code accordingly.

VBA and Publisher - replacing text in a master page

Recently I have been struggling with publisher at work, and the most recent source of concern is the software inability to give something a simple as an auto-updating "total pages" count.
I decided to look into macros for a solution, but with my limited knowledge of VBA I only managed to get so fat before hitting a roadblock.
I want to create a macro that automatically starts when the document is opened and as frequently as possible updates a textbox in the master page.
The textbox should show "page X of Y", but due to publisher limitations only the "X" is automatically updated without using a macro.
What I have right now:
Private WithEvents PubApp As Publisher.Application
Private Sub Document_Open()
Set PubApp = Publisher.Application
End Sub
Private Sub PubApp_WindowPageChange(ByVal Vw As View)
MsgBox "Your publication contains " & _
ActiveDocument.Pages.Count & " page(s)."
End Sub
The macro automatically starts when the document is opened and creates a popup with the total page number every time the user changes page.
So, I accomplished the first half of my goal, but I need some help with the rest.
Here's a working macro for anyone with the same problems
Private WithEvents PubApp As Publisher.Application
Private Sub Document_Open()
Set PubApp = Publisher.Application
End Sub
Private Sub PubApp_WindowPageChange(ByVal Vw As View)
Dim mp As MasterPages
Set mp = ActiveDocument.MasterPages
With mp.Item(1)
.Shapes(19).TextFrame.TextRange.Text = ""
.Shapes(19).TextFrame.TextRange.InsertPageNumber
.Shapes(19).TextFrame.TextRange.InsertBefore _
NewText:="Page "
If ActiveDocument.Pages.Count > 9 Then
.Shapes(19).TextFrame.TextRange.InsertAfter _
NewText:=" of " & _
ActiveDocument.Pages.Count
Else
.Shapes(19).TextFrame.TextRange.InsertAfter _
NewText:=" of 0" & _
ActiveDocument.Pages.Count
End If
End With
End Sub
X In mp.Item(X) identifies your master page (in case you have more than one).
Y in Shapes(Y) identifies the target text box.
The macro runs in background without the need of active input and refreshes the content of the target text box on every page change.
Since the automatic page numbering format used in the document is "01, 02, 03 ... 11, 12 13" I included an If selection to add a 0 before the total page count if said number is lower than 10.
I'm sure there are more elegant ways of solving this problems, but hey, at least it works.
Here is a clarification for anyone else trying to research the limited information on programmatically changing the header or footer in the master pages using VBA in Publisher. This tip also includes a clarification of how to Add Text To the Left,Center,Right portions of the header:
All the credit goes to M.Baroni
Function FixHeader1()
Dim mp As MasterPages
Set mp = ActiveDocument.MasterPages
With mp.Item(1)
.Header.TextRange.text = "My Publication" & vbTab
.Header.TextRange.InsertPageNumber
.Header.TextRange.InsertAfter NewText:=vbTab & MonthName(Month(Date)) & " " & Year(Date)
End With
End Function

Delete Selected Text in a Text Box

Hey fellow Stackoverflow users,
i'm trying to create a text editor with HTML tag functions (for export) and can't get a solution working. Therefor i created a textbox where the user should be able to insert the text. For this insert function i need the in the title described function to delete the selected text (only the selected text, the text around it stays) inside the textbox. Even SendKeys won't function right.
Please let me know if anyone got an idea, thanks upfront!
EDIT: Here's the corrected code for the bold button with maintextbox as textbox for the user's text:
Private Sub BoldButton_Click()
If maintextbox.SelLength = 0 Then
MsgBox ("Please highlight the text you want to edit!")
Else
SelectionText = maintextbox.SelText MsgBox ("SelText: " & SelectionText)
maintextbox.SelText = "<b>" & _
SelectionText & _
"</b>"
End If
End Sub
Simply:
TextBox.SelText = ""
The .Sel* methods all relate to the text currently selected within the control. SelText = "" replaces the current selection with nothing, thereby deleting it whilst preserving the surrounding unselected text.

How can i make link with selected range in word?

I am using word2003 document for processing in my document i have to made link with two string variables (Not in the sense Footnotes and Endnotes)
{Page 1} Best quote from David Brinkley[1]
{Page 6}[1] A successful person is one who can lay a firm foundation with the bricks that others throw at him
I suppose to use Footnote/Endnote for the value [1] to link but it cause some changes while editing the the Footnote/Endnote. Is there any other way to make link between the selected string?
There are probably many ways to do this, but you could bookmark the text on page 6, then add a hyperlink on page 1 that points to the bookmark. This can be done without VBA code:
Select the text for the bookmark on page 6.
Insert/Bookmark
Name the bookmark and add it
Select the text for the bookmark on page 1.
Insert/Hyperlink
Click the Bookmark button and select the bookmark in the list
The equivalent in VBA:
Option Explicit
Sub AddHyperlinkToBookmark()
Dim oBookmark As Bookmark
Dim oHyperlink As Hyperlink
'***** Add code to select text for bookmark
Set oBookmark = ThisDocument.Bookmarks.Add("BookmarkName", Selection.Range)
'***** Add code to select text for link
Set oHyperlink = ThisDocument.Hyperlinks.Add(Selection.Range, "", "BookmarkName")
Set oBookmark = Nothing
Set oHyperlink = Nothing
End Sub