I am trying to create a button that copies the contents of an entire page, and pastes it to a new page within the same document. When I first formatted this as a MacroButton (CTRL + F9) it would work multiple times. Now I have tried it using a command button under Legacy forms (Developer tab) as a click event, and it will only work once. I am not sure why this is occuring. Here is my code:
Private Sub AddPage3_Click()
Const wdPageBreak = 7
ActiveDocument.Bookmarks("\page").Range.Copy
Selection.InsertBreak (wdPageBreak)
Selection.Paste
Selection.TypeBackspace
Selection.TypeBackspace
End Sub
When I press the button, it will create another page in the document with the contents of the active page (the page the button is located on), but it will not work a second time. If you have any suggestions please let me know :)!
After copy/paste, you can rename your commandbutton as follows and the commandbutton keeps running each time:
Private Sub AddPage3_Click()
Dim shp As InlineShape
Const wdPageBreak = 7
ActiveDocument.Bookmarks("\page").Range.Copy
Selection.InsertBreak (wdPageBreak)
Selection.Paste
Selection.TypeBackspace
Selection.TypeBackspace
For Each shp In ActiveDocument.InlineShapes
On Error Resume Next
If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then
If shp.OLEFormat.Object.Name = "AddPage31" Then
shp.OLEFormat.Object.Name = "AddPage3"
End If
End If
Next
End Sub
Related
When Word 2010 starts I would to automatically execute the VBA code
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
to make the document fit the page.
Many thanks for a tip
You can put your command(s) in the AutoExec macro.
Sub AutoExec()
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
End Sub
You could add a macro like the following to the 'ThisDocument' code module of a global template (e.g. Normal.dotm):
Private Sub Document_Open()
With ActiveWindow
'Reduce flickering while changing settings
.Visible = False
'Switch to a single view pane
.View.SplitSpecial = wdPaneNone
.View.Type = wdPrintView
.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
'Restore the window now that we're finished
.Visible = True
End With
End Sub
My current project is making a list for a coworker and since I like to over-complicate everything, I'm running macros for him to check off as he completes work. The description of work will change from a header style to normal when he's completed it, then update the ToC. All of this is working, but I have trouble with the content control staying selected sometimes. I can usually check and uncheck it once or twice with no problem, but eventually the cursor doesn't move out of the check box for some reason so subsequent clicks don't fire the OnEnter.
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
End If
End With
End Sub
Is there a way to force word to deselect the content control and move the cursor somewhere on the same line?
I tried Selection.MoveDown 1, Selection.Collapse direction:=wdCollapseEnd, and also Selection.MoveEnd but none work.
You can leverage the fact that, through the Content Control's Range the objects which contain it can be accessed. For example, you can "drill up" to the paragraph in which the content control is located:
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
This could also be any character in the paragraph. The following (in my test) puts the selection immediately after the content control:
CCtrl.Range.Paragraphs(1).Range.Characters(4).Select
Incorporated into your code:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
'Select the last character in the paragraph (the paragraph mark)
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
'Remove the selection, so the cursor blinks at the end of the paragraph
Selection.Collapse
End If
End With
End Sub
I have an excel macro that does two very simple things:
It displays the current date and time in a little window.
It copies the display as a text string for pasting into other apps as needed.
The cell that is displayed has the following formula in it:
=TEXT(NOW(),"yyyy.MM.dd hh:mm:ss")
Every 5 seconds, the macro refreshes the time and the clock ticks.
My problem is that when I copy the time from the cell, I don't consistently get the contents pasted to the clipboard. Sometimes the cell contents are posted to the clipboard. I can't figure out why it works sometimes and not others as there isn't a lot going on. It should just always work.
I know the data aren't on the clipboard because I can try pasting the clipboard into different programs like notepad and other text apps and nothing happens.
The entire code is in a single module.
Dim stopSwitch As Integer
Dim NextTick
Sub myupdate()
If ActiveCell.Address = "$B$1" Then
growWindow ' resize window beyond just clock display
stopTime '
Exit Sub ' stop updating
End If
Range("a1").Select
Calculate
DoEvents
If ActiveWorkbook.Name = "calendar clock.xlsb" Then shrinkWindow
NextTick = Now + TimeValue("00:00:05") ' give me 5 seconds to copy/paste
Application.OnTime NextTick, "myupdate"
ThisWorkbook.Save ' futile attempt to prevent save dialog
End Sub
Sub auto_open()
' to stop clock, tap right arrow to select cell b1 when workbook is active
Range("a1").Select
myupdate
End Sub
Sub growWindow()
Application.Width = 768
Application.Height = 621.75
ThisWorkbook.Save
End Sub
Sub shrinkWindow()
' strip decorations so window is as small as possible
Application.DisplayFormulaBar = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False
' move window to second monitor and size to single cell display
Application.WindowState = xlNormal
Application.Top = 0
Application.Left = -720
Application.Width = 174
Application.Height = 127
ActiveWindow.WindowState = xlMaximized
End Sub
Sub stopTime() ' called when workbook is closed
On Error Resume Next
Application.OnTime NextTick, "myupdate", schedule:=False
Range("b1").Select
End Sub
Sub copyTime()
Range("a1").Copy ' copy time
Range("f5").PasteSpecial xlPasteValues ' strip formatting
Range("f5").Copy ' copy time as text
DoEvents ' hack to attempt to make copy work consistently
End Sub
The above code sizes the window and updates the clock every 5 seconds.
To copy the clock as text to the clipboard, I have the following code in the workbook
Private Sub Workbook_Activate()
Application.OnKey "^c", "module1.copyTime"
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "^c"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' turn off auto update
Module1.stopTime
' resize window so if I open another spreadsheet, it's a reasonable size
Application.WindowState = xlNormal
Application.Width = 768
Application.Height = 621.75
Application.OnKey "^c"
ThisWorkbook.Save ' try to prevent save dialog at close
End Sub
I modified the copyTime function to verify the ^C is seen by selecting the unformatted cell and I can see that the data consistently go to the cell so I know my problem isn't with the Range("a1").copy step in copytime or the pastespecial to cell f5.
That leaves the range("a5").copy command as the bad actor when the copy fails which is weird. It's as if copy works as long as the data are kept inside the spreadsheet but fails to update the external clipboard consistently.
That observation led me to try setting application.cutcopymode to xlcopy, true and false to see if that helped. The only effect I saw from trying all the settings is whether I saw f5 get highlighted with a marquee or not - none of the setting forced a copy to the external clipboard.
I tried waiting for a clock tick before copying to see if something was clearing the clipboard following the copy if it was time to update the clock. That appeared to help somewhat but, again not consistently.
So why does the copy fail to always update the clipboard? And why does it not work when it doesn't and does when it does? Even better, how can I modify this code so it always exports to the external clipboard?
Try using this method, it's always reliable for me
Dim TimeInClip As MSForms.DataObject
Set TimeInClip = New MSForms.DataObject
TimeInClip.SetText Range("A1").Value
TimeInClip.PutInClipboard
Try
Sub copyTime()
Range("a1").Copy ' copy time
Range("f5").PasteSpecial xlPasteValues ' strip formatting
Application.CutCopyMode = False ' Clear Excel clipboard
Range("f5").Copy ' copy time as text
DoEvents ' hack to attempt to make copy work consistently
End Sub
You said that you tried Application.CutCopyMode, but have you tried it that way?
It only forces the application to clear the clipboard before copying something else, which should then copy properly on the fresh clipboard.
This switches Microsoft Word 2013 to the Home Tab:
Sub HomeTab()
Application.ScreenUpdating = False
SendKeys "%h%"
ScreenUpdating = True
End Sub
This opens a document:
Sub OpenToDoLIst()
Documents.Open FileName:="C:\Users\Name\Name Documents\Microsoft Word Documents\Work Menu\To Do List.doc"
End Sub
But if I combine the two like this:
Sub WorkOpenToDoList()
Application.ScreenUpdating = False
SendKeys "%h%"
ScreenUpdating = True
Documents.Open FileName:="C:\Users\Name\Name Documents\Microsoft Word Documents\Work Menu\To Do List.doc"
End Sub
It doesn’t switch the original document I started from when I activated the macro to the Home Tab, it only opens the document. I suspect it's not waiting for the HomeTab steps to finish in the original window, but I don't know why.
It seems like this guy has had the same problem as me, but the vbModeless doesn't seem to do the trick.
I'm creating a simple add-in, and I want VBA to open a word file, copy the entire content and paste it in the original document.
It works perfectly when executed from the VBA editor or even after the first run where it debugs, but opening a Word instance for the first time and trying to execute the code from the add-in seems to be a problem.
The code is as follows:
Sub insertFigureFrame(control As IRibbonControl)
StandardFrames.StartUpPosition = 0
StandardFrames.Top = Application.Top + (Application.Height - StandardFrames.Height) * 0.5
StandardFrames.Left = Application.Left + (Application.Width - StandardFrames.Width) * 0.5
StandardFrames.Show
End Sub
Sub Standard()
Dim OriginalDocument As String
Dim SaveChanges As Boolean
Dim doc As Document
On Error GoTo err:
Application.DisplayAlerts = wdAlertsNone
Application.ScreenUpdating = False
OriginalDocument = ActiveDocument.Path
Documents.Open MyTemplate
Selection.WholeStory
Selection.Copy
Documents("MyTemplate").Close (SaveChanges = False)
Documents.Open (OriginalDocument)
Selection.PasteAndFormat wdPasteDefault
Exit Sub
err:
Call errHandling
End Sub
The StandardFrames Userform calls the sub Standard.
Any idea what might cause the problem?
EDIT:
I'm using Word 2007 and Windows XP.
Try hidding your userform before you open a document:
Sub Standard()
StandardFrames.Hide
'The rest of your code
End Sub