#1
|
|||
|
|||
ConmandButton dissappears when Content dispalyed on Page 1
Hi
I just created an ActiveX command button on Page 1 of document to initilize userform How can this be retained. ? because when clicking the command button of userform the document code overwrites and doest not display the command button as the content overwrites on page 1 any ideas if the document content is seen on Page 2. because on Page 1 i would just like the command button to retain coding in thisDocument for CommandButton on Page1 of document Code:
Private Sub cmdInitilizeuf_Click() Load userform1 userform1.Show vbModeless End Sub Code:
Private Sub cmdButton1_Click() Dim objWord As Word.Application Dim txtword As String Dim objDoc As Document Dim objRange As Range Dim objTable As Table Dim intRows As Integer Dim intCols As Integer txtWord = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add objDoc.Range.Text = txtWord Set objRange = objDoc.Range objRange.Collapse Direction:=wdCollapseEnd intRows = 8: intCols = 5 Set objTable = objDoc.Tables.Add(objRange, intRows, intCols) objTable.Borders.Enable = True End Sub |
#2
|
||||
|
||||
If you are running this button from a Word document, then you must already be in Word, so why create a new instance of Word? However when you create a new document using the code Set objDoc = objWord.Documents.Add this creates a document from the normal template which hopefully will not have your button. You need to create a new document from the template containing the button.
In any case you do not really need a button at all. You could use an autonew macro in the template you are creating to call the userform and that template could already have the table which is fixed in your code, so there is no need to build it.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Hi
the atmosphere is completely new in Word Classrom then Excel Classrom. I may take time in absorbing new syntaxes for VBA word. Please excuse me. i tried the following instead of Set objDoc = objWord.Documents.Add Set objDoc = Documents.Open("C:\WordTrial\cmdBtnWord.docm") Then the above also led to overwriting. I was trying for a possibility just like in Excel usually My first worksheet will have command button to initialize the userform. So in same manner was trying Quote:
ActiveX command button on Page 1 of document. i tried moving and placing. it did not move Just like in Excel you can paste it anywhere on the worksheet Quote:
if no then what to code in AutoNew Macro or is what i've coded and to impletement the same in Autonew Macro Also are you meaning the same to code in Document_Open() ? Thanks SamD |
#4
|
||||
|
||||
There are some similarities between Word and Excel, but they are not the same when it comes to programming. If you Add a document without reference to the template, which is what you had originally, then a new document is created from the normal template. Essentially a blank document unrelated to your document with the button. If you want to create a new document that has the button then you need to refer to the template e.g.
Set objDoc = Documents.Add(Template:="C:\WordTrial\cmdBtnWord.d ocm") This will create a new document that is an unnamed facsimile of the original. Or as it is a macro enabled document, you can simply open it as in your subsequent attempt. Having got the document open/created your code then writes a text value to the document range, which overwrites everything in the document body. You need instead to set a range to the start (or end) of the document and write to that e.g. Code:
Dim txtword As String Dim objDoc As Document Dim objRange As Range Dim objTable As Table Dim intRows As Integer Dim intCols As Integer txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr Set objDoc = Documents.Add(Template:=ThisDocument.FullName) Set objRange = objDoc.Range objRange.Collapse Direction:=wdCollapseStart objRange.Text = txtword objRange.Collapse Direction:=wdCollapseEnd intRows = 8: intCols = 5 Set objTable = objDoc.Tables.Add(objRange, intRows, intCols) objTable.Borders.Enable = True You would need to modify the ribbon in order to add a button there. See Customize the Office Ribbon (It doesn't take rocket science)
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com Last edited by gmayor; 08-12-2019 at 12:12 AM. |
#5
|
|||
|
|||
Hello
I tried as per your guidance but what happens when Quote:
Quote:
i tried the above without the below comments in Marked RED but added with syntax marked in Blue Code:
Dim txtword As String Dim objDoc As Document Dim objRange As Range Dim objTable As Table Dim intRows As Integer Dim intCols As Integer txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr Set objDoc = Documents.Add(Template:="C:\WordTrial\cmdBtnWord.docm") With objDoc .Range.Text = txtword .Range.Font.Name = "Tahoma" .Range.Font.Size = "12" .Paragraphs.SpaceAfter = 0 End With Set objRange = objDoc.Range objRange.Collapse Direction:=wdCollapseStart objRange.Text = txtword objRange.Collapse Direction:=wdCollapseEnd intRows = 8: intCols = 5 Set objTable = objDoc.Tables.Add(objRange, intRows, intCols) objTable.Borders.Enable = True Quote:
|
#6
|
||||
|
||||
As I explained, if you write to the document range you replace anything in the document range, which is what your blue text does and results in an empty one page document.
objrange is a range. It is set just above the red section, so objRange.Range.Text = txtword is incorrect. It should be objRange.Text = txtword which is what the code shows. I assume there is nothing on page 2, so you can set the range to the document then collapse the range to its end, rather than the start, and that will write to the empty page 2 which is at the end of the document. The macro below does not need any alteration to run from CommandButton1 Code:
Private Sub CommandButton1_Click() Dim txtword As String Dim objDoc As Document Dim objRange As Range Dim objTable As Table Dim intRows As Integer Dim intCols As Integer intRows = 8: intCols = 5 txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr Set objDoc = Documents.Add(Template:=ThisDocument.FullName) Set objRange = objDoc.Range 'set the range to the document With objRange .Collapse Direction:=wdCollapseEnd 'collapse the range .Text = txtword 'write to the range .Font.Name = "Tahoma" 'format the range .Font.Size = "12" .ParagraphFormat.SpaceAfter = 0 .Collapse Direction:=wdCollapseEnd 'collapse the range End With 'add a table Set objTable = objDoc.Tables.Add(objRange, intRows, intCols) 'Format the table here objTable.Borders.Enable = True objTable.Select 'select the table Selection.Collapse Direction:=wdCollapseEnd 'collapse the selection 'The document is displayed with the focus at the selection 'Clean up Set objDoc = Nothing Set objRange = Nothing Set objTable = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Tried with both wdCollapseEnd and wdCollapseStart ' RED and Blue Respectively
Code:
With objRange .Collapse Direction:=wdCollapseEnd 'collapse the range .Text = txtword 'write to the range .Font.Name = "Tahoma" 'format the range .Font.Size = "12" .ParagraphFormat.SpaceAfter = 0 .Collapse Direction:=wdCollapseEnd 'collapse the range End With but when i changed as below, the range was seen in completely New As Document1 Code:
With objRange .Collapse Direction:=wdCollapseStart 'starts the Range in NewPage .Text = txtword 'write to the range .Font.Name = "Tahoma" 'format the range .Font.Size = "12" .ParagraphFormat.SpaceAfter = 0 .Collapse Direction:=wdCollapseEnd 'collapse the range End With Also with above coding trials. Observed one thing whenever Document1 or new document Opens with that one more document opens but without a Page and item in Ribbons are disabled. Could you explain why this happens ? |
#8
|
||||
|
||||
The code doesn't create a second page. It writes to a second page that you implied existed in your document. If you want a second page you need to add it then reset the range e.g. as attached.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
|||
|
|||
Thank you so much for your attached code. I've tried the same It worked perfectly as you mentioned
Quote:
Quote:
|
#10
|
||||
|
||||
You can work on the original document if you open it rather than add it, but you can only work on it once as the code alters the document. So if you do that retain an unaltered original.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#11
|
|||
|
|||
The .docm file is always opened as Original Document.
Quote:
So how to retain the original opened .docm with a code to add the page. |
#12
|
||||
|
||||
You have lost me now.
You can either use the original document as a template and create a new document from it, or you can open the original document and process that. If you change the original you will have to save it or the changes will be lost. Much depends on what the aim of the exercise is, because so far I have seen no reason at all for the macro or the button as you could simply save the table in the document to start off with, which makes more sense than creating the table using a macro.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#13
|
|||
|
|||
Thanks Gmayor
Sorry for very Late reply. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
using styles to generate content page | TYPO | Word | 2 | 12-20-2015 12:48 PM |
Insert the page before table of content without altering it | kbhalakiya | Word | 2 | 08-29-2012 03:53 AM |
Rotate Page and Content Within It. | walker140 | Word | 1 | 11-13-2011 11:08 PM |
Page number in table of Content | Ritu | Office | 1 | 09-19-2011 05:10 AM |
Paragraph Disappears when Tabbing it over | GWalkaa | Word | 11 | 07-22-2011 03:19 AM |