Active Document is called “FinalDocument”
The following is in a loop (ie. Report1, Report2, etc..)
DoCmd.OpenReport ReportName, acPreview, , "Report1”
DoCmd.OutputTo acOutputReport, ReportName, acFormatRTF, “WordDoc1”
ActiveDocument.Content.InsertFile FileName:=WordDoc1, Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
I am successfully getting text from Report1 to FinalDocument.
My problem is that on every pass throught loop, InsertFile is REPLACING text.
I want to APPEND Text from Report1 to FinalDocument thereby building FinalDocument one pass at a time.
You should work with a Range object so that you can target the insertion point. First, set theRange to the entire document, then "collapse" it to a point - think of it like pressing the right-arrow key to make a selection a blinking cursor at the end of the selection.
For example:
Dim rng as Word.Range
Set rng = ActiveDocument.Content
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd. 'Or use 0
rng.InsertFile FileName:=WordDoc1, Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
Try this:
ActiveDocument.ActiveWindow.Selection.InsertFile FileName:=WordDoc1, Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
Background: I currently have a document that is broken up into separate sections by section breaks in Word. I have a macro to print pdf's of the sections to a users chosen directory and a macro to export static pages as pdf's. I've entered the page numbers in the export macro for the time being because the save function works a lot faster than the print as pdf function. But I would like to have the macro export sections as pdfs with page numbers that can change.
Note: Section pages can change depending on the work I am doing on my master file, so using static page numbers in my macro is only temporary solution. Resolving this is really important for me.
What I Have So Far (This is the export macro):
Sub PLANv()
'
' PLANv Macro
'
'
Dim strName As String
strName = InputBox(Prompt:="Save To:", Title:="Save file to:", _
Default:="C:\Users\PRESTONAVH\Desktop\Task Order Files\")
If strName = vbNullString Then
Exit Sub
Else
End If
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
strName & "PLAN.pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=3, To:=4, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
What I Would Like to Have:
It seems that my macro that exports as a PDF does not allow me to put the sections in the range field, I've tried and it always gives me an error message. I currently have the static page range in there (3-4). I was thinking that maybe there there is some code I could enter before that, that would return the starting page number and ending page number of the section I'm exporting. Then I could assign a string to whatever is returned and then in the range function enter those strings as the page numbers?
I'm really not good at this stuff but have been going through lots of forums and trying to piece together what other people have suggested with no luck. There is probably a much easier solution, but as long as it works that would really be great. I've been trying to figure this out for some time now, but I've gone through too many forums and am a VBA super beginner.
If anyone would be able to help me out I would really appreciate it.
Thank You
Update:
I tried the export section code as recommended but my fields were erased in the exported documents and a blank page added. So I'm trying to use the section range to set the first and last integers of the export range. I can get intValue1 which gives me the last page of the section range. But I don't know how to get intValue2 for the first page of the section range. Below is what I added in between my save prompt and the export code.
Dim intValueR As Range
Dim intValue1 As Integer
Dim intValue2 As Integer
Set intValueR = ActiveDocument.Sections(3).Range
intValue1 = CStr(intValueR.Information(wdActiveEndPageNumber))
intValue2 = ??
(SOLVED)
Hi everyone, thanks for helping me out, I have the final code that is working well for me now. Here's the code:
Dim strName As String
strName = InputBox(Prompt:="Save To:", Title:="Save file to:", _
Default:="C:\Users\PRESTONAVH\Desktop\Task Order Files\")
If strName = vbNullString Then
Exit Sub
Else
End If
Dim intValue1 As Integer
Dim intValue2 As Integer
intValue1 =
ActiveDocument.Sections(1).Range.Information(wdActiveEndPageNumber) + 1
intValue2 =
ActiveDocument.Sections(2).Range.Information(wdActiveEndPageNumber)
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
strName & "PLAN.pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=intValue1,
To:=intValue2, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
Alright so I found a solution, it's really ugly but it works. Since I didn't know how to set the first part of the section range as an integer but I did know how to set the last part of the section range as an integer. I simply set the first part as the last part of the previous section's range plus 1.
The result is this bit of code:
Dim intValueR As Range
Dim intValueR2 As Range
Dim intValue1 As Integer
Dim intValue2 As Integer
Set intValueR = ActiveDocument.Sections(3).Range
Set intValueR2 = ActiveDocument.Sections(2).Range
intValue1 = CStr(intValueR.Information(wdActiveEndPageNumber))
intValue2 = CStr(intValueR2.Information(wdActiveEndPageNumber)) + 1
If someone has a cleaner way of doing this, that would be fantastic. But if someone needs something like this, it's working for me.
Shout out to #TonyM and #jsotola!
I would suggest that you first select the section you want to export and then use wdExportSelection instead of wdExportFromTo. To select the section, see this page where Selection.Range.Sections.First.Range.Select is used, or experiment with the macro recorder. But, if you do want to use wdExportFromTo then the answer to your question about "entering those strings as page numbers" is that, yes, you have the right idea, but you would need to use integers rather than strings.
this is how to export a range ... arrived at this by recording a macro, which ended up starting with Selection.ExportAsFixedFormat .... Selection is a range object .....
ActiveDocument.Range(800, 1000).ExportAsFixedFormat _
OutputFileName:=strName & "PLAN.pdf", _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, _
OptimizeFor:=wdExportOptimizeForPrint, _
ExportCurrentPage:=False, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=False, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
change first line to export 2nd section
ActiveDocument.Sections(2).Range().ExportAsFixedFormat _
you can do this to see what will be exported ... use for debugging, to see which range you are referrencing
ActiveDocument.Sections(2).Range().Select ' you can instead paste this line into the "Immediate Window", and then look at your document
Stop
I have small Template for MS Word. I want to add comment with hyperlink. I can already add the comment with the following code, but I want to set the author name of that comment. I don't know how to do that.
Here is my working code: (author is not set currently)
URLText = "https:\\www.google.com"
Selection.Comments.Add Range:=Selection.Range
With Selection
.TypeText (CommentText) 'Add comment text
.Hyperlinks.Add Anchor:=Selection.Range, _ 'Add hyperlink to comment
Address:=URLText, _
ScreenTip:=URLText, _
TextToDisplay:=URLText
End With
However I have tried by following code. Which set author name but I can't add hyperlink in my comment by this way:
Dim cmtMyComment As Comment
Dim link As Hyperlink
link.Address = URLText
link.ScreenTip = URLText
link.TextToDisplay = URLText
Set cmtMyComment = Selection.Comments.Add(Range:=Selection.Range, _
Text:=(CommentText)
cmtMyComment.Author = "ABC"
I didn't find property to set hyperlink.
Can anybody suggest me how to set author name? I have tried but didn't find any property.
Well there is an Author property which you can set like so and the hyperlink still works as you can see with the GIF below
Public Sub AddCommentWithLink()
URLText = "https:\\www.google.com"
Set Comment = Selection.Comments.Add(Range:=Selection.Range)
Comment.Author = "Donald Duck"
With Selection
.TypeText (CommentText)
.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=URLText, _
ScreenTip:=URLText, _
TextToDisplay:=URLText
End With
End Sub
Which will result in a comment like this
I need help with some code. I built a User Form in word using Bookmarks as references to where the text in each TextBox should go, I was able to accomplish that. The challenge I'm having now is using the Split Function. I want to grab each word from TextBox3 and place them in a table, then I want to search each word in an excel database and retrieve the info on the cell next to it (in a Vlookup sort of way). Each word from the TextBox3 should be in a different line.
Here is the code:
Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("bmCN").Range _
.InsertBefore TextBox1
.Bookmarks("bmOriJob").Range _
.InsertBefore TextBox2
.Bookmarks("bmOptJob").Range _
.InsertBefore TextBox3
.Bookmarks("bmJobD").Range _
.InsertBefore TextBox4
.Bookmarks("bmJobRes").Range _
.InsertBefore TextBox5
.Bookmarks("bmJobR").Range _
.InsertBefore TextBox6
.Bookmarks("bmBen").Range _
.InsertBefore TextBox7
.Bookmarks("bmTag").Range _
.InsertBefore TextBox8
End With
UserForm1.Hide
Selection.WholeStory
Selection.Fields.Update
Selection.Collapse Direction:=wdCollapseEnd
End Sub
Any help would be appreciated.
Try something like this with array and loop:
(see some comments inside the code below)
'let's create temporary array
Dim tmpArray As Variant
tmpArray = Split(TextBox3, " ")
Dim i As Integer
For i = 1 To UBound(tmpArray)
'we will load values to first column in first table in yur document
ActiveDocument.Tables(1).Cell(i, 1).Range = tmpArray(i - 1)
'here do your stuff with excel- load what you need
'ActiveDocument.Tables(1).Cell(i, 2).Range = something from excel
Next i
I need to create a macro to print two sheets and include a custom footer that references several of the cells.
I have tried so many combinations, but I don't know what I am doing wrong. I get the error Object does not support this property or method.
Sub PrintSummarySheet()
' PrintSummarySheet Macro
Sheets("Project Data Input").Select
With ActiveSheet.PageSetup
.CenterFooter = .Range("C6").Text And .Range("F2").Text _
And .Range("F4").Text And .Range("F5").Text
End With
Sheets(Array("Project Data Input", "Project Estimate Summary")).Select
Sheets("Project Data Input").Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Sheets("INSTRUCTIONS").Select
Sheets("Project Data Input").Select
End Sub
You are using With ActiveSheet.PageSetup but on the next line you are trying to refer to the Worksheet and not the PageSetup by doing .Range(...).
You need to replace .Range(...) by ActiveSheet.Range(...).
The Run-time error 13 Type mismatch occurs because you are using And to concatenate text instead of the concatenation operator &
.Range("C6").Text And .Range("F2").Text _
And .Range("F4").Text And .Range("F5").Text
Should be:
.Range("C6").Text & .Range("F2").Text & _
.Range("F4").Text & .Range("F5").Text